Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/347.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用Scanner类打印字符串_Java_String_Printing - Fatal编程技术网

Java 使用Scanner类打印字符串

Java 使用Scanner类打印字符串,java,string,printing,Java,String,Printing,我在but中输入“欢迎来到HackerRank的Java教程!” 通过扫描器类仅打印“欢迎”字符串 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.n

我在but中输入“欢迎来到HackerRank的Java教程!”

通过扫描器类仅打印“欢迎”字符串

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

如何解决此问题?

关键是字符串在输入行中不包含整数和双精度值

如果您提供
12 2.56,欢迎使用HackerRank的Java教程,它将工作:

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

输出:

String:  Welcome to HackerRank's Java tutorials!
Double: 2.56
Int: 12
如果要确保字符串被解析,请使用
hasNext
方法(和)检查下一个标记:


看这个

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

如果行上没有任何内容,它只会使用换行符并移动到下一行的开头


在双重声明之后,您必须编写:
scan.nextLine()

请编写以下代码..它会工作

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

注意:如果在nextInt()或nextDouble()[读取输入标记并读取整行输入]方法之后立即使用nextLine()方法,请记住nextInt()或nextDouble()读取整数标记;因此,整数或双精度输入行的最后一个换行符仍在输入缓冲区中排队,下一个nextLine()将读取整数或双精度输入行的其余部分。因此,您需要再次调用nextLine()。

此代码无法打印“欢迎”您的问题有答案。欢迎使用堆栈溢出!你能在内容中有一个更好的标题和更详细的信息来解决这个问题吗?上面的回答不正确,我不知道是怎么回事acceptable@Arun它对OP非常有效。请参阅打印字符串、double和int值。您的要求似乎与此OP不同。如果您的代码仍然有问题,请发布您自己的问题。我希望上述解决方案不起作用,scan.nextLine();应在scan.nextDouble()之后添加;然后它就开始工作了。@komalakhani问题不在于如何读取多行,而在于如何解析传递给扫描仪的一行(可能包含或不包含特定数据)。因此,它适用于OP中的当前场景。无论如何,。你的解决方案成功了。我也在你的其他帖子中对你的解决方案给出了相同的评论。它适用于我!!太好了,我很高兴这对你有帮助。不鼓励只使用代码的答案。请添加一些解释,说明这是如何解决问题的,或者这与现有答案有何不同。谢谢你的建议@Nick。下一次我会尽力的
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);
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);
    }
}