[英]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:我正在尝试创建一个菜单,该菜单为 switch case 输入字符并循环直到输入为 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)线程“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.
所以我添加了 .trim() 但我仍然得到错误,它甚至不等待输入我只是得到错误。
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\
\
\
])?");
我也试过添加 keyboard.skip("(\\r\\n|[\\n\\r\
\
\
])?"); 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');
}
}
Try the line ch = keyboard.next().charAt(0);试试行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!快乐编码!
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');
}
Main thing here that scanner works with input right just after you hit "enter".这里的主要内容是扫描仪在您点击“输入”后立即处理输入。 So if your input would be entering
'\ '
(whitespaces) even several times on line 19 you're doing trim to empty line and according to javadoc charAt
.因此,如果您的输入将在第 19 行多次输入
'\ '
(空格),您将根据 javadoc charAt
将其修剪为空行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.