user2879101
user2879101

Reputation: 63

Creatng a simple text menu: String out of bonds expection ( range 0 ) Java

I'm trying to create a menu that prints to the screen a set of numbers, through the numbersystem method. When I try to loop my menu, I get the following error:

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0
    at java.lang.String.charAt(Unknown Source)
    at homework4_5.Driver.engageDriver(Driver.java:31)
    at homework4_5.Driver.engageDriver(Driver.java:47)
    at homework4_5.Driver.main(Driver.java:18)

I also believe that my method NumberSystem is not the issue. (As with using other input methods, I can get the method to produce proper results.)

I've run into this problem a lot with text menus. Can anyone help me? I think it may be that the scanner is receiving a blank string and can't parse a int from it.

import java.util.Scanner;

public class Driver {
    Scanner sc = new Scanner (System.in);

    public int baseCount = 0;
    public int numOfCols = 0;
    public boolean loop = true;


    public static void main(String[] args){
        Driver driver = new Driver();
        driver.engageDriver();          
    }


    public void engageDriver(){
        NumberSystem ns = new NumberSystem();
        System.out.println("Hello, please enter the numeric value corresponding to your counting/base system" );
        String temp = sc.nextLine();

        if(temp.charAt(0)== 'q'){
            System.out.println("Shutting Down the program...Thank You!");               
            loop = false;
            return;
        }
        baseCount = Integer.parseInt(temp);             

        System.out.println("Now enter how many columns you would like to display ( a value from 1 to 3 ) ");
        numOfCols=sc.nextInt();
        System.out.println("The number table you requested is:\n ");

        ns.printNumberSystem(baseCount, numOfCols);
        if(loop){
           engageDriver();
        }    
    }
}

Upvotes: 2

Views: 449

Answers (3)

Reimeus
Reimeus

Reputation: 159784

From the stacktrace its clear that temp is empty when charAt is invoked. Check that the String has content before attempting to invoke this method

if (!temp.isEmpty() && temp.charAt(0) == 'q') {

The main issue is that Scanner#nextInt does not consume newline characters. Therefore you will need to consume these characters to ensure that they are not passed back through the InputStream in the recursive call of engageDriver

numOfCols = sc.nextInt();
sc.nextLine();

Upvotes: 2

Dia
Dia

Reputation: 271

When you invoke nextInt() it doesn't change line. So next invocation of nextLine() reads the remainings of the line. In your case it's "" try to write something like 12 q and your program will quit. To fix that you need to force change reading line by invoking sc.nextLine() after sc.nextInt()

Upvotes: 0

Troubleshoot
Troubleshoot

Reputation: 1846

Use exceptions. If the user doesn't enter a string, then it will throw an exception. You can catch the exception.

try{
     if(temp.charAt(0)== 'q'){
            System.out.println("Shutting Down the program...Thank You!");

            loop = false;
            return;
     }
} catch(StringIndexOutOfBoundsException e) {
     //do whatever you want with it
     System.exit(-1);
}

Upvotes: 0

Related Questions