Hassan Raza
Hassan Raza

Reputation: 37

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?

Upvotes: 1

Views: 28894

Answers (5)

Yusuf Bahtiar
Yusuf Bahtiar

Reputation: 1

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

Upvotes: 0

Devang
Devang

Reputation: 21

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

Upvotes: 2

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626699

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.

Upvotes: 3

komal akhani
komal akhani

Reputation: 573

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

Upvotes: 1

VED
VED

Reputation: 9

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

Upvotes: 0

Related Questions