简体   繁体   English

使用 Scanner 类打印字符串

[英]print string by using Scanner class

I am giving this input "Welcome to HackerRank's Java tutorials!"我给出这个输入“欢迎来到 HackerRank 的 Java 教程!” into but进入但

printing only "Welcome" string through scanner class.通过扫描仪类仅打印“欢迎”字符串。

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();
        String s = scan.nextLine();
        scan.close();

        // Write your code here.

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

How to resolve this?如何解决这个问题?

The point is that your string does not contain the integer and a double value on the input line.关键是您的字符串在输入行中不包含整数和双精度值。

If you provide 12 2.56 Welcome to HackerRank's Java tutorials!如果提供12 2.56 Welcome to HackerRank's Java tutorials! , it will work: ,它将起作用:

Scanner scan = new Scanner(System.in);
int i = scan.nextInt();
double d = scan.nextDouble();
scan.nextLine();
String s = scan.nextLine();
scan.close();

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

See the Java demo查看Java 演示

Output:输出:

String:  Welcome to HackerRank's Java tutorials!
Double: 2.56
Int: 12

If you want to make sure your string gets parsed, check the next token with hasNext methods ( hasNextInt() and hasNextDouble() ):如果您想确保您的字符串被解析,请使用hasNext方法( hasNextInt()hasNextDouble() )检查下一个标记:

Scanner scan = new Scanner(System.in);
int i = 0;
if (scan.hasNextInt())
    i = scan.nextInt();
double d = 0d;
if (scan.hasNextDouble())
    d = scan.nextDouble();
String s = scan.nextLine();
scan.close();

See this demo .请参阅此演示

Please write following code..It will work!!请编写以下代码..它会工作!

Scanner scan = new Scanner(System.in);
int i = scan.nextInt();
double d = scan.nextDouble();
scan.nextLine();
String s = scan.nextLine();
scan.close();

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

NOTE: If you use the nextLine() method immediately following the nextInt() or nextDouble() [reading tokens of input and reading a full line of input] method, recall that nextInt() or nextDouble() reads integer tokens;注意:如果在 nextInt() 或 nextDouble() [读取输入标记并读取整行输入] 方法之后立即使用 nextLine() 方法,请记住 nextInt() 或 nextDouble() 读取整数标记; because of this, the last newline character for that line of integer or double input is still queued in the input buffer and the next nextLine() will be reading the remainder of the integer or double line.So,you need to make another call to nextLine().因此,该行整数或双精度输入的最后一个换行符仍在输入缓冲区中排队,下一个 nextLine() 将读取整数或双精度行的剩余部分。因此,您需要再次调用下一行()。

import java.util.Scanner;
class Solution {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("\nEnter The String= ");
        String s = scan.nextLine();
        System.out.println("\nEnter The int= ");
        int i = scan.nextInt();
        System.out.println("\nEnter The Double= ");
        double d = scan.nextDouble();   
        scan.close();

        // Write your code here.

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

I tried this and it was successful:我试过了,它成功了:

import java.util.Scanner;

public class Solution {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int i;
        double d;
        String s;
       
        i = scan.nextInt();
        d = scan.nextDouble();
        scan.nextLine();
        s = scan.nextLine();
        scan.close();
        
        System.out.println("String: " + s);
        System.out.println("Double: " + d);
        System.out.println("Int: " + i);
    }
}

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

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