扫描器在Java中返回空字符串。

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

Scanner Return Empty String in Java

问题

以下是翻译好的部分:

我有一段工作中的代码。基本上这是一个可以保存带有空格的字符串的扫描器输入。这是我的代码:

public class TestScannerString {
    
    static Scanner sc = new Scanner(System.in);
    
    public static void main(String[] args) {
        System.out.println("输入字符串:"); 
        String[] a = sc.nextLine().split(" ");
        String name = "";
 
        for (String a0 : a) {
            name += a0 + " ";
        }
        
        System.out.println(name);

    }
}

这是该代码的输出:
扫描器在Java中返回空字符串。

然后我尝试将这段代码实现到一个小项目中。这是包含先前代码的代码块:

for (int i=0; i<jumlahData; i++) {
    System.out.println("输入婴儿数据 " +(i+1));
    System.out.println("姓名:");
    String[] a = sc.nextLine().split(" ");
    String name = "";
    for (String a0 : a) {
        name += a0 + " ";
    }
    bayi[i].nama = name;
    sc.nextLine();
    System.out.print("月龄:");
    bayi[i].usiaDalamBulan = sc.nextInt();
    System.out.print("重量(kg):");
    bayi[i].beratDalamKg = sc.nextFloat();
    System.out.print("长度(cm):");
    bayi[i].panjangDalamCm = sc.nextInt();
    System.out.println("姓名:" +name);
    System.out.println();
    System.out.println("第 " + (i+1) + " 个婴儿的姓名:" + bayi[i].nama);
}

当我输入 "example of name" 并打印姓名时,它返回空字符串,如下所示:

扫描器在Java中返回空字符串。

是否有任何建议可以解释为什么会发生这种情况,以及我应该怎么做?非常感谢您的帮助。

英文:

I have a fraction of code which is working. This is basically a scanner input so that it can save String with space. This is my code:

public class TestScannerString {
    
    static Scanner sc = new Scanner(System.in);
    
    public static void main(String[] args) {
        System.out.println(&quot;Enter strings: &quot;); 
        String[] a = sc.nextLine().split(&quot; &quot;);
        String name = &quot;&quot;;
 
        for (String a0 : a) {
            name += a0 + &quot; &quot;;
        }
        
        System.out.println(name);

    }
}

This is the output of that code:
扫描器在Java中返回空字符串。

Then i try to implement this code into mini-project. This is the block that contain previous code:

for (int i=0; i&lt;jumlahData; i++) {
            System.out.println(&quot;Masukkan Data Bayi ke &quot; +(i+1));
            System.out.println(&quot;Nama : &quot;);
            String[] a = sc.nextLine().split(&quot; &quot;);
            String name = &quot;&quot;;
            for (String a0 : a) {
                name += a0 + &quot; &quot;;
            }
            bayi[i].nama = name;
            sc.nextLine();
            System.out.print(&quot;Usia dalam Bulan : &quot;);
            bayi[i].usiaDalamBulan = sc.nextInt();
            System.out.print(&quot;Berat (kg) : &quot;);
            bayi[i].beratDalamKg = sc.nextFloat();
            System.out.print(&quot;Panjang (cm) : &quot;);
            bayi[i].panjangDalamCm = sc.nextInt();
            System.out.println(&quot;Name :&quot; +name);
            System.out.println();
            System.out.println(&quot;Nama bayi ke &quot; + (i+1) + &quot;: &quot; +bayi[i].nama);
}

After i input "example of name" and print the name, it returns empty string like this:

扫描器在Java中返回空字符串。

Any suggestion why this happens and what should i do? Any help would be appreciated. Thanks before.

答案1

得分: 1

好的,修复方法是在循环开始之前将 nextLine() 放置在扫描器输入之前。我认为这是因为之前的输入中有换行符。因此,最终的代码如下所示:

for (int i = 0; i < jumlahData; i++) {
    sc.nextLine();
    System.out.println("Masukkan Data Bayi ke " + (i + 1));
    System.out.print("Nama : ");
    String nameTest = sc.nextLine();
    bayi[i].nama = nameTest;
    System.out.print("Usia dalam Bulan : ");
    bayi[i].usiaDalamBulan = sc.nextInt();
    System.out.print("Berat (kg) : ");
    bayi[i].beratDalamKg = sc.nextFloat();
    System.out.print("Panjang (cm) : ");
    bayi[i].panjangDalamCm = sc.nextInt();
    System.out.println();
}
英文:

Okay so the fix is by putting nextLine() in the beginning of loop before the scanner input. I think it is because of previous input that has newLine in it. So the final code is like this:

for (int i=0; i&lt;jumlahData; i++) {
            sc.nextLine();
            System.out.println(&quot;Masukkan Data Bayi ke &quot; +(i+1));
            System.out.print(&quot;Nama : &quot;);
            String nameTest = sc.nextLine();
            bayi[i].nama = nameTest;
            System.out.print(&quot;Usia dalam Bulan : &quot;);
            bayi[i].usiaDalamBulan = sc.nextInt();
            System.out.print(&quot;Berat (kg) : &quot;);
            bayi[i].beratDalamKg = sc.nextFloat();
            System.out.print(&quot;Panjang (cm) : &quot;);
            bayi[i].panjangDalamCm = sc.nextInt();
            System.out.println();
}

答案2

得分: 0

我没有完整的代码来测试这个,但看起来你有两行代码来接收一行字符串作为输入。

String[] a = sc.nextLine().split(" ");

sc.nextLine();

我猜你实际上是在为第二个输入输入名称,这不会将输入保存到任何引用中。对于前一行代码,你可能误认为空字符串作为响应。

英文:

I don't have the complete code to test this but it seems like you have two lines of code for taking a line of string as an input.

String[] a = sc.nextLine().split(&quot; &quot;);

and

sc.nextLine();

My guess is you are actually entering the name for the second one, which doesn't save the input to any reference. For the former line of code you probably are mistakenly responding with an empty string.

huangapple
  • 本文由 发表于 2020年9月24日 22:54:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/64049065.html
匿名

发表评论

匿名网友

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

确定