Java中hasNext()与next()的区别,hasNextInt()与nextInt()的区别,hasNextDouble()与nextDouble()的区别

转载自:https://blog.csdn.net/weixin_37770552/article/details/77431961

还有补充:https://zhidao.baidu.com/question/198579166802848525.html

java.util.Scanner;
Scanner in=new Scanner(System.in);
String well=in.nextLine();
  • 1
  • 2
  • 3
next()与hasNext() 的区别:
String next()读取输入的下一个单词,以空格作为分隔符,返回输入的字符串
int hasNext()读取下一个单词,以空格作为分隔符,返回检测输入中是否还有其他单词
String nextLine()读取输入的下一行内容,返回一个字符串
boolean hasNextInt(); 
String nextInt();
boolean hasNextDouble();
String nextDouble();
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

其中,hasNext()不能显示false:

import java.util.*;
public class 第3章a
{
    public static void main(String[] args)
    {
        Scanner in=new Scanner(System.in);
        boolean number=in.hasNext();
        System.out.println(number);
    }
 }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

Scanner在java.util.* ; 包中
Console 在java.io.Console包中
char[] readPassword(String prompt,Object args);
String readLine(String prompt ,Object args);

import java.io.Console;
public class ClassName
{
    public static void main(String[] args)
    {
        Console cons=System.console();
        String name=cons.readLine();
        char[] password=cons.readPassword();
    }
}
  •  
console有两个,一个在java.lang.System中,另一个在java.io.Console中。
1、java.lang.System中
 java.lang.System
 static Console console()
如果有可能进行交互,就通过控制台为交互的用户返回一个console的对象,否则返回null。对于任何一个通过控制台窗口启动的程序,都可使用Console对象,否则,其可用性与所使用的系统有关、
2、java.io.Console
static String readLine();
static char[] readPassword();

猜你喜欢

转载自blog.csdn.net/m0_37292262/article/details/81454370