When writing programs in Eclipse, if our variables need to be entered manually, we can use the scanner class.
Scanner class, a new utility for scanning input text. Because any data must be retrieved by the same pattern of capture groups or by using an index to retrieve various parts of the text. Then a method of retrieving a specific type of data item from the input stream can be used in combination. In this way, in addition to using regular expressions, the Scanner class can also arbitrarily analyze data of strings and primitive types (such as int and double). With Scanner, you can write a custom syntax analyzer for any text content you want to process.
Note: In Eclipse, you must import java.util.Scanner under the package at the beginning; otherwise the scanner cannot be called.
Listed as:
package com.lovo.homework;//Enter an A, then store the data from A to Z into the array in turn, and traverse and print out the lowercase characters from a to z. import java.util.Scanner;public class Homework2016_11_17_1 {public static void main(String[] args) {Scanner scn=new Scanner(System.in);System.out.println("Please enter string A:");String m=scn.nextLine();//Input string String y=m.toLowerCase();char chr=y.toCharArray()[0];//Convert string to character for (int i = 0; i < 26; i++) {System.out.println("chr["+i+"]="+chr);chr++;}}}Output:
Please enter the string A:
Enter a string and press Enter:
Please enter the string A:b/chr[0]=bchr[1]=ccr[2]=dchr[3]=ecr[4]=fchr[5]=gchr[6]=hchr[7]=ichr[8]=jchr[9]=kchr[10]=lchr[11]=mchr[12]=nchr[13]=ochr[14]=pchr[15]=qchr[16]=rchr[17]=schr[18]=tchr[19]=uchr[20]=vchr[21]=wchr[22]=xchr[23]=ychr[24]=zchr[25]={Except for strings, all other types can be entered. We just need to change the String in String m=scn.nextLine() to int to enter integers, and the same is true for others.
Tips:
The use of eclipse formats the Java code, and the shortcut key is: ctrl+shift+F .
The above code format is deliberately disrupted:
After formatting:
Isn't it much better?
Summarize
The above is the entire content of this article about the usage example of Java programming scanner class. I hope it will be helpful to everyone. Interested friends can continue to refer to other related topics on this site. If there are any shortcomings, please leave a message to point it out. Thank you friends for your support for this site!