-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCapitalAndCountry.java
More file actions
41 lines (32 loc) · 1.08 KB
/
CapitalAndCountry.java
File metadata and controls
41 lines (32 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import java.io.File;
import java.util.Scanner;
import java.util.Random;
import java.io.BufferedReader;
import java.io.FileReader;
//import java.io.FileWriter;
import java.io.IOException;
//import java.io.PrintWriter;
import java.util.regex.Pattern;
public class CapitalAndCountry{
String capital;
String country;
void randomCityAndCountry(String path) throws IOException {
if (path == ""){path = "countries_and_capitals.txt";}
String[] records = new String[1000];
int n = 0;
FileReader fileReader = new FileReader(path);
BufferedReader bufferedReader = new BufferedReader(fileReader);
String textLine = bufferedReader.readLine();
do {
records[n] = textLine;
n++;
textLine = bufferedReader.readLine();
} while(textLine != null);
bufferedReader.close();
Random rand = new Random();
int random_index = rand.nextInt(n);
String[] output = records[random_index].split(Pattern.quote(" | "));
capital = output[1];
country = output[0];
}
}