0

I can't figure out what I'm doing wrong here.

My code:

import java.util.Scanner;

public class test
  {
    public static void main(String args[])
      {
        Scanner input_scanner = new Scanner(System.in);
        System.out.printf("\nEnter number:");

        int num_shapes = input_scanner.nextInt();

        System.out.printf("\n%d", num_shapes);
      }
  }

Each time the program is run, I enter an integer, press the enter key, am taken to a new line, enter a new integer, and hit the enter key again. The first integer is then displayed.

How can I get it to display the first integer directly after it is entered, without having to enter a second integer?

I've tried it with and without

    input_scanner.nextLine();

following the line containing 'nextInt()', but get the same thing either way.

Any help in resolving this problem is greatly appreciated.

6
  • Its working perefectly no need to enter second number Commented Oct 23, 2013 at 5:20
  • 1
    I can't reproduce the issue with your code; are you sure that it's this exact code that's showing the issue? Commented Oct 23, 2013 at 5:20
  • Its working perfectly in my case too.... Commented Oct 23, 2013 at 5:21
  • it is working fine in my ide Commented Oct 23, 2013 at 5:24
  • works fine... you need a break, call your GF!!! Commented Oct 23, 2013 at 5:29

1 Answer 1

1

this works perfectly

import java.util.Scanner;

public class mainclass{

   public static void main(String args[])
     {
       Scanner input_scanner = new Scanner(System.in);
       System.out.println("Enter number:");

       int num_shapes = input_scanner.nextInt();

       System.out.println(num_shapes);
     }
 }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.