11 Replies - 7457 Views - Last Post: 27 January 2009 - 06:27 PM Rate Topic: -----

#1 amitdhar  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 49
  • Joined: 01-February 08

palindrome with StringBuffer

Post icon  Posted 26 January 2009 - 08:18 PM

I am trying a program which will print all the palindrome words from a sentence using StringBuffer, but am not geting the answer. Please help me. The code I have written is :
import java.io.*;
import java.lang.*;
class palindrome
{
public void check()throws IOException
{
BufferedReader x= new BufferedReader(new InputStreamReader(System.in));
System.out.println("plz enter the sentence");
String s=x.readLine();
String w="";
s=s+" ";
for(int i=0;i<s.length();i++)
{
char c=s.charAt(i);
if (c!=' ') w=w+c;
else
{
StringBuffer s1=new StringBuffer(w);
StringBuffer s2=new StringBuffer(w);
s1=s1.reverse();
if (s1==s2) System.out.println(s1);
w="";
	}
}
	}
}



Is This A Good Question/Topic? 0
  • +

Replies To: palindrome with StringBuffer

#2 NuclearPeon  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 27
  • Joined: 08-January 09

Re: palindrome with StringBuffer

Posted 26 January 2009 - 08:40 PM

Instead of using "==" to compare two strings, use the .equals() methods.

ex.

if(s1.equals(s2)) System.out.println(s1);


The way you're doing it is a little confusing.
Could you PLEASE comment your code?

This post has been edited by NuclearPeon: 26 January 2009 - 08:49 PM

Was This Post Helpful? 0
  • +
  • -

#3 amitdhar  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 49
  • Joined: 01-February 08

Re: palindrome with StringBuffer

Posted 26 January 2009 - 08:53 PM

View PostNuclearPeon, on 26 Jan, 2009 - 07:40 PM, said:

Instead of using "==" to compare two strings, use the .equals() methods.

ex.

if(s1.equals(s2)) System.out.println(s1);


The way you're doing it is a little confusing.
Could you PLEASE comment your code?



I have tried but this "equals()" also not working
Was This Post Helpful? 0
  • +
  • -

#4 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: palindrome with StringBuffer

Posted 26 January 2009 - 09:06 PM

http://www.dreaminco...snippet2748.htm
Was This Post Helpful? 0
  • +
  • -

#5 NuclearPeon  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 27
  • Joined: 08-January 09

Re: palindrome with StringBuffer

Posted 26 January 2009 - 09:12 PM

Hmmm. Perhaps what you are doing is comparing StringBuffer objects instead of the actual strings.

If I may make another suggestion, try something like this:

if(s1.toString().equals(s2.toString())) System.out.println(s1);


I've done this example before and I did it by reading the string backwards via a loop, then compared both strings (ignoring whitespaces and punctuation).

This post has been edited by NuclearPeon: 26 January 2009 - 09:13 PM

Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: palindrome with StringBuffer

Posted 26 January 2009 - 09:22 PM

The StringBuffer class does not have an equals() method that compare the characters inside them

You will have to convert them into String so the equals method of String will be called

