The Artima Developer Community
Sponsored Link

Java Answers Forum
error:stringindexoutofbounds, please help

3 replies on 1 page. Most recent reply: Feb 27, 2006 2:35 AM by Matthias Neumair

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 3 replies on 1 page
Michelle Loh

Posts: 10
Nickname: modernage
Registered: Feb, 2006

error:stringindexoutofbounds, please help Posted: Feb 24, 2006 11:04 PM
Reply to this message Reply
Advertisement
hello, i wrote a program that changes all occurrences of a letter in a sentence, into a different letter. for instance, "The pink pig flew." if the user wanted to change the 'p' into 't', the output would be "The tink tig flew." But everytime i run it, it tells me StringIndexOutOfBoundsException: String index out of range 7. I don't know what i did wrong, please help!

here is my code:

public class change
{
public static void main(String[] args)
{
String first,rest,phrase,chara,newchar;
int count;
System.out.println("What is the phrase?");
phrase=IO.readString();
System.out.println("What is the letter you want to replace?");
chara=IO.readString();
System.out.println("What is the letter you want in place?");
newchar=IO.readString();

count=-1;
int length=(phrase.length());
length=(length-1);
while (count!=length)
{
count=(count+1);
first=(phrase.substring((count),(count+1)));
rest=(phrase.substring(count+1));
if (first.equals(chara))
{
phrase=(first+newchar+rest);
}
else
{
phrase=(first+cha ra+rest);
}
}
IO.printString(phrase);
}
}


Dave Hinton

Posts: 42
Nickname: catbells
Registered: Oct, 2003

Re: error:stringindexoutofbounds, please help Posted: Feb 26, 2006 12:11 PM
Reply to this message Reply
You would be able to solve this for yourself if you stepped through it with a debugger.

If you do not have access to a debugger, try printing out the phrase each time through the loop, while you are looking for the bug.

I am not going to do your homework for you.

Michelle Loh

Posts: 10
Nickname: modernage
Registered: Feb, 2006

Re: error:stringindexoutofbounds, please help Posted: Feb 26, 2006 3:29 PM
Reply to this message Reply
this is not homework, I am practicing writing programs for my exam, and the whole program is done anyway so it's not like I didn't do anything, geez.

Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: error:stringindexoutofbounds, please help Posted: Feb 27, 2006 2:35 AM
Reply to this message Reply
Homework or not.

Even if someone would do the work for you, he had to use a debugger.

In your error message you didn't even write where the error occured.

You didn't format your code, too.
There ARE java tags you can (and must) use on this page or no one would read the code.

Now try to thikn what happens when [count] reaches [length], after you incremwent count.
count+1 is ...

Flat View: This topic has 3 replies on 1 page
Topic: servlet help Previous Topic   Next Topic Topic: Java Threads

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use