nextLine和next的区别

两者的共同作用:都能接收字符串数据

import java.util.Scanner;
public class Demo {
    //next和nextLine的基本用法
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);

        System.out.println("输入nextLine的字符串:");
        String str2=sc.nextLine();
        System.out.println(str2);

        System.out.println("输入next的字符串:");
        String str=sc.next();
        System.out.println(str);
    }
}

这里写图片描述

区别一:next如果前面有空格,Table,Enter都不会识别,直到遇到字符串才开始到遇到空格或者Enter结束

import java.util.Scanner;
public class Demo2 {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);

        System.out.println("请输入字符串:");
        String s=sc.next(); //开头会忽略空格,table,enter
                            //遇到字符串后遇到空格就结束了
        System.out.println(s);
    }
}

这里写图片描述

区别二:nextLine 如果前面有Enter就会结束读取,否则会读取整行内容(包括空格,table),知道遇到Enter结束.

import java.util.Scanner;
public class Demo3 {
    public static void main(String[] args) {
            Scanner sc=new Scanner(System.in);
            System.out.println("请输入字符串:");
            String s=sc.nextLine(); //如果Enter会结束读取
                                    //会读取整行内容(包括空格,table)
            System.out.println(s);
    }

}

这里写图片描述

**附:next和nextLine同时使用时(并且next在前)会出现问题!
原因:nextLine会接收next的enter从而结束读取**

import java.util.Scanner;
public class Demo4 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        System.out.println("输入next的字符串:");
        String str=sc.next();
        System.out.println(str);

        System.out.println("输入nextLine的字符串:");
        String str2=sc.nextLine();
        System.out.println(str2);
        System.out.println("nextLine结束读取!");
    }
}

这里写图片描述

**解决方法:
1.重新创建一个键盘录入对象**

import java.util.Scanner;
public class Demo4 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        System.out.println("输入next的字符串:");
        String str=sc.next();
        System.out.println(str);

        //解决方法一:添加一键盘录入对象
        Scanner sc2=new Scanner(System.in);
        System.out.println("输入nextLine的字符串:");
        String str2=sc2.nextLine();
        System.out.println(str2);
        System.out.println("nextLine结束读取!");
    }
}

这里写图片描述

2.增加一个nextLine接收Enter

import java.util.Scanner;
public class Demo6 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        System.out.println("输入next的字符串:");
        String str=sc.next();
        System.out.println(str);

        System.out.println("输入nextLine的字符串:");
        sc.nextLine();  //解决方法二:next后面添加一个nextLIne
        String str2=sc.nextLine();
        System.out.println(str2);
        System.out.println("nextLine结束读取!");
    }
}

这里写图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值