You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java Scanner输入异常:获取数组长度后跳过首个字符串输入

解决Scanner读取整数后跳过第一个字符串输入的问题

这是Java新手使用Scanner时经常踩的小坑,我来帮你捋清楚问题根源和解决办法~

问题重现

在以下Java代码中,首次循环时无法获取第一个单词的输入。当不向用户获取字符串数组长度时,代码运行正常。可查看输出,其中未获取word 0的输入。

用户提供的代码:

public class Stringinput { 
    public static void main(String[] args) { 
        Scanner in = new Scanner(System.in); 
        System.out.println("How many words do you need to make the sentence: "); 
        int n = in.nextInt(); 
        String[] names = new String[n]; 
        for (int i = 0; i < names.length; i++) { 
            System.out.printf("Enter word no %d : ",i); 
            names[i]= in.nextLine(); 
        } 
        System.out.println("The sentence you made is: "); 
        for (int i = 0; i < names.length; i++) { 
            System.out.print(names[i]+" "); 
        } 
        System.out.println(""); 
    } 
}

运行输出:

How many words do you need to make the sentence: 4 
Enter word no 0 : 
Enter word no 1 : My 
Enter word no 2 : name 
Enter word no 3 : is 
The sentence you made is:  My name is  
BUILD SUCCESSFUL (total time: 11 seconds)

问题根源

核心原因是**nextInt()不会读取输入末尾的换行符**:
当你输入4然后按下回车时,in.nextInt()只取走了数字4,而回车产生的\n字符还留在输入缓冲区里。紧接着第一次循环调用in.nextLine()时,它会直接读取这个残留的换行符,把它当成一个空字符串赋值给names[0],所以看起来像是跳过了第一个单词的输入。

两种解决方案

方案1:在nextInt()后额外调用一次nextLine()清空换行符

在读取完整数n之后,加一行in.nextLine()来吃掉残留的换行符,这样后续的nextLine()就能正常读取用户输入的单词了:

public class Stringinput { 
    public static void main(String[] args) { 
        Scanner in = new Scanner(System.in); 
        System.out.println("How many words do you need to make the sentence: "); 
        int n = in.nextInt(); 
        // 新增这一行,清空输入缓冲区里的换行符
        in.nextLine();
        String[] names = new String[n]; 
        for (int i = 0; i < names.length; i++) { 
            System.out.printf("Enter word no %d : ",i); 
            names[i]= in.nextLine(); 
        } 
        System.out.println("The sentence you made is: "); 
        for (int i = 0; i < names.length; i++) { 
            System.out.print(names[i]+" "); 
        } 
        System.out.println(""); 
    } 
}

方案2:用nextLine()读取整行后转成整数(更稳妥)

避免混用nextInt()和nextLine(),直接用nextLine()读取用户输入的整行内容,再通过Integer.parseInt()转成整数,这样就不会有换行符残留的问题:

public class Stringinput { 
    public static void main(String[] args) { 
        Scanner in = new Scanner(System.in); 
        System.out.println("How many words do you need to make the sentence: "); 
        // 读取整行后转成整数
        int n = Integer.parseInt(in.nextLine()); 
        String[] names = new String[n]; 
        for (int i = 0; i < names.length; i++) { 
            System.out.printf("Enter word no %d : ",i); 
            names[i]= in.nextLine(); 
        } 
        System.out.println("The sentence you made is: "); 
        for (int i = 0; i < names.length; i++) { 
            System.out.print(names[i]+" "); 
        } 
        System.out.println(""); 
    } 
}

修改后运行效果

调整后再运行,输入4个单词的话,输出会正常获取所有输入:

How many words do you need to make the sentence: 4
Enter word no 0 : Hello
Enter word no 1 : My
Enter word no 2 : name
Enter word no 3 : is
The sentence you made is: Hello My name is 

内容的提问来源于stack exchange,提问作者 Mahbub

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 03:55:47