|
|
Welcome to the Java Forums.
You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:
- have access to post topics
- communicate privately with other members (PM)
- not see advertisements between posts
- have the possibility to earn one of our surprises if you are an active member
- access many other special features that will be introduced later.
Registration is fast, simple and absolutely free so please, join our community today!
If you have any problems with the registration process or your account login, please contact us.
|
|

01-25-2008, 06:51 PM
|
|
Member
|
|
Join Date: Nov 2007
Posts: 21
|
|
|
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  !!
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 07:07 PM.
|
|

01-25-2008, 08:06 PM
|
 |
Moderator
|
|
Join Date: Jan 2008
Location: Dallas
Posts: 263
|
|
|
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.
|
|

01-25-2008, 10:34 PM
|
|
Member
|
|
Join Date: Nov 2007
Posts: 21
|
|
Originally Posted by roots
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 10:36 PM.
|
|

01-26-2008, 08:01 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,412
|
|
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. 
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

01-26-2008, 08:42 AM
|
 |
Moderator
|
|
Join Date: Dec 2007
Location: NewEngland, US
Posts: 841
|
|
Originally Posted by michcio
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.
__________________
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. to our beloved Java Forums! (closes on September 4, 2008)
Want to voice your opinion on your IDE/Editor of choice? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. !
Got a little Capt'n in you? (drink responsibly)
|
|

01-26-2008, 02:53 PM
|
|
Member
|
|
Join Date: Nov 2007
Posts: 21
|
|
here is my code ones again (without any errors now):
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);
}
Originally Posted by CaptainMorgan
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
|
|

01-27-2008, 02:30 AM
|
 |
Moderator
|
|
Join Date: Dec 2007
Location: NewEngland, US
Posts: 841
|
|
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.
__________________
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. to our beloved Java Forums! (closes on September 4, 2008)
Want to voice your opinion on your IDE/Editor of choice? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. !
Got a little Capt'n in you? (drink responsibly)
|
|

01-27-2008, 02:41 AM
|
|
Member
|
|
Join Date: Nov 2007
Posts: 21
|
|
|
well yeah sry I will be more specific in the future then
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|