Java Learning notes-----scanner Usage

Source: Internet
Author: User

Transferred from: http://blog.sina.com.cn/s/blog_7014ad5c01018sov.html

Let's look at a simple example:
Import java.util.*;
public class Scannertest {

public static void Main (string[] args) {
Scanner scanner=new Scanner (system.in);
Double a=scanner.nextdouble ();
System.out.println (a);
}
}
Run
Enter an arbitrary number and then output this number

Note where the bold word is, this line implements the ability to enter numbers from the console if you want to enter a string
can be used
String A=scanner.next ();//Note not nextstring ()

Scanner can also scan files directly. For example (a little bit longer, be patient):
Import java.util.*;
Import java.io.*;
public class Scannertest {
public static void Main (string[] args) throws ioexception{//here involves file IO operations
Double sum=0.0;
int count=0;
FileWriter fout=new FileWriter ("Text.txt");
Fout.write ("2 2.2 3 3.3 4 4.5 done");//write this string to the file
Fout.close ();
FileReader fin=new FileReader ("Text.txt");
Scanner scanner=new Scanner (Fin);//Note The parameter here is the FileReader type of fin
while (Scanner.hasnext ()) {//If there is content
if (scanner.hasnextdouble ()) {//if the number
Sum=sum+scanner.nextdouble ();
count++;
}else{
String Str=scanner.next ();
if (Str.equals ("Done")) {
Break
}else{
SYSTEM.OUT.PRINTLN ("file format is wrong!");
Return
}
}
}
Fin.close ();
System.out.println ("The average number of data in the file is:" +sum/count);
}
}
The average number of data in the resulting output file is: 3.1666666666666665
The function of this program is to write "2 2.2 3 3.3 4 4.5 Done" to the file scanner read the number of files until finished. And find out the average of the numbers, it is interesting that scanner will automatically use a space as a separator to distinguish between different numbers. Of course, you can also use Scanner.usedelimiter (pattern pattern) to set different delimiters, such as Scanner.usedelimiter (", *");
===========================================================================================================

Java code
1. Summary of Java.util.Scanner
2.//Construction method (three commonly used)
3.//scanner (File Source)
4.//scanner (InputStream Source)
5.//scanner (String Source)
6.
7.//Comparison between the two methods
8.//scanner sc=new Scanner (system.in);
9.//bufferedreader br=new BufferedReader (New InputStreamReader (system.in))
10.
11.//Method
//usedelimiter (pattern pattern) changes the way tokens are split, by default, by spaces, by the pattern object
//usedelimiter (string pattern) changes the way tokens are split, by default a space, a string
14.
//hasnext (); see if there is a token split segment
//hasnextint (); see if there is a split segment for tokens of type int
//nextint (); Returns the value of the next int
//nextline (); return a row
19.
//hasnext (pattern pattern); Returns the token of the next pattern type
21st.
public class Scannertest {
. public static void Main (string[] args) {
String str = "1.1 22.2 s 4 5.3 6 7.5 8 9";
Scanner Scanner = new Scanner (str);
//scanner.usedelimiter ("\ \");
while (Scanner.hasnext ()) {
if (Scanner.hasnext (Pattern.compile ("\\d\\.\\d")) {
System.out.println (Scanner.next ());
}else{.
Scanner.next ();//To Invoke next () related method to the next token
32.}
33.}
34.}
35.}
36.
37. Results:
38.1.1
39.5.3
40.7.5
41.
42.
public class Scannertest {
The public static void main (string[] args) {
String str = "1.2 S.4 5 6.7 8 9";
Scanner Scanner = new Scanner (str);
//token. Splitting
Scanner.usedelimiter ("\ \");
The. while (Scanner.hasnext ()) {
System.out.println (Scanner.next ());
51.}
52.}
53.}

Finally, scanner is rarely used in Java development, as it is not necessary to enter a number dynamically

Java Learning Note-----Scanner usage (GO)

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.