The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

Re: String index is out of range

Heres what I'm trying to do:

System.out.print("Enter a last name: ");
name1 = stdIn.nextLine();

The program outputs to the user asking for a last name then the user input is stored in the string name1 using scanner class.
Then the line: int namelength = name1.length();. Is being used to get and store the length of the string name1 in the interger namelength.
That is where the exception occurs and it says that the "String index is out of range."

Re: String index is out of range

Actually your error is occurring at the line

char x = name1.charAt(namelength);

You are probably incrementing namelength or decreasing the size of name1 somewhere, but not checking your location in the String or resetting the size (respectfully). We'll need to see more of that while loop to provide you with any useful tips.

EDIT:
namelength is the length of your String, but the index starts counting at 0 and ends at the length of the String minus 1. I assume you are trying to get the last character of name1. To do that, you need to say: namelength-1 instead of namelength.

Last edited by aussiemcgr; April 2nd, 2012 at 05:20 PM.

NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

When asking for help, please follow these guidelines to receive better and more prompt help:
1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
2. Give full details of errors and provide us with as much information about the situation as possible.
3. Give us an example of what the output should look like when done correctly.

Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

The Following User Says Thank You to aussiemcgr For This Useful Post:

Re: String index is out of range

Thanks for the help aussie, I was looking in the wrong place.
The while loop is a verification loop designed to ensure the name entered only contains letters or whitespace(But now two in a row, which I haven't worked out).

Re: String index is out of range

Ok, you've made a very simple mistake that is very often seen in those who are new to programming. Your mistake has to do with the difference between how a human counts and how the computer counts. When humans count, we start at 1, but when computers count, they start at 0.

So, let's say we have the word: Programming. That word has 11 characters, so its length is 11. But, if we want to get the first character, word.charAt(1); will give us r, NOT P. The reason is because indexing starts at 0 and ends at the length of the word, minus one. For example, here is the indexes of the word:
P = 0
r = 1
o = 2
g = 3
r = 4
a = 5
m = 6
m = 7
i = 8
n = 9
g = 10

So, as you can see, to get the first letter, we say: word.charAt(0);, and to get the last letter we say: word.charAt(10);. Or, to get the last letter of any word, we say: word.charAt(word.length()-1);.

Does that make sense?

NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

When asking for help, please follow these guidelines to receive better and more prompt help:
1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
2. Give full details of errors and provide us with as much information about the situation as possible.
3. Give us an example of what the output should look like when done correctly.

Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

Re: String index is out of range

char x = name1.charAt(namelength - 1);
fixed the exception by adding the -1, but now the if statements that follow don't work either.

What does it do instead? Does it throw an Exception? Does it exhibit some strange behavior? Does it sprout wings and fly away? Something else? Telling us that "it don't work" is as useful to us as us saying "then fix it" is useful to you.

Re: String index is out of range

since the index 0 is a valid index.
Also, check the last if statement in your loop. I think you misspelled namelength.

NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

When asking for help, please follow these guidelines to receive better and more prompt help:
1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
2. Give full details of errors and provide us with as much information about the situation as possible.
3. Give us an example of what the output should look like when done correctly.

Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

Re: String index is out of range

Sorry about the short and not to the point replies, I understand that it was pointless and didn't help me or yourself. On the other hand I did end of fixing the overall program by switching from the process I was using and just going with a pattern and matcher setup which ran a lot easier and more compact than trying to use multiple loops to verify each letter in the user inputs.