Get the App
SLTechnology News&Howtos  ›  Development  › 

How to input and output in Java

Shulou Source: shulou.com Published: 2022-06-01 00:06:36 10月13日 Update

Most people do not understand the knowledge points of this article "how to input and output in Java", so the editor summarizes the following contents, detailed contents, clear steps, and certain reference value. I hope you can get something after reading this article. Let's take a look at this "how to input and output in Java" article.

The first input method: scannerimport java.util.Scanner; / / Import java.util.Scannerpublic class User {public static void main (String [] args) {var sc = new Scanner (System.in); / / generate scanner object System.out.print ("Please enter your name:"); String name = sc.nextLine () / / enter the string System.out.print ("Please enter your age:"); int age = sc.nextInt (); / / enter the integer System.out.println ("name:" + name + "\ n" + "Age:" + age);}}

Running result

The commonly used next () method

NextInt () enter an integer

NextLine () input string

NextDouble () input double precision number

Next () input string with a space as a delimiter

The second input method: JOptionPane

Running result

The third input method: io.Console

This input method is a safe input method and can only be run in the cmd console, not in tools such as IDEA, eclipse, etc.

Create a User.java file

Import java.io.Console;// import java.io.Consolepublic class User {public static void main (String [] args) {Console con = System.console (); / / establish security input String name = con.readLine ("Please enter account:"); String passworld = con.readLine ("Please enter password:"); System.out.printf ("account:% s", name) System.out.printf ("% n password:% s", passworld);}}

Cmd goes to the directory where the file is located and runs the following instructions

Java-Dfile.encoding=UTF-8 User.java

Running result

Output method first output method: System.out.print ()

System.out.print () output directly without line wrapping

Public class User {public static void main (String [] args) {int a = 1; int b = 2; System.out.print (a); / / print () does not wrap the value of output a System.out.print (b); / / print () does not wrap the value of output b}}

Running result

twelve

The second output method: System.out.println ()

System.out.println () newline output

Public class User {public static void main (String [] args) {int a = 1; int b = 2; System.out.println (a); / / println () the value of the newline output a System.out.println (b); / / println () the value of the newline output b}}

Running result

one

two

The third output method: System.out.printf ()

System.out.printf () formatted output

Public class User {public static void main (String [] args) {int a = 1; int b = 2; System.out.printf ("a=%d%n", a); / / printf () formatted output System.out.printf ("baked% d", b); / / printf () formatted output}}

Running result

Axi1

Bread2

The above is about the content of this article on "how to input and output in Java". I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more related knowledge, please pay attention to the industry information channel.

Tags: Input output method run result content character string format security name password age integer file article knowledge article account value Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL Shulou Information Shulou Technology OPPO Reno Docker