Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





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.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 01-25-2008, 06:51 PM
Member
 
Join Date: Nov 2007
Posts: 21
michcio has a little shameless behaviour in the past
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

Quote:
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.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 01-25-2008, 08:06 PM
roots's Avatar
Moderator
 
Join Date: Jan 2008
Location: Dallas
Posts: 263
roots is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 01-25-2008, 10:34 PM
Member
 
Join Date: Nov 2007
Posts: 21
michcio has a little shameless behaviour in the past
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 10:36 PM.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 01-26-2008, 08:01 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,412
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
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.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 01-26-2008, 08:42 AM
CaptainMorgan's Avatar
Moderator
 
Join Date: Dec 2007
Location: NewEngland, US
Posts: 841
CaptainMorgan will become famous soon enoughCaptainMorgan will become famous soon enough
Send a message via AIM to CaptainMorgan
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.
__________________

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)
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 01-26-2008, 02:53 PM
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
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 01-27-2008, 02:30 AM
CaptainMorgan's Avatar
Moderator
 
Join Date: Dec 2007
Location: NewEngland, US
Posts: 841
CaptainMorgan will become famous soon enoughCaptainMorgan will become famous soon enough
Send a message via AIM to CaptainMorgan
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:
Quote:
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
Quote:
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)
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 01-27-2008, 02:41 AM
Member
 
Join Date: Nov 2007
Posts: 21
michcio has a little shameless behaviour in the past
well yeah sry I will be more specific in the future then
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
String substring function ravian New To Java 6 01-02-2008 09:35 PM
substring Java Tip Java Tips 0 11-11-2007 10:15 PM
How to execute an External Program through Java program JavaBean Java Tips 0 10-04-2007 11:33 PM
Easy question JavaNoob New To Java 10 08-04-2007 12:28 AM
easy way to study the java springs concept kumar84 New To Java 1 07-17-2007 05:53 PM


All times are GMT +3. The time now is 01:22 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org