BlaM
BlaM

Reputation: 43

Java Scanner Reading Leading Space

I am trying to have a menu that takes a character input for a switch case and loops till the input is q but after the loop run once i get this error:

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0 at java.lang.String.charAt(Unknown Source) at Menu.main(Menu.java:19)

Which means its getting a null input right? So i added the .trim() but i still get the error, it doesn't even wait for an input i just get the error.

Sorry if this has been answered before but i can't find it anywhere. I've also tried adding keyboard.skip("(\r\n|[\n\r\u2028\u2029\u0085])?"); which doesn't work as well.

Input given for the program with error log:

Enter Option (a,b,c,d,e,f,q):

c

Enter 2 Numbers

First Number:

1

Second Number:

10

1, 2, 3, 4, 5,
6, 7, 8, 9, 10

Enter Option (a,b,c,d,e,f,q):

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0
        at java.lang.String.charAt(Unknown Source)
        at Menu.main(Menu.java:19)

CODE:

import java.io.*;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

    Scanner keyboard = new Scanner (System.in);
    char ch; int m,n; String y;
    do
    {
        System.out.println("Enter Option (a,b,c,d,e,f,q):");
        ch = keyboard.nextLine().toLowerCase().charAt(0);
        switch (ch)
        {
        case 'c':
            
            System.out.println("Enter 2 Numbers");
            System.out.println("First Number: ");
            m = keyboard.nextInt();
            System.out.println("Second Number: ");
            n = keyboard.nextInt(); 
            for(int i = 1; i <= n - m + 1; i++)
            {
                if (i % 5 == 0)
                {
                    System.out.print(i);
                    if (i != n - m + 1)
                    {
                    System.out.println(", ");
                    }
                }else
                {
                    System.out.print(i);
                    if (i != n - m + 1)
                    {
                    System.out.print(", ");
                    }
                }
                
            }
            System.out.println("");
            break;
        }
    }while (ch != 'q');

}
 
} 

Upvotes: 0

Views: 107

Answers (3)

MdBasha
MdBasha

Reputation: 451

Try the line ch = keyboard.next().charAt(0); switch(ch)

import java.io.*;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

    Scanner keyboard = new Scanner (System.in);
    char ch; int m,n; String y;
    do
    {
        System.out.println("Enter Option (a,b,c,d,e,f,q):");
        ch = keyboard.next().charAt(0);

        switch (ch)
        {
        case 'c':
            
            System.out.println("Enter 2 Numbers");
            System.out.println("First Number: ");
            m = keyboard.nextInt();
            System.out.println("Second Number: ");
            n = keyboard.nextInt(); 
            for(int i = 1; i <= n - m + 1; i++)
            {
                if (i % 5 == 0)
                {
                    System.out.print(i);
                    if (i != n - m + 1)
                    {
                    System.out.println(", ");
                    }
                }else
                {
                    System.out.print(i);
                    if (i != n - m + 1)
                    {
                    System.out.print(", ");
                    }
                }
                
            }
            System.out.println("");
            break;
        }
    }while (ch != 'q');

}
 
} 

Happy coding!

Upvotes: 2

Satish Hawalppagol
Satish Hawalppagol

Reputation: 184

Here's the updated code Please check:

public static void main(String[] args)
{
    

    Scanner keyboard = new Scanner (System.in);
    char ch;
    do
    {
        System.out.println("Enter Option (a,b,c,d,e,f,q):");
        ch = keyboard.nextLine().toLowerCase().charAt(0);   
        switch (ch)
        {
        //case statements for a,b,c,d,e and f
        }
    }while (ch != 'q');

}

Upvotes: 1

Alex
Alex

Reputation: 163

Main thing here that scanner works with input right just after you hit "enter". So if your input would be entering '\u0020'(whitespaces) even several times on line 19 you're doing trim to empty line and according to javadoc charAt.

enter image description here

Upvotes: 1

Related Questions