0

I have a program that asks the user for an input of numbers and characters. For this I am using a Scanner class. But when I take two subsequent string inputs, the console just moves and asks the user to input the second string. The user is unable to input the first string. I have attached my code below to be more specific.

import java.util.Scanner;

class XYZ {

    public static void main(String args[]) {
        int x, y, z, b, c;
        String p, q;
        Scanner sc = new Scanner(System.in);

        System.out.println("Enter a number");
        b = sc.nextInt();

        System.out.println("Enter a new number");
        c = sc.nextInt();

        System.out.println("Enter a String");
        p = sc.nextLine();

        System.out.println("Enter a new String");
        q = sc.nextLine();

        System.out.println("Enter a number");
        x = sc.nextInt();

        System.out.println("Enter 2nd number");
        y = sc.nextInt();

        System.out.println("Enter 3rd number");
        z = sc.nextInt();

        ABC a = new ABC();

        a.PQR(b, c);
        a.PQR(p, q);
        a.PQR(x, y, z);
    }
}

class ABC {

    void PQR(int a, int b) {
        int res = a + b;
        System.out.println("The sum is" + " " + res);
    }

    void PQR(String a, String b) {
        System.out.println("Thye concataneted string is" + a + " " + b);
    }

    void PQR(int a, int b, int c) {
        int res = a * b * c;
        System.out.println("The product is" + " " + res);
    }
}
Adam Michalik
  • 9,448
  • 11
  • 55
  • 89
Pratik Paul
  • 43
  • 1
  • 11

5 Answers5

4

You need to make a dummy call to nextLine() after your call to nextInt(). The nextInt() isn't taking the "\n" into account.

Alternatively, and preferably, just use nextLine() to accept all input and whichever ones need to be ints, just use:

int i = Integer.parseInt(**string**);

Does this help?

Debosmit Ray
  • 4,731
  • 2
  • 21
  • 41
2

Thats because the Scanner#nextInt method does not consume the last newline character of your input, and thus that newline is consumed in the next call to Scanner#nextLine.

ref

OR

If you use the nextLine() method immediately following the nextInt() method, recall that nextInt() reads integer tokens, 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). - hackerrank.com

Update : After c = sc.nextInt(); you need to put one more statement i.e.

sc.nextLine();

HTH!

Community
  • 1
  • 1
Himanshu Bhandari
  • 1,379
  • 1
  • 20
  • 32
2
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();

    // Write your code here.

    System.out.println("String: " + s);
    System.out.println("Double: " + d);
    System.out.println("Int: " + i);
  }
}
zx485
  • 24,099
  • 26
  • 45
  • 52
1
import java.util.Scanner;

public class Solution {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int i = sc.nextInt();
        sc.nextLine();
        double d = sc.nextDouble();
        sc.nextLine();
        String s = sc.nextLine();

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

To take three lines of input: int,double and String

Seamus
  • 3,733
  • 2
  • 30
  • 39
1

If you use the nextLine() method immediately following the nextInt() method, recall that the nextInt() reads integer tokens; 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).

That's why the console asks the user to input the second string, so you can use

Scanner md = new Scanner(System.in);
int n      = md.nextInt();  //before using the input stream for string
String st  = md.nextLine();
zx485
  • 24,099
  • 26
  • 45
  • 52
mayankD
  • 36
  • 3