Java by API/java.util/Scanner
Содержание
- 1 new Scanner(FileReader file)
- 2 new Scanner(InputStream source)
- 3 new Scanner(Readable source)
- 4 new Scanner(String instr)
- 5 Scanner: findInLine(String str)
- 6 Scanner: findWithinHorizon(String pattern, int horizon)
- 7 Scanner: hasNext()
- 8 Scanner: hasNextBoolean()
- 9 Scanner: hasNextDouble()
- 10 Scanner: hasNextInt()
- 11 Scanner: hasNextLine()
- 12 Scanner: next()
- 13 Scanner: nextBoolean()
- 14 Scanner: nextDouble()
- 15 Scanner: nextInt()
- 16 Scanner: nextLine()
- 17 Scanner: useDelimiter(String pattern)
new Scanner(FileReader 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();
}
}
new Scanner(InputStream source)
/**
*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);
}
}
new Scanner(Readable source)
/*
*/
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);
}
}
new Scanner(String instr)
/**
*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!");
}
}
Scanner: findInLine(String str)
/**
*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!");
}
}
Scanner: findWithinHorizon(String pattern, int horizon)
import java.util.Scanner;
public class Main {
public static void main(String args[]) {
Scanner sc = new Scanner("Name: Tom Age: 28 ID: 77");
sc.findWithinHorizon("ID:",100);
if (sc.hasNext())
System.out.println(sc.next());
else
System.out.println("Error!");
}
}
Scanner: hasNext()
/**
*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);
}
}
Scanner: hasNextBoolean()
/*
String: string
boolean: true
boolean: false
int: 1
int: 2
int: 3
double: 4.12
*/
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("string true false 1 2 3 4.12");
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();
}
}
Scanner: hasNextDouble()
/**
*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);
}
}
Scanner: hasNextInt()
/*
String: string
boolean: true
boolean: false
int: 1
int: 2
int: 3
double: 4.12
*/
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("string true false 1 2 3 4.12");
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();
}
}
Scanner: hasNextLine()
import java.io.File;
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws Exception{
File file = new File("data.txt");
Scanner scanner = new Scanner(file);
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
System.out.println(line);
}
}
}
Scanner: next()
/**
*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);
}
}
Scanner: nextBoolean()
/*
String: string
boolean: true
boolean: false
int: 1
int: 2
int: 3
double: 4.12
*/
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("string true false 1 2 3 4.12");
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();
}
}
Scanner: nextDouble()
/*
String: string
boolean: true
boolean: false
int: 1
int: 2
int: 3
double: 4.12
*/
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("string true false 1 2 3 4.12");
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();
}
}
Scanner: nextInt()
/*
String: string
boolean: true
boolean: false
int: 1
int: 2
int: 3
double: 4.12
*/
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("string true false 1 2 3 4.12");
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();
}
}
Scanner: nextLine()
import java.io.File;
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws Exception{
File file = new File("data.txt");
Scanner scanner = new Scanner(file);
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
System.out.println(line);
}
}
}
Scanner: useDelimiter(String pattern)
/*
*/
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);
}
}