Results 1 to 8 of 8
  1. #1
    michcio is offline Member
    Join Date
    Nov 2007
    Posts
    22
    Rep Power
    0

    Smile help me with a realy easy program (substring)

    hi!
    I have been trying to write a program, it should first read in a word with JOptionPane.showInput... and the if the word was the same if you read it from the beginning or behind forexample lol, mum and so on... I have written a code and it seems fine to me but when I run it in Netbeans it shows an error that the string index is out of range please help:confused: :confused: !!
    Michcio

    public static void main(String [] arg){
    String a = JOptionPane.showInputDialog("Skriv in ett ord!");
    int b = 0;
    int c = a.length();
    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);
    }
    }
    M.
    Last edited by michcio; 01-25-2008 at 05:07 PM.

  2. #2
    roots's Avatar
    roots is offline Moderator
    Join Date
    Jan 2008
    Location
    Dallas
    Posts
    293
    Rep Power
    7

    Default

    michcio,
    Mistake is on int c = a.length();

    change it to int c = a.length()-1;

    Please refer to javadoc of substring method of String for greater details, In summary it says "@param endIndex the ending index, exclusive."

    Just a tip, When you need only one character from string you can use charAt() method of String as well ..

    Hope it helps,and dont forget to lookup the String's javadoc.
    dont worry newbie, we got you covered.

  3. #3
    michcio is offline Member
    Join Date
    Nov 2007
    Posts
    22
    Rep Power
    0

    Default

    Quote Originally Posted by roots View Post
    change it to int c = a.length()-1;
    hum... it's still not working ... same error! :S
    Last edited by michcio; 01-25-2008 at 08:36 PM.

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    As root says, have you try charAt(). Except what root says, I can't see any error. Sorry I don't have Java installation in this machine I use now. :(

  5. #5
    CaptainMorgan's Avatar
    CaptainMorgan is offline Moderator
    Join Date
    Dec 2007
    Location
    NewEngland, US
    Posts
    835
    Rep Power
    8

    Default

    Quote Originally Posted by michcio View Post
    hum... it's still not working ... same error! :S
    Are you absolutely sure? I tested your code before and after roots said to use ...length() - 1; and found it to work after his suggestion. Please post your updated code with roots' suggestion. And, I also roll with Netbeans. Thank you.
    Vote for the new slogan to our beloved Java Forums! (closes on September 4, 2008)
    Want to voice your opinion on your IDE/Editor of choice? Vote now!
    Got a little Capt'n in you? (drink responsibly)

  6. #6
    michcio is offline Member
    Join Date
    Nov 2007
    Posts
    22
    Rep Power
    0

    Default

    here is my code ones again (without any errors now):

    Java Code:
        [COLOR="Blue"]public static void[/COLOR] main(String [] arg){
            String a = JOptionPane.showInputDialog([COLOR="Magenta"][I]"Skriv in ett ord!"[/I][/COLOR]);
            [COLOR="Blue"]i[/COLOR][COLOR="Blue"]nt[/COLOR] b = 0;
            [COLOR="Blue"]int[/COLOR] c = a.length()[COLOR="Red"]-1[/COLOR];     //root's sugeston works perfectly!!
            String ord1 = "";
            String ord2 = "";
            [COLOR="Blue"]for[/COLOR]([COLOR="Blue"]int[/COLOR] d=0; [COLOR="Red"]c<b[/COLOR]; d++){
                ord1 = ord1 + a.substring(b,(b+1));
                ord2 = ord2 + a.substring(c,(c+1));
                c--;          
                b++;           
            }
            [COLOR="Blue"]if[/COLOR](ord1.equals(ord2))
                JOptionPane.showMessageDialog
                        ([COLOR="Blue"]null[/COLOR], [COLOR="Magenta"][I]"Det inskrivna ordet &#228;r ett palindrom!!!"[/I][/COLOR]);
            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

  7. #7
    CaptainMorgan's Avatar
    CaptainMorgan is offline Moderator
    Join Date
    Dec 2007
    Location
    NewEngland, US
    Posts
    835
    Rep Power
    8

    Default

    Your initial problem was solved. If a subsequent logic error is in play, then that's typically up to you as only you can understand how the program is intended to work. You used the word "mum", and this is what I used to test it. Whether or not it works with c < b is only important to you - especially if you don't tell us ahead of time. What I mean more specifically:
    I have been trying to write a program, it should first read in a word with JOptionPane.showInput... and the if the word was the same if you read it from the beginning or behind forexample lol, mum and so on... I have written a code and it seems fine to me but when I run it in Netbeans it shows an error that the string index is out of range please help !!
    And
    hum... it's still not working ... same error! :S
    How are we supposed to read your mind and know there's a logic error based on what you have said up to that point? In the future, it would behoove you to be as specific as possible with your initial post and description of your problem. Saying "it doesn't work" does not help you or us, especially when it compiles without error! Instead of five posts added to this thread to assist you, it would've been only one to solve your real problem. Please review the FAQ for more on this.
    Vote for the new slogan to our beloved Java Forums! (closes on September 4, 2008)
    Want to voice your opinion on your IDE/Editor of choice? Vote now!
    Got a little Capt'n in you? (drink responsibly)

  8. #8
    michcio is offline Member
    Join Date
    Nov 2007
    Posts
    22
    Rep Power
    0

Similar Threads

  1. String substring function
    By ravian in forum New To Java
    Replies: 6
    Last Post: 01-02-2008, 07:35 PM
  2. substring
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-11-2007, 08:15 PM
  3. Replies: 0
    Last Post: 10-04-2007, 09:33 PM
  4. Easy question
    By JavaNoob in forum New To Java
    Replies: 10
    Last Post: 08-03-2007, 10:28 PM
  5. easy way to study the java springs concept
    By kumar84 in forum New To Java
    Replies: 1
    Last Post: 07-17-2007, 03:53 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •