Java - Scanner只执行Int和“skip”当我在Int之前输入String数据时的Strings数据类型 [英] Java - Scanner execute only the Int and "skip" the Strings data types when i input String data before the Int ones

查看:163
本文介绍了Java - Scanner只执行Int和“skip”当我在Int之前输入String数据时的Strings数据类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用 java.util.Scanner 时,我尝试使用整数和 String s at数据输入。

While I'm working with java.util.Scanner, I tried to use integers and Strings at data input.

我遇到的问题是,当我在String 1之前输入Int数据时,控制台跳过String数据并立即转到下一个Int数据。

The problem that I faced is, when I input an Int data before a String one, the console skip the String data and go immediately to the next Int one.

这是我发生问题的简单问题:

Here is my simple problem where the problem is happening :

package justForTest;

import java.util.Scanner;

public class EmptySpaceWorkshop {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);

        System.out.print("- Enter your Name : ");
        String name = input.nextLine();

        System.out.print("- Enter your IC number : ");
        int icNumber = input.nextInt();

        System.out.print("- Enter your Place of birth : ");
        String placeOfBirth = input.nextLine();

        System.out.print("- Enter your Age : ");
        int age = input.nextInt();

        System.out.println("There once was a wonderful person named " + name+ ", His IC number is " + icNumber );

        System.out.println(". He/She is " + age + " years old. She/He was born at " + placeOfBirth );

    }

}

这里是我的输出:

- Enter your Name : Ali HISOKA
- Enter your IC number : 123456
- Enter your Place of birth : - Enter your Age : 

我尝试了很多来解决这个问题。我能想出的唯一解决方案是使用 input.next(); 而不是 input.nextLine(); 。但是,这个解决方案对我来说是USELESS,因为你们知道,当使用 input.next(); 时,我们只能输入一个单一字,不像 input.nextLine(); 我们可以输入很多单词,这是我正在寻找的东西。此外,我不想重新排序(重新安排)我的输入并首先键入字符串数据,然后通过Int数据来解决我的问题。我希望我的数据与您在我的简单程序中输入的数据相同(输入名称,输入IC编号,输入出生地点,然后输入年龄)。我可以解决这个问题吗?

I tried a lot to fix this problem. The only solution I could came up with is using input.next(); instead of input.nextLine(); . However, this solution is USELESS for me because as you guys know, when using input.next(); we can only type One Single Word, unlike input.nextLine(); we can type a lot of words which is the thing that I'm looking for. Also I DO NOT want to re-sort (re-arrange) my input and type the Strings data first, then following by the Int data to solve my problem. I want my data to be at the same sort as you seen above in my simple program ( Enter Name, Enter IC Number, Enter Place of Birth, then Enter age). So can I solve this problem ?

我在互联网上搜索了很多像我这样的人有问题,但我找不到问题和解决问题的方法看起来和我一模一样。

I searched a lot on the internet for someone got a problem as mine, but I couldn't find a question and solution for a problem looks exactly like mine.

我已经知道我的问题的原因和解释,这可以用

I already know the reason and the explanation of my problem which is explained by


Joshua

Joshua

错误的原因是nextInt只拉取整数,
不是换行符。如果你添加一个在你的for循环之前的in.nextLine()中,
将占用空的新行并允许你输入3个名字。

"The reason for the error is that the nextInt only pulls the integer, not the newline. If you add a in.nextLine() before your for loop, it will eat the empty new line and allow you to enter 3 names."

但是它对解决我的问题没有帮助。

but still it's not helpful for solving my problem.

推荐答案

从我看来它似乎 readLine()只是消耗了从缓冲区中获取int后剩下的换行符。解决此问题的更好方法是使用 nextLine()获取字符串值并将其转换为:

From what I can see it appears that the readLine() is just consuming the newline left after the int was taken from the buffer. A better way to fix this is to use nextLine() to get a string value and convert it:

int icNumber = Integer.parseInt(input.nextLine());

这篇关于Java - Scanner只执行Int和“skip”当我在Int之前输入String数据时的Strings数据类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