if(s1.toString().equals(s2.toString())
Was This Post Helpful? 0
  • +
  • -

#7 TriggaMike  Icon User is offline

  • Using up all your 1's and 0's
  • member icon

Reputation: 85
  • View blog
  • Posts: 1,103
  • Joined: 26-September 08

Re: palindrome with StringBuffer

Posted 26 January 2009 - 09:25 PM

I agree with Da... Nuclear Peon, I really have no idea what you're trying to do, and your code is formatted very poorly.

import java.io.*;

//Don't import this, it is automatically imported
import java.lang.*;

//You shouldn't use the default access modifier
class palindrome
{
	public void check()throws IOException
	{
		//Why are you using BufferedReader? Try java.util.Scanner;
		BufferedReader x= new BufferedReader(new InputStreamReader(System.in));
		
		//Why are you using improper English in a program?
		System.out.println("plz enter the sentence");
		
		//Variable names should be descriptive
		String s=x.readLine();
		
		//Once again, descriptive
		String w="";
		
		//Why are you adding a space to the end of this?
		s=s+" ";
		
		//You are able to format like this, makes it much easier to read
		for(int i = 0; i < s.length(); i++)
		{
			//Again, descriptive
			char c=s.charAt(i);
			
			//What is the purpose of this
			if (c!=' ') 
				w=w+c;
			else
			{
				//??? What are you doing here? Why are you using StringBuffers?
				StringBuffer s1=new StringBuffer(w);
				StringBuffer s2=new StringBuffer(w);
				
				//Just need it to reverse it?
				s1=s1.reverse();
				
				//This should work now that I added a block of code
				if (s1.equals(s2)) 
				{					
					//Only printint out the string?
					System.out.println(s1);
					
					//Why are you clearing w?
					w="";
				}
			}
		}
	}
}


Study all of my comments, and the formatting of the code.
Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: palindrome with StringBuffer

Posted 26 January 2009 - 09:43 PM

View PostTriggaMike, on 26 Jan, 2009 - 08:25 PM, said:

I agree with Da... Nuclear Peon, I really have no idea what you're trying to do, and your code is formatted very poorly.

import java.io.*;

//Don't import this, it is automatically imported
import java.lang.*;

//You shouldn't use the default access modifier
class palindrome
{
	public void check()throws IOException
	{
		//Why are you using BufferedReader? Try java.util.Scanner;
		BufferedReader x= new BufferedReader(new InputStreamReader(System.in));
		
		//Why are you using improper English in a program?
		System.out.println("plz enter the sentence");
		
		//Variable names should be descriptive
		String s=x.readLine();
		
		//Once again, descriptive
		String w="";
		
		//Why are you adding a space to the end of this?
		s=s+" ";
		
		//You are able to format like this, makes it much easier to read
		for(int i = 0; i < s.length(); i++)
		{
			//Again, descriptive
			char c=s.charAt(i);
			
			//What is the purpose of this
			if (c!=' ') 
				w=w+c;
			else
			{
				//??? What are you doing here? Why are you using StringBuffers?
				StringBuffer s1=new StringBuffer(w);
				StringBuffer s2=new StringBuffer(w);
				
				//Just need it to reverse it?
				s1=s1.reverse();
				
				//This should work now that I added a block of code
				if (s1.equals(s2)) 
				{					
					//Only printint out the string?
					System.out.println(s1);
					
					//Why are you clearing w?
					w="";
				}
			}
		}
	}
}


Study all of my comments, and the formatting of the code.

After all the post we have about palyndromes and the way to reverse a String (recursively or not) I think that using a StringBuffer that has a reverse() method is a very good idea

StringBuffer s1 = new StringBuffer(w);
StringBuffer s2 = new StringBuffer(w);
s2 = s2.reverse();
w = "";	  // set to nothing to restart the appending of char

// I admit this one is useless
s=s+" ";



Here is a shortest version without all the useless stuff:

import java.io.*;
//Don't import this, it is automatically imported
import java.util.StringTokenizer;

//You shouldn't use the default access modifier
class palindrome
{
	public static void main(String[] arg) throws IOException
	{
		//Why are you using BufferedReader? Try java.util.Scanner;
		BufferedReader x= new BufferedReader(new InputStreamReader(System.in));
		
		//Why are you using improper English in a program?
		System.out.println("plz enter the sentence");
		
		//Variable names should be descriptive
		String s=x.readLine();
		
		StringTokenizer st = new StringTokenizer(s);
		while(st.hasMoreTokens()) {
			String token = st.nextToken();
			StringBuffer s1 = new StringBuffer(token);
			s1.reverse();
			if(token.equals(s1.toString()))
				System.out.println(token + " is a palindrome");
		}
	}
}


This post has been edited by pbl: 26 January 2009 - 09:48 PM

Was This Post Helpful? 0
  • +
  • -

#9 NuclearPeon  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 27
  • Joined: 08-January 09

Re: palindrome with StringBuffer

Posted 26 January 2009 - 09:55 PM

TriggaMike,

The code I write is formatted quite well.
I was writting said code back in a format that the poster would recognize, as it was in his style.

Sometimes, reformatting someone's code is seen as a personal attack on style, and I don't wish to portray that image.

EDIT: on second thought, if it's particularly bad I should probably be a good role model... :]

This post has been edited by NuclearPeon: 26 January 2009 - 09:59 PM

Was This Post Helpful? 0
  • +
  • -

#10 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: palindrome with StringBuffer

Posted 26 January 2009 - 10:00 PM

View PostNuclearPeon, on 26 Jan, 2009 - 08:55 PM, said:

TriggaMike,

The code I write is formatted quite well.
I was writting said code back in a format that the poster would recognize, as it was in his style.

Sometimes, reformatting someone's code is seen as a personal attack on style, and I don't wish to portray that image.

I think it is part of the "pedagogic" philosophy of this site to "try" to show good coding practice
So in a certain way, TriggaMike was right
on the other hand, most of the code post here is posted by newbie so ... yes you can suggest better code indentation and to add comments but if you can't pass over that and try to understand horrible code to fix problem better to forget about replying here.

Give comments on bad coding practice, yes, but if you refuse to fix the problem because the code is poorly presented your remarks are kind of useless

Thanks for helping newbies
Happy programming

This post has been edited by pbl: 26 January 2009 - 10:02 PM

Was This Post Helpful? 0
  • +
  • -

#11 TriggaMike  Icon User is offline

  • Using up all your 1's and 0's
  • member icon

Reputation: 85
  • View blog
  • Posts: 1,103
  • Joined: 26-September 08

Re: palindrome with StringBuffer

Posted 27 January 2009 - 07:57 AM

Usually when I see poor programming practices I try to point them out and give reasons as to why people don't like them. In programming there are things that you consider more personal, but there are certain practices that aren't readable by anyone. If you read the comments essentially all I said was that he should rename his variables and I let Eclipse fix the indentation. Other than that I was just confused on what he was trying to accomplish with certain things.
Was This Post Helpful? 0
  • +
  • -

#12 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: palindrome with StringBuffer

Posted 27 January 2009 - 06:27 PM

View PostTriggaMike, on 27 Jan, 2009 - 06:57 AM, said:

Usually when I see poor programming practices I try to point them out and give reasons as to why people don't like them. In programming there are things that you consider more personal, but there are certain practices that aren't readable by anyone. If you read the comments essentially all I said was that he should rename his variables and I let Eclipse fix the indentation. Other than that I was just confused on what he was trying to accomplish with certain things.

Nothing against that. Perfectly legitimate. But may be the user does not use Eclipse and cannot do <ctrl>A<ctrl>I
I have nothing against your "pedagogic" comments/tutorials but after these please provide a solution to fix the shitty code or refrain from replying

Cheers
Paul-Benoit
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1