Input in JAVA

1. Overview
   Input and output can be said to be the basic functions of a computer. As a language system, java is mainly implemented in accordance with the mode of stream. The flow of data is determined according to the direction of the computer. The data stream flowing into the computer is called the input stream (inputStream), and the data stream sent by the computer is called the output stream (outputStream).

In the Java language system, the main operations on the data stream are encapsulated in the java.io package, and the input and output operations of the computer can be realized through the classes in the java.io package. When writing input and output operation code, you need to use import statement to import the java.io package into the class where the application is located, and then you can use the classes and interfaces in java.io.

Second, the input statement
1. Use the Scanner class:

(1) Use the java.util package. import java.util.*;

(2) Construct the Scanner class object, which is attached to the standard input stream System.in . Scanner s = new Scanner( System.in );

(3) Commonly used next() method series: nextInt(): input integer nextLine(): input string nextDouble(): input double precision number next(): input string (with space as separator).

import java.util.*;  
public class DEMO_1 {  
    public static void main(String[] args){  
        Scanner s = new Scanner( System.in );  
        System.out.print("Enter your name:");  
        String name = s.nextLine();  
        System.out.print("Enter your age:");  
        int age = s.nextInt();  
        System.out.println("Name: " + name + " Age: " + age );  
        s.close(); //There will be a warning if the Scanner object is not closed  
    }  

Note: Line 1 of the code creates an object of the Scanner class, which is used for input. The following code takes a value from the console input and assigns it to the corresponding variable.

2.使用java.io.BufferedReader和java.io.InputStreamReader:

step:

(1) Use the java.io package. import java.io .*;

(2) Construct a BufferedReader class object, which is attached to the standard input stream System.in .

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

public class Sandbox4 extends Sandbox2{ public static void main(String[] args) { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String str = ; while(true){ try { str = br.readLine(); }catch(IOException e){ e.printStackTrace(); } if(str.equals(“END”))break; System.out.print(str); } } }













caution:

(1) read method: read a single character. Returns: the character read as an integer ranging from 0 to 65535 (0x00-0xffff), or -1 if the end of the stream has been reached;

(2) readLine method: read a text line. A line is considered terminated by one of the following characters: a newline ('\n'), a carriage return ('\r'), or a carriage return followed by a newline. Returns: a string containing the contents of the line, without any line terminators, or null if the end of the stream has been reached.

3 、 使用 java.io.Console :

public class Sandbox4 extends Sandbox2{ public static void main(String[] args) { Console console = System.console(); if (console == ) { throw new IllegalStateException(“Console is not available!”); } String str = ; while(true){ str = console.readLine(“请输入”); if(“END”.equals(str))break; System.out.println(str); } } }  输入问题解惑:













Note: The picture of this question comes from Baidu Know.

3. Output statement

There are four types of output statements in Java:

System.out.println(1111);//Print with newline
System.out.print(1111);//Print without newline
System.out.write(2222);//Byte output
System.out.printf("%+ 8.3f\n", 3.14);//Output in format
System.out.println(); is the most common output statement, it converts the content in parentheses into strings and outputs them to the output window (console), and Newline, when the output is a basic data type, it will be automatically converted into a string, if the output is an object, it will automatically call the toString(); method of the object, and output the return value to the console
System.out.print( ); is very similar to the first one, the difference is that the previous output will wrap after the output, and this command will not wrap after the output.
System.out.printf(); This method continues the output method of the C language, through formatted text and parameter list output.


Author: Mr. L 08
Source: CSDN
Original text: https://blog.csdn.net/baidu_41666198/article/details/79942661
Copyright statement: This article is an original article by the blogger, please attach the blog post link for reprinting!

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324126611&siteId=291194637