Java scanner not working on eclipse

2019-10-06 18:13发布

问题:

The program compiles and runs, but it is not allowing me to input any value. The program just ends without letting me input anything. Why?

package prac;

import java.util.Scanner;

    public class prac {

        public static void main(String args[]) {

            Scanner input = new Scanner(System. in );
            int number;
            System.out.println("Input number: ");
            number = input.nextInt();
            System.out.println(number);


        }

    }

回答1:

Your probably not getting focus on the console. AFter you run the java program switch to the console.



回答2:

Looks like you are missing a close statement for Scanner

try:

number = input.nextInt();
in.close();


回答3:

I believe what's happening is (I may be wrong) it is executing all the code and then closing so fast that you couldnt possibly input anything.



回答4:

If you want to give arguments on RunTime , right click on java file go to Run AS -> run configurations and you can pass arguments by clicking Arguments Tab ,using program variables option