简体   繁体   中英

print string by using Scanner class

I am giving this input "Welcome to HackerRank's Java tutorials!" 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! , 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

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

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

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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