Java/Development Class/Scanner
Содержание
- 1 Count all vowels inputed from keyboard
- 2 Demonstrate findInLine()
- 3 Letting the user decide when to quit
- 4 Read int by using Scanner Class
- 5 Test the input string in the while condition.
- 6 Use Scanner to compute an average a list of comma-separated values
- 7 Use Scanner to compute an average of the values
- 8 Use Scanner to compute an average of the values in a file
- 9 use Scanner to read input that contains several different types of data
- 10 Use Scanner to read user input
- 11 Use Scanner to read various types of data from a file
Count all vowels inputed from keyboard
import java.util.Scanner;
public class CountVowels {
static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
System.out.print("Enter a string: ");
String s = sc.nextLine();
int vowelCount = 0;
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if ((c == "A") || (c == "a") || (c == "E") || (c == "e") || (c == "I") || (c == "i")
|| (c == "O") || (c == "o") || (c == "U") || (c == "u"))
vowelCount++;
}
System.out.println("That string contains " + vowelCount + " vowels.");
}
}
Demonstrate findInLine()
/**
*Output:
28
*/
import java.util.Scanner;
public class MainClass {
public static void main(String args[]) {
String instr = "Name: Joe Age: 28 ID: 77";
Scanner conin = new Scanner(instr);
conin.findInLine("Age:"); // find Age
if (conin.hasNext())
System.out.println(conin.next());
else
System.out.println("Error!");
}
}
Letting the user decide when to quit
import java.util.Scanner;
public class MainClass {
static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
int number = 2;
String input;
while (true) {
System.out.println(number + " ");
System.out.print("Do you want to keep counting?" + " (Y or N)");
input = sc.next();
if (input.equalsIgnoreCase("N"))
break;
number += 2;
}
}
}
Read int by using Scanner Class
import java.util.Scanner;
public class MainClass {
static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
System.out.print("Enter an integer: ");
int x = sc.nextInt();
System.out.println("You entered " + x + ".");
}
}
Test the input string in the while condition.
import java.util.Scanner;
public class MainClass {
static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
int number = 2;
String input = "Y";
while (input.equalsIgnoreCase("Y")) {
System.out.println(number + " ");
System.out.print("Do you want to keep counting?" + " (Y or N)");
input = sc.next();
number += 2;
}
}
}
Use Scanner to compute an average a list of comma-separated values
/**
*Output:
Average is 6.2
*/
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
public class MainClass {
public static void main(String args[]) throws IOException {
int count = 0;
double sum = 0.0;
FileWriter fout = new FileWriter("test.txt");
fout.write("2, 3.4, 5,6, 7.4, 9.1, 10.5, done");
fout.close();
FileReader fin = new FileReader("Test.txt");
Scanner src = new Scanner(fin);
src.useDelimiter(", *");
while (src.hasNext()) {
if (src.hasNextDouble()) {
sum += src.nextDouble();
count++;
} else {
String str = src.next();
if (str.equals("done"))
break;
else {
System.out.println("File format error.");
return;
}
}
}
fin.close();
System.out.println("Average is " + sum / count);
}
}
Use Scanner to compute an average of the values
/**
*Output:
*/
import java.util.Scanner;
public class MainClass {
public static void main(String args[]) {
Scanner conin = new Scanner(System.in);
int count = 0;
double sum = 0.0;
System.out.println("Enter numbers to average.");
while (conin.hasNext()) {
if (conin.hasNextDouble()) {
sum += conin.nextDouble();
count++;
} else {
String str = conin.next();
if (str.equals("done"))
break;
else {
System.out.println("Data format error.");
return;
}
}
}
System.out.println("Average is " + sum / count);
}
}
Use Scanner to compute an average of the values in a file
/**
*Output:
Average is 6.2
*/
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
public class MainClass {
public static void main(String args[]) throws IOException {
int count = 0;
double sum = 0.0;
FileWriter fout = new FileWriter("test.txt");
fout.write("2 3.4 5 6 7.4 9.1 10.5 done");
fout.close();
FileReader fin = new FileReader("Test.txt");
Scanner src = new Scanner(fin);
while (src.hasNext()) {
if (src.hasNextDouble()) {
sum += src.nextDouble();
count++;
} else {
String str = src.next();
if (str.equals("done"))
break;
else {
System.out.println("File format error.");
return;
}
}
}
fin.close();
System.out.println("Average is " + sum / count);
}
}
use Scanner to read input that contains several different types of data
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
class ScanMixed {
public static void main(String args[]) throws IOException {
int i;
double d;
boolean b;
String str;
FileWriter fout = new FileWriter("test.txt");
fout.write("Testing Scanner 10 12.2 one true two false");
fout.close();
FileReader fin = new FileReader("Test.txt");
Scanner src = new Scanner(fin);
while (src.hasNext()) {
if (src.hasNextInt()) {
i = src.nextInt();
System.out.println("int: " + i);
} else if (src.hasNextDouble()) {
d = src.nextDouble();
System.out.println("double: " + d);
} else if (src.hasNextBoolean()) {
b = src.nextBoolean();
System.out.println("boolean: " + b);
} else {
str = src.next();
System.out.println("String: " + str);
}
}
fin.close();
}
}
Use Scanner to read user input
import java.util.Scanner;
class Console {
static Scanner sc = new Scanner(System.in);
public static boolean askYorN(String prompt) {
while (true) {
String answer;
System.out.print("\n" + prompt + " (Y or N) ");
answer = sc.next();
if (answer.equalsIgnoreCase("Y"))
return true;
else if (answer.equalsIgnoreCase("N"))
return false;
}
}
}
public class MainClass {
public static void main(String[] args) {
while (Console.askYorN("Keep going?")) {
System.out.println("!");
}
}
}
Use Scanner to read various types of data from a file
/**
*Output:
String: Testing
String: Scanner
int: 10
double: 12.2
String: one
boolean: true
String: two
boolean: false
*/
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
public class MainClass {
public static void main(String args[]) throws IOException {
int i;
double d;
boolean b;
String str;
FileWriter fout = new FileWriter("test.txt");
fout.write("Testing Scanner 10 12.2 one true two false");
fout.close();
FileReader fin = new FileReader("Test.txt");
Scanner src = new Scanner(fin);
while (src.hasNext()) {
if (src.hasNextInt()) {
i = src.nextInt();
System.out.println("int: " + i);
} else if (src.hasNextDouble()) {
d = src.nextDouble();
System.out.println("double: " + d);
} else if (src.hasNextBoolean()) {
b = src.nextBoolean();
System.out.println("boolean: " + b);
} else {
str = src.next();
System.out.println("String: " + str);
}
}
fin.close();
}
}