wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes  String index out of range: 1 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark " String index out of range: 1" Watch " String index out of range: 1" New topic
Author

String index out of range: 1

ed spithaler
Greenhorn

Joined: Feb 29, 2012
Posts: 9
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

Joined: Mar 17, 2011
Posts: 7696
    
  20

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


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
ed spithaler
Greenhorn

Joined: Feb 29, 2012
Posts: 9
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

Joined: Oct 13, 2005
Posts: 38467
    
  23
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

Joined: Sep 12, 2008
Posts: 507
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

Joined: Feb 29, 2012
Posts: 9
The movieCode has to be 1 or 2 chars.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38467
    
  23
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

Joined: Feb 29, 2012
Posts: 9
Thanks for all of your options. I am sure I can find one to work.

Ed
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38467
    
  23
You’re welcome
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: String index out of range: 1