java中的nextInt(),next()和nextLine()

转自博客:http://www.cnblogs.com/Skyar/p/5892825.html



解释:

nextInt(): it only reads the int value, nextInt() places the cursor in the same line after reading the input.

next(): read the input only till the space. It can't read two words separated by space. Also, next() places the cursor in the same line after reading the input.

nextLine():  reads input including space between the words (that is, it reads till the end of line \n). Once the input is read, nextLine() positions the cursor in the next line.

看完之后nextInt()、next()和nextLine()的区别已经很清楚了,我觉得最容易出错的就是cursor问题。

看下面代码:

复制代码
 1 import java.util.Scanner;
 2 
 3 public class MaxMap {
 4     public static void main(String[] args){
 5         Scanner cin = new Scanner(System.in);
 6         int n = cin.nextInt();
 7         String str = cin.nextLine();
 8         System.out.println("END");
 9         }
10 }            
复制代码

执行后结果:

从执行结果上看,貌似直接跳过了String str = cin.nextLine();这行代码。

其实不然,原因是:nextInt()只读取数值,剩下"\n"还没有读取,并将cursor放在本行中。nextLine()会读取"\n",并结束(nextLine() reads till the end of line \n)。

如果想要在nextInt()后读取一行,就得在nextInt()之后额外加上cin.nextLine(),代码如下

复制代码
import java.util.Scanner;

public class MaxMap {
    public static void main(String[] args){
        Scanner cin = new Scanner(System.in);
        int n = cin.nextInt();
        cin.nextLine();
        String str = cin.nextLine();
        System.out.println("END");
        }
}
复制代码

在看下面代码:

复制代码
 1 import java.util.Scanner;
 2 
 3 public class MaxMap {
 4     public static void main(String[] args){
 5         Scanner cin = new Scanner(System.in);
 6         String n = cin.next();
 7         //cin.nextLine();
 8         String str = cin.nextLine();
 9         System.out.println("END");
10         System.out.println("next()read:"+n);
11         System.out.println("nextLine()read:"+str);
12     }
13 }
复制代码

执行结果:

 原因:next()只读空格之前的数据,并且cursor指向本行,后面的nextLine()会继续读取前面留下的数据。

    想要读取整行,就是用nextLine()。

    读取数字也可以使用nextLine(),不过需要转换:Integer.parseInt(cin.nextLine())。

注意在next()、nextInt()与nextLine()一起使用时,next()、nextInt()往往会读取部分数据(会留下"\n"或者空格之后的数据)。


  • 7
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
nextLine() 方法用于读取输入的一行文本,包括空格和特殊字符。该方法返回一个字符串,以换行符(\n)结尾。 例如,以下代码将读取用户输入的一行文本: ``` import java.util.Scanner; public class Test { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("请输入一行文本:"); String str = sc.nextLine(); System.out.println("您输入的是:" + str); } } ``` 如果用户输入了文本 "Hello World",则输出结果如下: ``` 请输入一行文本:Hello World 您输入的是:Hello World ``` next() 方法用于读取输入的下一个字符串,直到遇到空格或特殊字符为止。该方法返回一个字符串,不包括空格和特殊字符。 例如,以下代码将读取用户输入的一个字符串: ``` import java.util.Scanner; public class Test { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("请输入一个字符串:"); String str = sc.next(); System.out.println("您输入的是:" + str); } } ``` 如果用户输入了字符串 "Hello World",则输出结果如下: ``` 请输入一个字符串:Hello World 您输入的是:Hello ``` nextLine() 方法和 next() 方法的区别在于,nextLine() 方法会读取包括空格和特殊字符在内的整行文本,而 next() 方法只会读取下一个字符串,直到遇到空格或特殊字符为止。因此,如果想读取包含空格的字符串,应该使用 nextLine() 方法。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值