Home > Java > javaTutorial > java gets the value entered by keyboard

java gets the value entered by keyboard

angryTom
Release: 2019-07-17 08:41:27
Original
3159 people have browsed it

In the process of program development, we often need to obtain the value entered on the keyboard. C language provides us with scanf(), and C provides us with cin(), a ready-made function to obtain the keyboard input value. So how does Java get the value entered on the keyboard? Let's introduce three methods to you.

Method 1: Receive a character from the console and then print it out

import java.io.*;public static void main(String [] args) throws IOException{ 
         System.out.print("Enter a Char:"); 
         char i = (char) System.in.read(); 
         System.out.println("your char is :"+i); 
}
Copy after login

Although this method achieves getting input from the keyboard Character, but System.out.read() can only obtain one character. At the same time, the type of the variable obtained can only be char. When we input a number and hope to get an integer variable, we also You have to modify the variable types, which is more troublesome.

Method 2: Receive a string from the console and print it out. In this question, we need to use the BufferedReader class and the InputStreamReader class

import java.io.*;public static void main(String [] args) throws IOException{ 
           BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 
           String str = null; 
           System.out.println("Enter your value:"); 
           str = br.readLine(); 
           System.out.println("your value is :"+str); 
}
Copy after login

so that we can get the string we input.

Method 3: I think this method is the simplest and most powerful, which is to use the Scanner class

import java.util.Scanner;public static void main(String [] args) { 
         Scanner sc = new Scanner(System.in); 
         System.out.println("请输入你的姓名:"); 
         String name = sc.nextLine(); 
         System.out.println("请输入你的年龄:"); 
         int age = sc.nextInt(); 
         System.out.println("请输入你的工资:"); 
         float salary = sc.nextFloat(); 
         System.out.println("你的信息如下:"); 
         System.out.println("姓名:"+name+"\n"+"年龄:"+age+"\n"+"工资:"+salary); 
}
Copy after login

If If you need to enter data separated by spaces in one line, then:

package iotest; 
import java.util.Scanner; 
public class ScannerTest { 
       public static void main(String[] args) {
              System.out.println("请输入5个整数,以空格分开,以回车结束:");              try{                     // 创建Scanner对象
                     Scanner scanner = new Scanner(System.in);                    
                     // 用于保存5个数字的数组
                     int a[] = new int[5];                    
                     for(int i=0;i<5;i++){                            // 把字符串转换为数字
                            a[i] = scanner.nextInt();
                            System.out.println(a[i]);
                     }
                    
              }catch(Exception e){
                     System.out.println("异常:"+e.toString());
              }
       }
}
Copy after login

The above two pieces of code have shown that the Scanner class only needs to do one thing whether it is for strings, integer data or float type variables. A small change can realize the function! Undoubtedly he is the most powerful!

But when using Scanner, you need to pay attention to the situation of nextInt() after nextLine().

For more related knowledge, please click: java tutorial

The above is the detailed content of java gets the value entered by keyboard. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template