Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java string index out of range: 0 [closed]

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
like image 407
user2704743 Avatar asked Aug 21 '13 18:08

user2704743


People also ask

How do you fix a string index out of bound exception in Java?

The StringIndexOutOfBoundsException is an exception in Java, and therefore can be handled using try-catch blocks using the following steps: Surround the statements that can throw an StringIndexOutOfBoundsException in try-catch blocks. Catch the StringIndexOutOfBoundsException.

Why is my string index out of range?

The string index out of range indicates that the index you are attempting to reach does not exist. Within a string, that implies you are working on getting a character from that string through a presented point.

What is StringIndexOutOfBoundsException?

java.lang.StringIndexOutOfBoundsException. Thrown by String methods to indicate that an index is either negative or greater than the size of the string. For some methods such as the charAt method, this exception also is thrown when the index is equal to the size of the string.

Can I index a string in Java?

You can get the character at a particular index within a string by invoking the charAt() accessor method. The index of the first character is 0, while the index of the last character is length()-1 . For example, the following code gets the character at index 9 in a string: String anotherPalindrome = "Niagara.


2 Answers

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.

like image 103
Andrew Martin Avatar answered Nov 16 '22 23:11

Andrew Martin


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.

like image 36
Marc-Andre Avatar answered Nov 17 '22 01:11

Marc-Andre