Reputation: 192
Hi I have a code that checks if a string is palindrome or not.the code is like this:
package ProjeTarahi;
import java.util.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.lang.String;
public class Main
{
public boolean CheckIsSymmetric(String s)
{
if (s.length()<=1)
{
return true;
}
if (s.charAt(0)==s.charAt(s.length()-1))
{
String sub = s.substring(1,s.length()-2);
return CheckIsSymmetric(sub);
}
else
{
return false;
}
}
public static void main(String args[])throws FileNotFoundException
{
Scanner sc=new Scanner(new FileInputStream(new File("in.txt")));
String input=sc.nextLine();
Main p=new Main();
if(p.CheckIsSymmetric(input)==true)
{
System.out.println("in reshte motegharen ast");
}
else
{
System.out.println("infinite");
}
}
}
I have written a code in c# that is exactly the same as the code above and its working very well but its not working properly in java and its output is always infinite. I stepped over my code and i think the problem is in the first if-statement of the CheckSymmetric() and it always jumps it but i don't know why.anybody can help me plz?
Upvotes: 4
Views: 10244
Reputation: 1
Surprisingly this type of question appear a lot in Computer-Science related classes. Here is my version of finding palindrome of a String. This version is design to strip away any non-word characters and reduce every character to lower case. It also work with numbers as it will just compare it as a String. It's only 13 lines long with a main and a Boolean method. No for loop, substring or anything.
Hope I help! -Cheers
p/s: I apologize for my style of indentation :D
import java.util.Scanner;
public class Palindrome {
public static void main(String[]args){
if(isReverse()){System.out.println("This is a palindrome.");}
else{System.out.print("This is not a palindrome");}
}
public static boolean isReverse(){
Scanner keyboard = new Scanner(System.in);
System.out.print("Please type something: ");
String line = ((keyboard.nextLine()).toLowerCase()).replaceAll("\\W","");
return (line.equals(new StringBuffer(line).reverse().toString()));
}
}
I used the StringBuffer integrated class of Java and the meta character "/W" (all non-word characters, must be capital W). Please feel free to criticize or discuss!
Upvotes: 0
Reputation: 12421
The only issue with your code is that you have to change:
String sub = s.substring(1,s.length()-2); to
String sub = s.substring(1,s.length()-1);
Your code won't give you the desired result and also you will be getting a IndexOutOfBoundsException when your input is of length 2.
I guess your issue may be that you are reading the string from file, and at the end of the file the editor might be adding some extra character with is not visible to the eye.
Insted of reading from a file try to hardcode and check your code
//String input=sc.nextLine();
String input = "d";
I guess this should work.
Upvotes: 0
Reputation: 5348
public boolean checkIsSymmetric(String s) {
return new StringBuilder(s).reverse().toString().equals(s);
}
Keep it simple and use the API.
Upvotes: 0
Reputation: 163
Java doc for String.substring:
* @param beginIndex the beginning index, inclusive.
* @param endIndex the ending index, exclusive.
It means that: String s = "AbccbA"; System.out.println(s.substring(1,s.length()-2));
returns: "bcc" but not "bccb" as you expected.
Upvotes: 0
Reputation: 1499860
This is a difference between String.Substring(int)
in .NET, and String.substring(int, int)
in Java.
In .NET, the second parameter is the length of the substring you're trying to take.
In Java, the second parameter is the exclusive end index of the substring you're trying to take.
For example:
// .NET
"Hello world".Substring(3, 4) => "lo w"
// Java
"Hello world".substring(3, 4) => "w"
You're trying to take a substring which ends 1 character before the end of the string, so you want
String sub = s.substring(1, s.length() - 1);
Lessons to take from this:
String.length()
are virtually zero.Upvotes: 15
Reputation: 3596
Reading the Java documentation on substring, I noticed this line:
The substring begins at the specified beginIndex and extends to the character at index endIndex - 1
So to get your desired result, you'll want to change it to
String sub = s.substring(1, s.length() - 1);
Upvotes: 2
Reputation: 3509
length method in String returns the length of the string. Here you want to get the substring after remove first char and last char. Thus
String sub = s.substring(1,s.length()-2);
should be:
String sub = s.substring(1,s.length()-1);
Upvotes: 5