简体   繁体   English

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

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

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...它只扫描一个词,请提供任何建议...

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();

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);
    }
}
s = sc.next();

it scans only one word pleasd give any suggestions...它只扫描一个词,请提供任何建议...

This is what next();这就是next(); is intended to do.是打算做的。 If you want to read multiple words, the simplest solution is to read a line of text.如果要阅读多个单词,最简单的解决方案是阅读一行文本。 eg例如

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() methods the difference is that you don't discard the rest of the line, but rather you use the String returned by nextLine();注意:虽然答案类似于Scanner 在使用 next()、nextInt() 或其他 nextFoo() 方法后跳过 nextLine()不同之处在于您不会丢弃该行的其余部分,而是使用 String由nextLine();返回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()的额外类一起nextLine()

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);
}

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 上关注我

If you use the nextLine() method immediately following the nextInt() method, recall that nextInt() reads integer tokens;如果您在 nextInt() 方法之后使用 nextLine() 方法,请记住 nextInt() 读取整数标记; 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).因此,该整数输入行的最后一个换行符仍在输入缓冲区中排队,下一个 nextLine() 将读取整数行的其余部分(这是空的)。

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);
     }
}
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();

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();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM