How can I read input from the console using the Scanner class in Java?

前端 未结 15 1947
我寻月下人不归
我寻月下人不归 2020-11-21 06:19

How could I read input from the console using the Scanner class? Something like this:

System.out.println(\"Enter your username: \");
Scanner = i         


        
相关标签:
15条回答
  • 2020-11-21 06:55

    You can flow this code:

    Scanner obj= new Scanner(System.in);
    String s = obj.nextLine();
    
    0 讨论(0)
  • 2020-11-21 06:57
    import java.util.*;
    
    class Ss
    {
        int id, salary;
        String name;
    
       void Ss(int id, int salary, String name)
        {
            this.id = id;
            this.salary = salary;
            this.name = name;
        }
    
        void display()
        {
            System.out.println("The id of employee:" + id);
            System.out.println("The name of employye:" + name);
            System.out.println("The salary of employee:" + salary);
        }
    }
    
    class employee
    {
        public static void main(String args[])
        {
            Scanner sc = new Scanner(System.in);
    
            Ss s = new Ss(sc.nextInt(), sc.nextInt(), sc.nextLine());
            s.display();
        }
    }
    
    0 讨论(0)
  • 2020-11-21 07:00

    There are several ways to get input from the user. Here in this program we will take the Scanner class to achieve the task. This Scanner class comes under java.util, hence the first line of the program is import java.util.Scanner; which allows the user to read values of various types in Java. The import statement line should have to be in the first line the java program, and we proceed further for code.

    in.nextInt(); // It just reads the numbers
    
    in.nextLine(); // It get the String which user enters
    

    To access methods in the Scanner class create a new scanner object as "in". Now we use one of its method, that is "next". The "next" method gets the string of text that a user enters on the keyboard.

    Here I'm using in.nextLine(); to get the String which the user enters.

    import java.util.Scanner;
    
    class GetInputFromUser {
        public static void main(String args[]) {
            int a;
            float b;
            String s;
    
            Scanner in = new Scanner(System.in);
            System.out.println("Enter a string");
            s = in.nextLine();
            System.out.println("You entered string " + s);
    
            System.out.println("Enter an integer");
            a = in.nextInt();
            System.out.println("You entered integer " + a);
    
            System.out.println("Enter a float");
            b = in.nextFloat();
            System.out.println("You entered float " + b);
        }
    }
    
    0 讨论(0)
  • 2020-11-21 07:02

    You can make a simple program to ask for the user's name and print whatever the reply use inputs.

    Or ask the user to enter two numbers and you can add, multiply, subtract, or divide those numbers and print the answers for user inputs just like the behavior of a calculator.

    So there you need the Scanner class. You have to import java.util.Scanner;, and in the code you need to use:

    Scanner input = new Scanner(System.in);
    

    input is a variable name.

    Scanner input = new Scanner(System.in);
    
    System.out.println("Please enter your name: ");
    s = input.next(); // Getting a String value
    
    System.out.println("Please enter your age: ");
    i = input.nextInt(); // Getting an integer
    
    System.out.println("Please enter your salary: ");
    d = input.nextDouble(); // Getting a double
    

    See how this differs: input.next();, i = input.nextInt();, d = input.nextDouble();

    According to a String, int and a double varies the same way for the rest. Don't forget the import statement at the top of your code.

    0 讨论(0)
  • 2020-11-21 07:05

    Here is the complete class which performs the required operation:

    import java.util.Scanner;
    
    public class App {
        public static void main(String[] args) {
            Scanner input = new Scanner(System.in);
            final int valid = 6;
    
            Scanner one = new Scanner(System.in);
            System.out.println("Enter your username: ");
            String s = one.nextLine();
    
            if (s.length() < valid) {
                System.out.println("Enter a valid username");
                System.out.println(
                    "User name must contain " + valid + " characters");
                System.out.println("Enter again: ");
                s = one.nextLine();
            }
    
            System.out.println("Username accepted: " + s);
    
            Scanner two = new Scanner(System.in);
            System.out.println("Enter your age: ");
            int a = two.nextInt();
            System.out.println("Age accepted: " + a);
    
            Scanner three = new Scanner(System.in);
            System.out.println("Enter your sex: ");
            String sex = three.nextLine();
            System.out.println("Sex accepted: " + sex);
        }
    }
    
    0 讨论(0)
  • 2020-11-21 07:08

    Reading Data From The Console

    • BufferedReader is synchronized, so read operations on a BufferedReader can be safely done from multiple threads. The buffer size may be specified, or the default size(8192) may be used. The default is large enough for most purposes.

      readLine() « just reads data line by line from the stream or source. A line is considered to be terminated by any one these: \n, \r (or) \r\n

    • Scanner breaks its input into tokens using a delimiter pattern, which by default matches whitespace(\s) and it is recognised by Character.isWhitespace.

      « Until the user enters data, the scanning operation may block, waiting for input. « Use Scanner(BUFFER_SIZE = 1024) if you want to parse a specific type of token from a stream. « A scanner however is not thread safe. It has to be externally synchronized.

      next() « Finds and returns the next complete token from this scanner. nextInt() « Scans the next token of the input as an int.

    Code

    String name = null;
    int number;
    
    java.io.BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    name = in.readLine(); // If the user has not entered anything, assume the default value.
    number = Integer.parseInt(in.readLine()); // It reads only String,and we need to parse it.
    System.out.println("Name " + name + "\t number " + number);
    
    java.util.Scanner sc = new Scanner(System.in).useDelimiter("\\s");
    name = sc.next();  // It will not leave until the user enters data.
    number = sc.nextInt(); // We can read specific data.
    System.out.println("Name " + name + "\t number " + number);
    
    // The Console class is not working in the IDE as expected.
    java.io.Console cnsl = System.console();
    if (cnsl != null) {
        // Read a line from the user input. The cursor blinks after the specified input.
        name = cnsl.readLine("Name: ");
        System.out.println("Name entered: " + name);
    }
    

    Inputs and outputs of Stream

    Reader Input:     Output:
    Yash 777          Line1 = Yash 777
         7            Line1 = 7
    
    Scanner Input:    Output:
    Yash 777          token1 = Yash
                      token2 = 777
    
    0 讨论(0)
提交回复
热议问题