Java string index out of range: 0

32,639

Solution 1

Your problem is in this line:

userInput.nextLine().charAt(0);

The nextLine() method scans everything on the current line and then advances the pointer past that line. So when you call the charAt() method, you are calling it on the next line, which is blank space, and thus an error is occuring.

Instead, change this line to:

userInput.next().charAt(0)

Note, this means other parts of your code will need changed too.

Edit:

Was about to edit my solution, but @Marc-Andre added his answer which covers it, so just cast your eyes over it too.

Solution 2

The problem when you're doing age = userInput.nextInt(); is that you've probably enter a number say 4 and then press Enter.

So the scanner read 4 when you're calling nextInt but the new line is not consume. That means that when you do : userInput.nextLine().charAt(0); you're consuming the new line, so the the nextLine() will return an empty String. Since you're doing chartAt on an empty String, it give you an Exception.

You could do:

age = userInput.nextInt();
userInput.nextLine();

This will consume the new line, so the stream should be empty. So you won't have the exception and you can ask for the next input.

Share:
32,639
user2704743
Author by

user2704743

Updated on August 22, 2020

Comments

  • user2704743
    user2704743 over 3 years

    I have this problem where as soon as I enter my first input the program crashes and I get

    String index out of range: 0

    I've looked elsewhere and tried to find my mistakes but I found different problems which aren't what I had. Could someone please tell me where have I gone wrong?.

    Thanks for your help, here is the code:

    import java.util.Scanner;
    
    public class Assignment1Q2 {
    
        public static void main(String[] args) {
    
            System.out.println("Thank you for your call,\nPlease take some time to answer a few questions");
            collectData();
    
        }//end of main
    
        public static void collectData() {
    
            Scanner userInput = new Scanner(System.in);
    
            int age;
            char gender;
            char show;
            int over30MY = 0, over30FY = 0, under30MY = 0, under30FY = 0;
            int over30MN = 0, over30FN = 0, under30MN = 0, under30FN = 0;
    
            System.out.println("\nWhat is your age?\n");
            age = userInput.nextInt();
    
            System.out.println("Male or Female (Enter M or Y)");
            gender = userInput.nextLine().charAt(0);
            gender = Character.toLowerCase(gender);
    
            System.out.println("Do you watch the show regularly? (Enter Y or N)");
            show = userInput.nextLine().charAt(0);
            show = Character.toLowerCase(show);
    
            if((age > 30) && (gender == 'm') && (show == 'y')) {       
                over30MY++;             
            }
            else if((age > 30) && (gender == 'f') && (show == 'y')) {
                over30FY++;
            }
            else if((age < 30) && (gender == 'm') && (show == 'y')) {
                under30MY++;
            }
            else if((age < 30) && (gender == 'f') && (show == 'y')) {
                under30FY++;
            }
            else if((age > 30) && (gender == 'm') && (show == 'n')) {
                over30MN++;
            }
            else if((age > 30) && (gender == 'f') && (show == 'n')) {
                over30FN++;
            }
            else if((age < 30) && (gender == 'm') && (show == 'n')) {
                under30MN++;
            }
            else if((age < 30) && (gender == 'f') && (show == 'n')) {
                under30FN++;
            }//end of if else
    
        }//end of collectData
    }// end of class
    
  • user2704743
    user2704743 over 10 years
    Thank you! this worked I'll be sure to keep this in mind next time
  • Fritz
    Fritz over 10 years
    @user2704743 Nothings says "thank you" better than accepting an answer (when the timer allows it).
  • Andrew Martin
    Andrew Martin over 10 years
    @Gamb: Was just about to say that myself! Thanks
  • user2704743
    user2704743 over 10 years
    still waiting on the timer unfortunately
  • user2704743
    user2704743 over 10 years
    thanks for clarifying that I understand what I had done wrong now :)