Reputation: 11
I'm new to Java and I can't figure out how to solve this problem: Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 1
Here's the entire code:
public static void main(String[] args) {
// Create a Scanner
Scanner input = new Scanner(System.in);
//Step 1: Ask user to enter first and last name
System.out.println("\nPlease enter your first and last name: ");
String name = input.nextLine();
String major = "";
String classification = "";
//Step 2: Ask user to enter two characters
System.out.println("\nPlease enter two characters (1st character represents the major and 2nd character represents the classification): ");
char ch = input.next().charAt(0);
char ch1 = input.nextLine().charAt(1);
//Step 3: Print statement
switch(ch) {
case 'i': major = "Information Technology"; break;
case 'c': major = "Computer Science"; break;
case 'm': major = "Mathematics"; break;
case 'p': major = "Physics"; break;
case 'b': major = "Biology"; break;
case 'e': major = "Engineering"; break;
case 'h': major = "History"; break;
case 'j': major = "Journalism"; break;
case 'a': major = "Art and Design"; break;
case 'l': major = "Literature"; break;
case 's': major = "Sport Medicine"; break;
default: System.out.println("\nInvalid Major Code");
System.out.println("Please enter a character followed by an integer!");
break;
}//end of switch
//Step 3: Print statement
switch(ch1) {
case '1': classification = "Freshman"; break;
case '2': classification = "Sophmore"; break;
case '3': classification = "Junior"; break;
case '4': classification = "Senior"; break;
case '5': classification = "Graduate"; break;
default: System.out.println("\nInvalid Classification Code");
System.out.println("Please enter a character followed by an integer!");
break;
}//end of switch
System.out.println("\nMajor and classification is: " + major + "" + classification);
System.out.println("\nThank You!");
}//end of main
Any help would be greatly appreciated.
Upvotes: 0
Views: 1266
Reputation: 2576
Calling
final char ch1 = input.nextLine().charAt(1);
expects a whole line to be read, and there getting the second letter. But
char ch = input.next().charAt(0);
already consumed the first char.
So you should read the whole input in one go, then (do more checking and then) get your chars.
//Step 2: Ask user to enter two characters
System.out.println("\nPlease enter two characters (1st character represents the major and 2nd character represents the classification): ");
final String reply = input.nextLine();
final char ch = reply.charAt(0);
final char ch1 = reply.charAt(1);
//Step 3: Print statement
Upvotes: 1
Reputation: 78
change your code from char ch1 = input.nextLine().charAt(1);
to this char ch1 = input.next().charAt(0);
you can check this for more details about differences between next() and nextLine()
Upvotes: 1
Reputation: 44398
If you enter the second entered character, it reads out of range because of charAt(1)
. Just change it to charAt(0)
, because you alwayws read only the first character of the input:
char ch = input.next().charAt(0);
char ch1 = input.next().charAt(0);
It works for both inputs:
i
3
And also i 3
Upvotes: 1
Reputation: 18748
The problem is that char ch = input.next().charAt(0);
will read both the characters, but not "consume" the newline character that is generated when the user hits Enter.
You then call input.nextLine()
, which will consume the newline character (but now the two characters were already consumed by next()
, so the resulting string is empty). Calling .charAt(1)
on the empty string generates the exception because there is no position (1)
in the empty string (it's length is 0).
Instead I'd suggest you use something like this:
//Step 2: Ask user to enter two characters
System.out.println("\nPlease enter two characters (1st character represents the major and 2nd character represents the classification): ");
String majorAndClassification = "";
while( majorAndClassification.length() != 2 )
majorAndClassification = input.nextLine();
char ch = majorAndClassification.charAt(0);
char ch1 = majorAndClassification.charAt(1);
This will make sure that the user enters two characters - if he doesn't, he'll have to try again until he does. A slightly better option would of course be to print out the prompt every time, like so:
//Step 2: Ask user to enter two characters
String majorAndClassification = "";
while( majorAndClassification.length() != 2 ){
System.out.println("\nPlease enter two characters (1st character represents the major and 2nd character represents the classification): ");
majorAndClassification = input.nextLine();
}
Upvotes: 1
Reputation: 1774
Try to change your code so that it first checks for the length of the input before it refer to the index of the String. Something like the following:
//Step 2: Ask user to enter two characters
System.out.println("\nPlease enter two characters (1st character represents the major and 2nd character represents the classification): ");
String inputNext = input.next();
String inputNextLine;
if(inputNext.length>0 && inputNextLine.length>0){
char ch = inputNext;
char ch1 = inputNextLine;
//Step 3: Print statement
switch(ch)
{
case 'i': major = "Information Technology";
break;
case 'c': major = "Computer Science";
break;
case 'm': major = "Mathematics";
break;
case 'p': major = "Physics";
break;
case 'b': major = "Biology";
break;
case 'e': major = "Engineering";
break;
case 'h': major = "History";
break;
case 'j': major = "Journalism";
break;
case 'a': major = "Art and Design";
break;
case 'l': major = "Literature";
break;
case 's': major = "Sport Medicine";
break;
default: System.out.println("\nInvalid Major Code");
System.out.println("Please enter a character followed by an integer!");
break;
}//end of switch
//Step 3: Print statement
switch(ch1)
{
case '1': classification = "Freshman";
break;
case '2': classification = "Sophmore";
break;
case '3': classification = "Junior";
break;
case '4': classification = "Senior";
break;
case '5': classification = "Graduate";
break;
}
}
Upvotes: 1