search
HomeJavajavaTutorialjava gets the value entered by keyboard

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); 
}

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); 
}

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); 
}

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());
              }
       }
}

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!

Statement
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.