View Single Post
  #6 (permalink)  
Old 01-26-2008, 02:53 PM
michcio michcio is offline
Member
 
Join Date: Nov 2007
Posts: 21
michcio has a little shameless behaviour in the past
here is my code ones again (without any errors now):

Code:
public static void main(String [] arg){ String a = JOptionPane.showInputDialog("Skriv in ett ord!"); int b = 0; int c = a.length()-1; //root's sugeston works perfectly!! String ord1 = ""; String ord2 = ""; for(int d=0; c<b; d++){ ord1 = ord1 + a.substring(b,(b+1)); ord2 = ord2 + a.substring(c,(c+1)); c--; b++; } if(ord1.equals(ord2)) JOptionPane.showMessageDialog (null, "Det inskrivna ordet är ett palindrom!!!"); System.exit(0); }
Quote:
Originally Posted by CaptainMorgan View Post
Are you absolutely sure?
yeah I am absloutly sure...but I have found the error myself now... The length()-1 was helpfull I didn't knew that if you had a word for ex. "hello" then the length of it was 5 and not 4. But there was one more error in this program. In the "for" loop I have written !(b==c) (have now been change in the program see the red boolean) and it means that when b ins't equals to c the "for" loop runs. This works but only if the word have odd numbers of letter in it for example it works fine when I write the word "hello" because first b=0 and c=length()-1 gives c=4 ...the for loop runs and c gets 3 and b = 1 again ...c become 2 and b = 2 ... the loop ends (b==c!!!!!) but if I write a word with even numbers ofletter it doesn't work. For example "ha" b=0 and c=1...the for loop runs ones and b become 1 and c 0... and so on ... (c never become equals to b!!!) String gets out of expression !!! ... Therefor I should instead chande !(b==c) to c<b (red in the program) so the program works both for odd and even numbers of letters in a word...
So my problem is now sulved... and tanks a lot for your help

Michcio
Reply With Quote