Java 读取字符串 next() 和 nextLine()

发布于 2024-12-27 09:45:03 字数 1421 浏览 0 评论 0原文

问题是我无法使用 next() 读取变量输入,因为当我尝试拆分 (.split" ") 每个空格时,数组只获取我输入的前两个单词,所以我必须使用 Keyboard.nextLine() 和拆分过程按其应有的方式工作,我得到了数组中的所有单词,但问题是,如果我使用 nextLine() 那么我必须创建另一个键盘对象来读取第一个变量(答案),这是唯一的方法我可以让它工作这里是代码

    Scanner keyboard=new Scanner(System.in);
    Scanner keyboard2=new Scanner(System.in);//just to make answer word

    int answer=keyboard.nextInt();//if I don't use the keyboard2 here then the program will not work as it should work, but if I use next() instead of nextLine down there this will not be a problem but then the splitting part is a problem(this variable counts number of lines the program will have).

    int current=1;
    int left=0,right=0,forward=0,back=0;

    for(int count=0;count<answer;count++,current++)
    {
        String input=keyboard.nextLine();
        String array[]=input.split(" ");
        for (int counter=0;counter<array.length;counter++)
        {
            if (array[counter].equalsIgnoreCase("left"))
            {
                left++;
            }
            else if (array[counter].equalsIgnoreCase("right"))
            {     
                right++;
            }
            else if (array[counter].equalsIgnoreCase("forward"))
            {
                forward++;   
            }
            else if (array[counter].equalsIgnoreCase("back"))
            {     
                back++;
            }
        }

   }
}

谢谢:)

The problem is I cant read the variable input with next() cause when I try to split (.split" ") every whitespace then the array just get the first two words I type so I had to use keyboard.nextLine() and the splitting process works the way it should work and I get all the words in the array but the problem is that If I use nextLine() then I have to create another keyboard object to read the first variable (answer) and that is the only way I can make it work here is the code

    Scanner keyboard=new Scanner(System.in);
    Scanner keyboard2=new Scanner(System.in);//just to make answer word

    int answer=keyboard.nextInt();//if I don't use the keyboard2 here then the program will not work as it should work, but if I use next() instead of nextLine down there this will not be a problem but then the splitting part is a problem(this variable counts number of lines the program will have).

    int current=1;
    int left=0,right=0,forward=0,back=0;

    for(int count=0;count<answer;count++,current++)
    {
        String input=keyboard.nextLine();
        String array[]=input.split(" ");
        for (int counter=0;counter<array.length;counter++)
        {
            if (array[counter].equalsIgnoreCase("left"))
            {
                left++;
            }
            else if (array[counter].equalsIgnoreCase("right"))
            {     
                right++;
            }
            else if (array[counter].equalsIgnoreCase("forward"))
            {
                forward++;   
            }
            else if (array[counter].equalsIgnoreCase("back"))
            {     
                back++;
            }
        }

   }
}

Thanks :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

吾性傲以野 2025-01-03 09:45:03

keyboard.nextLine() 放在此行之后:

int answer=keyboard.nextInt();

这是一个常见问题,通常在 nextInt() 之后使用 nextLine() 方法时发生Scanner 类的方法。

实际发生的情况是,当用户在 int answer = Keyboard.nextInt(); 输入整数时,扫描仪将仅获取数字并保留换行符 \n.因此,您需要通过调用 keyboard.nextLine(); 来丢弃换行符,然后您可以调用 String input = Keyboard.nextLine(); > 没有任何问题。

Put keyboard.nextLine() after this line:

int answer=keyboard.nextInt();

This is a common problem that usually happens when you use nextLine() method after nextInt() method of Scanner class.

What actually happens is that when the user enters an integer at int answer = keyboard.nextInt();, the scanner will take the digits only and leave the new-line character \n. So you need to do a trick by calling keyboard.nextLine(); just to discard that new-line character and then you can call String input = keyboard.nextLine(); without any problem.

智商已欠费 2025-01-03 09:45:03

我的第一个建议是将整行(以空格分隔)添加到 String 数组中。
然后,利用 Integer.parseInt 方法将 answer 分配给数组的 0 索引。

这是您的代码,已重构。

Scanner keyboard = new Scanner(System.in);
String[] strings = keyboard.nextLine().split(" ");
int answer = Integer.parseInt(strings[0]);
int left = 0, right = 0, forward = 0, back = 0;
for (int countA = 0; countA < answer; countA++) {
    for (String string : strings) {
        switch (string.toLowerCase()) {
            case "left" -> left++;
            case "right" -> right++;
            case "forward" -> forward++;
            case "back" -> back++;
        }
    }
}

输入

10 left right back left

输出

answer = 10
strings = [10, left, right, back, left]
left = 20
right = 10
forward = 0
back = 10

My first suggestion is to just add the entire line, split on a space, to a String array.
Then, assign answer the 0 index of the array, utilizing the Integer.parseInt method.

Here is your code, refactored.

Scanner keyboard = new Scanner(System.in);
String[] strings = keyboard.nextLine().split(" ");
int answer = Integer.parseInt(strings[0]);
int left = 0, right = 0, forward = 0, back = 0;
for (int countA = 0; countA < answer; countA++) {
    for (String string : strings) {
        switch (string.toLowerCase()) {
            case "left" -> left++;
            case "right" -> right++;
            case "forward" -> forward++;
            case "back" -> back++;
        }
    }
}

Input

10 left right back left

Output

answer = 10
strings = [10, left, right, back, left]
left = 20
right = 10
forward = 0
back = 10
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文