Scanner类

 

import java.io.Console;
import java.util.Scanner;

/*2015-11-29*/
public class ConsoleInput {
    public static void main(String[] args) {
        System.out.println("Please enter the item ID:");
        Console console = System.console();
        if (console == null) {
            System.out.println("以Javaw所执行的应用(譬如eclipse)没有主控制台(console),所以System.console()为null");
            System.out.print(">");
            Scanner scanner = new Scanner(System.in);
            long input = scanner.nextLong();
            System.out.println(input);
            return;
        }
        String input = console.readLine(">");
        System.out.println(Long.parseLong(input));
    }

}

输入:两个回车,再输入“test”,回车

nextLine:

import java.util.Scanner;

public class ScanerTest {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()) {
            String input = scanner.nextLine();
//             String input=scanner.next();
            System.out.println("Input:" + input);
        }
        scanner.close();
    }
}

OutPut:

Input:
Input:
Input:test

next:

import java.util.Scanner;

public class ScanerTest {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()) {
//            String input = scanner.nextLine();
             String input=scanner.next();
            System.out.println("Input:" + input);
        }
        scanner.close();
    }
}

OutPut:

Input:test

 

Scanner对象中nextLine和next的差别。

String java.util.Scanner.nextLine()

Advances this scanner past the current line and returns the input that was skipped. This method returns the rest of the current line, excluding any line separator at the end. The position is set to the beginning of the next line.

Since this method continues to search through the input looking for a line separator, it may buffer all of the input searching for the line to skip if no line separators are present.

Returns: the line that was skipped
Throws: NoSuchElementException - if no line was found
IllegalStateException - if this scanner is closed

 

String java.util.Scanner.next()

Finds and returns the next complete token from this scanner. A complete token is preceded and followed by input that matches the delimiter pattern. This method may block while waiting for input to scan, even if a previous invocation of hasNext returned true.

Specified by:
next() in Iterator
Returns: the next token
Throws: NoSuchElementException - if no more tokens are available
IllegalStateException - if this scanner is closed
See Also: java.util.Iterator

 

 

时间: 2024-09-20 00:18:27

Scanner类的相关文章

scanner-Java基础学习中Scanner类nextInt方法问题

问题描述 Java基础学习中Scanner类nextInt方法问题 import java.util.*; public class Practice { public static String prompt = "How are you? "; public static void main(String[] args) { Scanner console = new Scanner(System.in); System.out.print(prompt); //int year=

java-Java Scanner类使用时产生的一个错误,求解

问题描述 Java Scanner类使用时产生的一个错误,求解 package upper_test; import java.util.*; public class Scanner_00 { int a; long b; float c; String d; Double e; public Scanner_00(String x){ Scanner s=new Scanner(x); a=s.nextInt(); b=s.nextLong(); c=s.nextFloat(); d=s.n

scanner-为什么我导入不了Scanner类啊?

问题描述 为什么我导入不了Scanner类啊? 解决方案 import java.util.Scanner; 注意大小写 解决方案二: java.uti.Scanner改为 java.util.scanner 解决方案三: uti 应该为 util

java中Scanner类的简单用法分享_java

复制代码 代码如下: public static void main(String[] args) {     System.out.println("请输入值,以回车结束:");     Scanner sc = new Scanner(System.in);     sc.useDelimiter("\n");     while (sc.hasNext()) {         String s = sc.next();         System.out.

Java中Scanner类与BufferReader类的不同点(非常详细)_java

java.util.Scanner类是一个简单的文本扫描类,它可以解析基本数据类型和字符串.它本质上是使用正则表达式去读取不同的数据类型. Java.io.BufferedReader类为了能够高效的读取字符序列,从字符输入流和字符缓冲区读取文本. 下面是两个类的不同之处: 当nextLine()被用在nextXXX()之后,用Scanner类有什么问题 尝试去猜测下面代码的输出内容: // Code using Scanner Class import java.util.Scanner; c

令人蛋疼的Scanner类

问题描述 不得不承认Scanner类对英文输入支持是非常好的!但是针对中文输入,Scanner的传说中的阻塞问题就出来了!请看下面的小程序:import java.util.Scanner;public class Test {public static void main(String [] args) {new Test().start();}public void start() {Scanner sc = new Scanner(System.in);while(true) {Syste

Scanner类输入

1.导入java.util.Scanner类 2.创建Scanner对象 Scanner input=new Scanner(System.in); 3.接收并保存用户输入的值 int x=input.nextInt(); input.nextDouble();

Scanner类及正则表达式

  import java.util.Scanner; public class ScannerToString { public static void main(String[] args) { Scanner scanner = new Scanner("InputString"); System.out.println(scanner); } } Output: java.util.Scanner[delimiters=\p{javaWhitespace}+][position

java Scanner输入问题

问题描述 java Scanner输入问题 我想一下子输入都个数:不想这么写,有没有简单点的方法 Scanner in = new Scanner(System.in); int a = in.nextInt(); Scanner in1 = new Scanner(System.in); int b = in.nextInt(); Scanner in2 = new Scanner(System.in); int c = in.nextInt(); 解决方案 Scanner in = new