Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String index out of range: 1

 
ed spithaler
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I enter the 2 char movieCode the program works fine, but when I enter only 1 I get String index out of range: 1. Is there a way to code it to accept 1 or 2 chars? Thanks.



 
Winston Gutkowski
Bartender
Pie
Posts: 10417
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ed spithaler wrote:When I enter the 2 char movieCode the program works fine, but when I enter only 1 I get String index out of range: 1. Is there a way to code it to accept 1 or 2 chars?

Yes.

But the question you need to be asking yourself is: what is the index of the first character?

Suggestion: Look up the API documentation for String.charAt().

Winston
 
ed spithaler
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston,
The API shows:

Returns the char value at the specified index. An index ranges from 0 to length() - 1. The first char value of the sequence is at index 0, the next at index 1, and so on, as for array indexing.

I know that the first char is at 0 and the second char is at 1. When I enter only 1 char it is at 0, but the program is trying to extract at 0 and 1 indexes, and 1 is empty, which is probably why there is an error. OK, how do you fix it?

Ed
 
Campbell Ritchie
Sheriff
Pie
Posts: 48972
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is for you to decide. You need to decide whether you accept only two-character codes. In which case you would do well to test for them before acceptance, and maybe throw an IllegalArgumentException if the length is incorrect.
 
Saurabh Pillai
Ranch Hand
Posts: 524
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How many characters user can enter? Is there any limit on that? Your code does look for at least 2 characters.


or is it a typo?

You can validate user input for number of characters before you call computeCost function.
 
ed spithaler
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The movieCode has to be 1 or 2 chars.
 
Campbell Ritchie
Sheriff
Pie
Posts: 48972
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In that case you need something different from charAt(1).
You could try a Map<String, Something>.
You could try a switch-case with the different codes as the cases (Strings there only work in Java7).
You could try an if test on the length of the code. Inside an if (code.length() > 1) block, you can be sure charAt(1) will work.
 
ed spithaler
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for all of your options. I am sure I can find one to work.

Ed
 
Campbell Ritchie
Sheriff
Pie
Posts: 48972
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You’re welcome
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic