Click here to Skip to main content
14,924,665 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I've just learned to use Scanner class to take user input during execution. However, on practicing this, I'm facing an issue.
Here is my code

Java
import java.util.Scanner;
public class a
{
public static void main()
{
    Scanner b = new Scanner(System.in);
    System.out.println("Enter the integer");
    int c = b.nextInt();
    System.out.println("Enter the string");
    String d = b.nextLine();
    System.out.println();
    System.out.println(" The integer you entered is" + a);
    System.out.println(" The string you entered is "+b);
    }
}


What I have tried:

On running the program, it's taking input of integer and printing accordingly but it isn't asking for the input for String and printing a blank string directly.

However, if i were to completely eliminate the taking input from integer and printing it, then my program correctly takes input of String and prints. Why can't the both be done together?

Can someone please help?!
Posted
Updated 1-Jul-18 18:24pm

Try add "b.nextLine();" right after int c = b.nextInt(); so that it will read the new line character after the integer.

Java
import java.util.Scanner;
public class MyClass {
    public static void main(String args[]) {
       Scanner b = new Scanner(System.in);
        System.out.println("Enter the integer");
        int c = b.nextInt();
        b.nextLine();
        System.out.println("Enter the string");
        String d = b.nextLine();
        System.out.println("Enter the integer part 2");
        int e = b.nextInt();
        b.nextLine();
        System.out.println("Enter the string part 2");
        String f = b.nextLine();
        System.out.println();
        System.out.println(" The integer you entered is " + c);
        System.out.println(" The string you entered is "+d);
         System.out.println(" The integer you entered is " + e);
        System.out.println(" The string you entered is "+f);
    }
}


Java ask String input with multiple words - Stack Overflow[^]
   
What magic are you using ?
Java
    int c = b.nextInt(); // you read the integer in c
...
    System.out.println(" The integer you entered is" + a); // and you print a

Java
    String d = b.nextLine(); // you read the next line in d
...
    System.out.println(" The string you entered is "+b); // and you print b

you will certainly get advantage of learning the debugger.

Your code do not behave the way you expect, and you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.
Debugger - Wikipedia, the free encyclopedia[^]
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]
The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900