Java console input

Source: Internet
Author: User

The input in the Java console is not as simple as in C or other languages, so many basic java tutorials are not described. However, it is very important, especially for students who are switching from C or C ++ to Java, so I want to discuss the input statements of Java. Of course, as a beginner, I am an expert in class, and I just think of some ideas.Role. I hope you can discuss it together and give me some advice!
First, Java. util. the scanner class in the scanner package. This class is a final class inherited from the object class. It can be seen from its class name that it is a bit similar to a scanner, therefore, it can only scan user input to the screen information, which requires a system. in and then scan (I think so ). Of course, it only scans characters, but can be converted to other types as needed. It provides many such methods: String next (), bigdecimal nextbigdecimal (), biginteger nextbiginteger () biginteger
Nextbiginteger (INT Radix), Boolean nextboolean (), byte nextbyte (), byte nextbyte (INT Radix), double nextdouble (), float nextfloat (), int nextint () int nextint (INT Radix), string nextline (), long nextlong (), long nextlong (INT Radix), short nextshort (), short
Nextshort (INT Radix ). All of these methods can obtain corresponding data types. Example

For example:
Import java. util. collections;
Public class importtext {
Public static void main (string [] ARGs ){
Pipeline SC = new pipeline (system. In );
Int I = SC. nextint ();
System. Out. println (I );
}
}
In this example, an integer data is obtained.
(There are still a lot of things about NLP. I don't know much about it. I only know some simple and more complex things. Please give me some advice !)
In addition, this bufferedreader class reads text from Character Input streams and caches various characters to provide efficient reading of characters, arrays, and rows (from the Java help documentation ), therefore, it requires a buffer. Two methods are used to create a buffer: bufferedreader (Reader in) to create a buffer character input stream that uses the default size of the input buffer. Bufferedreader (Reader in, int sz) creates a buffer character input stream that uses the specified size input buffer. Similarly, it reads strings and needs to be processed to convert strings into integer, floating point, and other types. The type conversion I have seen involves the integer. parseint () function, which is a method of static class integer parse.
Think about parsing, so you can understand it. I have also seen float. parsefloat (). For example:
Import java. Io .*;
Public class importtext {
Public static void main (string [] ARGs ){
String st;
Int num;
Float fnum;
Try {
System. Out. Print ("input :");
Bufferedreader BR = new bufferedreader (New inputstreamreader (system. In ));
St = Br. Readline ();
System. Out. Print ("enter a number :");
Num = integer. parseint (Br. Readline ());
System. Out. Print ("enter a floating point :");
Fnum = float. parsefloat (Br. Readline ());
System. Out. Print ("output:" + st + '/N ');
System. Out. Print ("output:" + num + '/N ');
System. Out. Print ("output:" + fnum + '/N ');
} Catch (ioexception e ){}
}
}
Another way is to run the Add parameter method, because I didn't use the DOS interface to input commands, so it's useless, but I just checked this method when I heard a lot of senior students say. It's also novel, let me talk about it, because the main function of Java has parameters (I have also seen the main function of C ++ with parameters, but I can't add parameters myself, I don't know why, you can give me some advice! Thank you), argS [] is a string array that can receive data. After you compile the program, compile it into a bytecode file, and then add the real parameters to the runtime. [Java file name parameters (
String). This is not an example.
This is a small summary of my learning about Java. I hope you will not hesitate to give me some advice. Thank you.

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.