can someone check my codes where my error was?!

import java.io.*;
import java.util.Arrays;
public class exer06_01{
	public static void main(String [] argv)throws Exception{
		
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		
		String sh = " ";
		String str = new String(sh);
		System.out.print("Enter a string: ");
		str = br.readLine();
		System.out.println("The reverse: "+ReverseString.reverseIt(str));
		
	 if (ReverseString.reverseIt(str)) {
      System.out.println("It IS a palindrome!");
    } else {
      System.out.println("It is NOT a palindrome!");
    }
    System.out.println();
  	}
}

thanks in advance..;) ..it will be a big help or me..

Recommended Answers

All 9 Replies

First of all you need to tell us what is your problem.
Second, you forgot to post the
ReverseString.reverseIt() method.

We don't need to know how you call your method. The code you posted is correct. You call the method and you print it. What you should have posted is the implementation of the reverseIt().

There are a couple of problems, the biggest being that you didn't create the method ReverseIt.
Also, you had a few syntax errors. Make sure you check everything over thoroughly.

public class reverse {

	
	public static String reverseIt(String a) {
	
		int length = a.length();
		StringBuilder reverse = new StringBuilder();
		for(int i = length; i > 0; --i) {
			char result = a.charAt(i-1);
			System.out.println(""+result);
			reverse.append(result);
		}
		return reverse.toString();
	}
	
}

There are a couple of problems. Be sure to check your code's syntax a little more carefully. Also, consider going through your program step by step using a debugger to monitor the status of each of your variables at different stages.

If you have read carefully the code you would have seen that:

1) the method reverseIt returns boolean not String.
2) the poster doesn't want the solution. He probably has the solution and wants as to check the code. The solution is in the method reverseIt that he wrote and we still wait to see that code
3) your code is wrong and you will get an ArrayIndexOutOfBoundsException if you run it

If you have read carefully the code you would have seen that:

1) the method reverseIt returns boolean not String.
2) the poster doesn't want the solution. He probably has the solution and wants as to check the code. The solution is in the method reverseIt that he wrote and we still wait to see that code
3) your code is wrong and you will get an ArrayIndexOutOfBoundsException if you run it

The palindrome check does have problems. Removed it. However, the ReverseIt that I wrote works fine and returns the exact reverse of the string...
And now I've corrected the check. So that works too. Thank you for the pointer on the .equals, I'm still relatively new to Java myself.
The way I correct code is to fix any pressing error and then write some sections in ways I feel are simpler or more reasonable. Returning boolean while the comparison between the reversed string and the input lies in the main would be pointless, so I compared the returned String with the input String.

import java.util.*;

public class palindrome{
	
private static String str;

	public static void main(String [] args) {
		
		Scanner input = new Scanner(System.in);
		
		System.out.print("Enter a string: ");
		str = input.nextLine();
		System.out.println("The reverse: "+reverse.reverseIt(str));
		
	 if (reverse.reverseIt(str).equals(str)){
      System.out.println("It IS a palindrome!");
	 } else {
      System.out.println("It is NOT a palindrome!");
    }
    System.out.println();
  	}
}

Perhaps when I was writing the post I was looking an older version of the code.

There was a problem at first but it's fixed now; again, thanks for the pointer about using .equals, I'm relatively new to Java.
The reason I changed reverseIt to return String rather than boolean is because his palindrome check lies in his main; returning boolean would be pointless then.

Actually he wrote something like this:

if (ReverseString.reverseIt(str))

which indicates that the method returns true (boolean) if it is palindrome.
There is a way as you know that checks if a string is palindrome without actually calculating the palindrome.
The reason why you were confused, I believe was the name of the method:
reverseIt which means what you expected.

That is why I am still waiting for him to post the code. We can only speculate without it.

Member Avatar for sravan953

Try:

import java.io.*;
class string
{
void palindrome()throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Please enter a sentence/word: ");
String s=br.readLine(),s1="";
for(int j=s.length()-1;j>=0;j--)
{
s1+=s.charAt(j);
}
if(s1.equalsIgnoreCase(s))
{
System.out.println("Palindrome");
}
else
{
System.out.println("Not a palindrome.");
}
}
}

There is a much more simpler approach to the Palindrome problem. I would suggest using StringBuffer in place of String, it provides many effective methods like reverse(). This method automatically reverses the String we need to reverse and then compare.
Check out
SNIP
for more info on this type of implementation. I hope the post would be helpful in realizing a much simpler approach.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.