Java Scanner 读取前导空格

huangapple go评论79阅读模式
英文:

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编码中的&quot;并将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 &quot;main&quot; 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(&quot;Enter Option (a,b,c,d,e,f,q):&quot;);
ch = keyboard.nextLine().toLowerCase().charAt(0);
switch (ch)
{
case &#39;c&#39;:
System.out.println(&quot;Enter 2 Numbers&quot;);
System.out.println(&quot;First Number: &quot;);
m = keyboard.nextInt();
System.out.println(&quot;Second Number: &quot;);
n = keyboard.nextInt(); 
for(int i = 1; i &lt;= n - m + 1; i++)
{
if (i % 5 == 0)
{
System.out.print(i);
if (i != n - m + 1)
{
System.out.println(&quot;, &quot;);
}
}else
{
System.out.print(i);
if (i != n - m + 1)
{
System.out.print(&quot;, &quot;);
}
}
}
System.out.println(&quot;&quot;);
break;
}
}while (ch != &#39;q&#39;);
}
} 

答案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(&quot;Enter Option (a,b,c,d,e,f,q):&quot;);
ch = keyboard.next().charAt(0);
switch (ch)
{
case &#39;c&#39;:
System.out.println(&quot;Enter 2 Numbers&quot;);
System.out.println(&quot;First Number: &quot;);
m = keyboard.nextInt();
System.out.println(&quot;Second Number: &quot;);
n = keyboard.nextInt(); 
for(int i = 1; i &lt;= n - m + 1; i++)
{
if (i % 5 == 0)
{
System.out.print(i);
if (i != n - m + 1)
{
System.out.println(&quot;, &quot;);
}
}else
{
System.out.print(i);
if (i != n - m + 1)
{
System.out.print(&quot;, &quot;);
}
}
}
System.out.println(&quot;&quot;);
break;
}
}while (ch != &#39;q&#39;);
}
} 

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(&quot;Enter Option (a,b,c,d,e,f,q):&quot;);
ch = keyboard.nextLine().toLowerCase().charAt(0);   
switch (ch)
{
//case statements for a,b,c,d,e and f
}
}while (ch != &#39;q&#39;);
}

答案3

得分: 1

主要问题在于扫描器在你按下"Enter"键后立即与输入一起工作。
因此,如果你的输入是输入'\u0020'(空格),甚至在第19行多次输入,你会对空行进行修剪,根据javadoc中的charAt

英文:

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

Java Scanner 读取前导空格

huangapple
  • 本文由 发表于 2020年8月10日 16:15:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/63336479.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定