How can I get the user input in Java?

2018-12-31 02:52发布

I attempted to create a calculator, but I can not get it to work because I don't know how to get user input.

How can I get the user input in Java?

24条回答
查无此人
2楼-- · 2018-12-31 03:29

One of the simplest ways is to use a Scanner object as follows:

import java.util.Scanner;

Scanner reader = new Scanner(System.in);  // Reading from System.in
System.out.println("Enter a number: ");
int n = reader.nextInt(); // Scans the next token of the input as an int.
//once finished
reader.close();
查看更多
梦醉为红颜
3楼-- · 2018-12-31 03:29

Add throws IOException beside main(), then

DataInputStream input = new DataInputStream(System.in);
System.out.print("Enter your name");
String name = input.readLine();
查看更多
弹指情弦暗扣
4楼-- · 2018-12-31 03:29

Here is a more developed version of the accepted answer that addresses two common needs:

  • Collecting user input repeatedly until an exit value has been entered
  • Dealing with invalid input values (non-integers in this example)

Code

package inputTest;

import java.util.Scanner;
import java.util.InputMismatchException;

public class InputTest {
    public static void main(String args[]) {
        Scanner reader = new Scanner(System.in);
        System.out.println("Please enter integers. Type 0 to exit.");

        boolean done = false;
        while (!done) {
            System.out.print("Enter an integer: ");
            try {
                int n = reader.nextInt();
                if (n == 0) {
                    done = true;
                }
                else {
                    // do something with the input
                    System.out.println("\tThe number entered was: " + n);
                }
            }
            catch (InputMismatchException e) {
                System.out.println("\tInvalid input type (must be an integer)");
                reader.nextLine();  // Clear invalid input from scanner buffer.
            }
        }
        System.out.println("Exiting...");
        reader.close();
    }
}

Example

Please enter integers. Type 0 to exit.
Enter an integer: 12
    The number entered was: 12
Enter an integer: -56
    The number entered was: -56
Enter an integer: 4.2
    Invalid input type (must be an integer)
Enter an integer: but i hate integers
    Invalid input type (must be an integer)
Enter an integer: 3
    The number entered was: 3
Enter an integer: 0
Exiting...

Note that without nextLine(), the bad input will trigger the same exception repeatedly in an infinite loop. You might want to use next() instead depending on the circumstance, but know that input like this has spaces will generate multiple exceptions.

查看更多
只若初见
5楼-- · 2018-12-31 03:30

Here is your program from the question using java.util.Scanner:

import java.util.Scanner;

public class Example {
    public static void main(String[] args) {
        int input = 0;
        System.out.println("The super insano calculator");
        System.out.println("enter the corrosponding number:");
        Scanner reader3 = new Scanner(System.in);
        System.out.println(
            "1. Add | 2. Subtract | 3. Divide | 4. Multiply");

        input = reader3.nextInt();

        int a = 0, b = 0;

        Scanner reader = new Scanner(System.in);
        System.out.println("Enter the first number");
        // get user input for a
        a = reader.nextInt();

        Scanner reader1 = new Scanner(System.in);
        System.out.println("Enter the scend number");
        // get user input for b
        b = reader1.nextInt();

        switch (input){
            case 1:  System.out.println(a + " + " + b + " = " + add(a, b));
                     break;
            case 2:  System.out.println(a + " - " + b + " = " + subtract(a, b));
                     break;
            case 3:  System.out.println(a + " / " + b + " = " + divide(a, b));
                     break;
            case 4:  System.out.println(a + " * " + b + " = " + multiply(a, b));
                     break;
            default: System.out.println("your input is invalid!");
                     break;
        }
    }

    static int      add(int lhs, int rhs) { return lhs + rhs; }
    static int subtract(int lhs, int rhs) { return lhs - rhs; }
    static int   divide(int lhs, int rhs) { return lhs / rhs; }
    static int multiply(int lhs, int rhs) { return lhs * rhs; }
}
查看更多
大哥的爱人
6楼-- · 2018-12-31 03:30

I like the following:

public String readLine(String tPromptString) {
    byte[] tBuffer = new byte[256];
    int tPos = 0;
    System.out.print(tPromptString);

    while(true) {
        byte tNextByte = readByte();
        if(tNextByte == 10) {
            return new String(tBuffer, 0, tPos);
        }

        if(tNextByte != 13) {
            tBuffer[tPos] = tNextByte;
            ++tPos;
        }
    }
}

and for example, I would do:

String name = this.readLine("What is your name?")
查看更多
有味是清欢
7楼-- · 2018-12-31 03:33
import java.util.Scanner; 

class Daytwo{
    public static void main(String[] args){
        System.out.println("HelloWorld");

        Scanner reader = new Scanner(System.in);
        System.out.println("Enter the number ");

        int n = reader.nextInt();
        System.out.println("You entered " + n);

    }
}
查看更多
登录 后发表回答