英文:
Java Scanner Reading Leading Space
问题
我试图创建一个菜单,接受一个字符输入用于开关案例,并循环直到输入为"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)
这意味着它正在获取一个空输入,对吗?所以我添加了.trim()
但我仍然得到了错误,它甚至不等待输入,我只是得到了错误。
抱歉,如果以前有人回答过这个问题,但我找不到任何答案。我还尝试添加了keyboard.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
,但也不起作用。
程序错误日志的输入:
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)
代码:
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');
}
}
请注意,我已经移除了HTML编码中的"
并将toLowerCase()
应用到ch
的输入上。
英文:
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');
}
}
答案1
得分: 2
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');
}
}
英文:
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!
答案2
得分: 1
以下是已更新的代码,请检查:
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
char ch;
do
{
System.out.println("输入选项 (a,b,c,d,e,f,q):");
ch = keyboard.nextLine().toLowerCase().charAt(0);
switch (ch)
{
// a、b、c、d、e 和 f 的 case 语句
}
} while (ch != 'q');
}
英文:
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');
}
答案3
得分: 1
主要问题在于扫描器在你按下"Enter"键后立即与输入一起工作。
因此,如果你的输入是输入'\u0020'
(空格),甚至在第19行多次输入,你会对空行进行修剪,根据javadoc中的charAt
。
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论