Input usage in Java

Source: Internet
Author: User

Input usage in Java
Input in Java
1. Read the input. 1) construct a token and attach it to System. inspecin = new partition (System. in). in this case, you can use various methods of the category class to read the input. For example, the nextLine method reads a row of input System. out. print ("What is your name? "); String name = in. nextLine ();
NextLine (): The read row may contain spaces. next (): read a single word. NextInt (): read an integer. NextDouble (): reads a floating point number.
Example: String firstName = in. next (); int age = in. nextInt ();
The handler class is included in the java. util package. Therefore, at the beginning of the program, enter import java. util .*;
Note: keystore is not suitable for reading passwords from terminals because the input text is visible to anyone. The Console class is introduced in Java SE6 for this purpose. Use the following code to read the password: Console cons = System. console (); String username = cons. readLine ("User Name:"); char [] passwd = cons. readPassword ("Password:"); for security reasons, password returns a character array instead of a string. The Console object is not as convenient as processing input. The Console object can only read one row at a time. There is no other way to read a single word or number.
For example:

import java.io.Console;import java.lang.System;import java.util.*;/** * This program demonstrates console input * @version 1.10 2014-8-5 * @author Administrator * */public class InputTest {public static void main(String[] args){Scanner in = new Scanner(System.in);// get first inputSystem.out.print("What is your name?");String name = in.nextLine();// get second inputSystem.out.print("How old are you?");int age = in.nextInt();// display output to consoleSystem.out.println("Hello, " + name + ". New year you will be " + (age + 1));Console  cons = System.console();if(cons != null){System.out.print("User Name:");String username = cons.readLine();System.out.print("Password: ");char[] passwd = cons.readPassword();}else{System.out.println("Console object is null");}}}

Running result:



From the above results, we can see that the input of the Console object must be run on an interactive terminal. If you run the Code directly in Eclipse, the console object is null.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.