Java 如何使用相同的扫描仪变量读取字符串的 int、double 和句子

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32948425/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 14:04:35  来源:igfitidea点击:

how to read int,double,and sentence of string using same scanner variable

javajava.util.scanner

提问by Gokul Raj

import java.util.Scanner;

public class Solution {

    public static void main(String[] args) {
            Scanner sc=new Scanner(System.in);
            String s=new String();
            int x=sc.nextInt();
            double y=sc.nextDouble();
            s = sc.next();

            System.out.println("String:"+s);
            System.out.println("Double: "+y); 
            System.out.println("Int: "+x);     
     }       
}

it scans only one word pleasd give any suggestions...

它只扫描一个词,请提供任何建议...

回答by Peter Lawrey

s = sc.next();

it scans only one word pleasd give any suggestions...

它只扫描一个词,请提供任何建议...

This is what next();is intended to do. If you want to read multiple words, the simplest solution is to read a line of text. e.g.

这是我们next();打算做的。如果要阅读多个单词,最简单的解决方案是阅读一行文本。例如

String s = sc.nextLine();

If you need to read multi-line text, you need to workout a strategy for doing this such as reading everything in a loop until you have a blank line.

如果您需要阅读多行文本,则需要制定执行此操作的策略,例如循环阅读所有内容,直到出现空行。

Note: while the answer is similar to Scanner is skipping nextLine() after using next(), nextInt() or other nextFoo() methodsthe difference is that you don't discardthe rest of the line, but rather you use the String returned by nextLine();

注意:虽然答案类似于Scanner 在使用 next()、nextInt() 或其他 nextFoo() 方法后跳过 nextLine()不同之处在于您不会丢弃该行的其余部分,而是使用 String返回nextLine();

If you are expecting the input to be

如果您希望输入是

 1 2.0 Hello World

then you want to use the suggestion above, however if the input is on multiple lines like this

那么你想使用上面的建议,但是如果输入是这样的多行

 1 2.0
 Hello World

You will need to discard the rest of the line with an extra class to nextLine()as the link above suggests.

nextLine()正如上面的链接所建议的那样,您需要使用额外的类来丢弃该行的其余部分。

回答by Arun

import java.util.Scanner;

public class Solution {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int x = sc.nextInt();
        double y = sc.nextDouble();
        sc.nextLine();
        String s = sc.nextLine();

        System.out.println("String: " + s);
        System.out.println("Double: " + y);
        System.out.println("Int: " + x);
    }
}

回答by Bittu Sharma

I'd rather suggest to try out this block of code

我宁愿建议尝试这个代码块

Scanner scan = new Scanner();
int i = Integer.parseInt(scan.nextLine());
double d = Double.parseDouble(scan.nextLine());
String s = scan.nextLine();

I hope this would not skip reading the String input (or just read a single word skipping rest of the Line) after a double or integer input..

我希望这不会在双精度或整数输入之后跳过读取字符串输入(或者只是读取一个单词跳过行的其余部分)。

follow me on fb/ayur.sharma

在 fb/ayur.sharma 上关注我

回答by shrihari wankhade

If you use the nextLine() method immediately following the nextInt() method, recall that nextInt() reads integer tokens; because of this, the last newline character for that line of integer input is still queued in the input buffer and the next nextLine() will be reading the remainder of the integer line (which is empty).

如果您在 nextInt() 方法之后使用 nextLine() 方法,请记住 nextInt() 读取整数标记;因此,该整数输入行的最后一个换行符仍在输入缓冲区中排队,下一个 nextLine() 将读取整数行的其余部分(这是空的)。

回答by Sneha

import java.util.Scanner;

public class Solution 
{
    public static void main(String[] args) 
    {
     Scanner scan = new Scanner(System.in);
     int i = Integer.parseInt(scan.nextLine());
     double d = Double.parseDouble(scan.nextLine());
     String s = scan.nextLine();

     System.out.println("String: " + s);
     System.out.println("Double: " + d);
     System.out.println("Int: "+ i);
     }
}

回答by Arun

import java.util.Scanner;

public class Solution 
{
    public static void main(String[] args) 
    {
     Scanner scan = new Scanner(System.in);
     int i = Integer.parseInt(scan.nextLine());
     double d = Double.parseDouble(scan.nextLine());
     scan.nextLine();
     String s = scan.nextLine();
     scan.close();

     System.out.println("String: " + s);
     System.out.println("Double: " + d);
     System.out.println("Int: "+ i);
     }
}

When switching between reading tokens of input and reading a full line of input, you need to make another call to nextLine() because the Scanner object will read the rest of the line where its previous read left off.

在读取输入标记和读取整行输入之间切换时,您需要再次调用 nextLine(),因为 Scanner 对象将读取前一次读取停止的行的其余部分。

If there is nothing on the line, it simply consumes the newline and moves to the beginning of the next line.

如果该行没有任何内容,它只会消耗换行符并移动到下一行的开头。

After double declaration, you have to write: scan.nextLine();

双重声明后,你必须写: scan.nextLine();

回答by Puneet Soni

You can try this code:

你可以试试这个代码:

import java.util.Scanner;

public class Solution {

public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    int i = scan.nextInt();
    double d = scan.nextDouble();

    scan.nextLine();
    String s = scan.nextLine();

    System.out.println("String: " + s);
    System.out.println("Double: " + d);
    System.out.println("Int: " + i);
}

Because the Scanner object will read the rest of the line where its previous read left off.

因为 Scanner 对象将读取其上一次读取停止的行的其余部分。

If there is nothing on the line, it simply consumes the newline and moves to the starting of the next line.

如果该行没有任何内容,它只会消耗换行符并移动到下一行的开头。

After double declaration, you have to write: scan.nextLine();

双重声明后,你必须写: scan.nextLine();

回答by jprism

Why don't you skip as follows

你为什么不跳过如下

import java.util.Scanner;

public class Solution {

public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    int i = scan.nextInt();
    scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
    double d = scan.nextDouble();
    scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");

    scan.nextLine();
    String s = scan.nextLine();

    System.out.println("String: " + s);
    System.out.println("Double: " + d);
    System.out.println("Int: " + i);
}

回答by Mukta

Here is your answer

这是你的答案

public static void main(String[] args) {     
    Scanner scan = new Scanner(System.in);

    int i = scan.nextInt();         
    double d = scan.nextDouble(); 

    scan.nextLine();
    String s = scan.nextLine();  

    //if you want to skip the unnecessary input
    //scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");

    System.out.println("String: " + s);       
    System.out.println("Double: " + d);      
    System.out.println("Int: " + i);

   scan.close();
}