aspose file tools
The moose likes Beginning Java and the fly likes  String index out of range: 1 Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Reply 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: 4734
    
    7

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?
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: 32611
    
    4
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: 449
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: 32611
    
    4
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: 32611
    
    4
You’re welcome
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: String index out of range: 1
 
Similar Threads
How do I get one program to take information out of another one.
Switch error
If/Else/Switch Statements
Null pointer Exception error
Just Can't figure it out and it's due today