-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHighScore.java
More file actions
57 lines (48 loc) · 1.79 KB
/
HighScore.java
File metadata and controls
57 lines (48 loc) · 1.79 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/*
https://programmingbydoing.com/a/high-score.html
Write a dumb little program that asks the user for their name and a number.
Then store that name and that "high score" in a file. The file should be called "score.txt".
Bugz: If the Int or String is anything but that, then the input ends up on the next line or cut off...
*/
import java.io.FileWriter;
import java.io.IOException;
import java.util.*;
public class HighScore {
public static void writeToFile(int score, String name){
try{
java.io.File myFile = new java.io.File("score.txt");
FileWriter writer = new FileWriter(myFile);
writer.write(String.valueOf(score) + "\n");
writer.write(name + "\n");
writer.close();
} catch (IOException io){
io.printStackTrace();
}
System.out.println("Value's stored are: The score is " + score + "\tName is " + name);
System.out.println("Data store into score.txt");
}
public static int getInt(int score, Scanner myScanner) {
// If number entered is not an int, then default is 0
System.out.print("Please enter your score: ");
if (myScanner.hasNextInt()) {
score = myScanner.nextInt();
}
return score;
}
public static String getString(String name, Scanner myScanner) {
System.out.print("Please enter your name: ");
if (myScanner.hasNext()) {
name = myScanner.next();
myScanner.close();
}
return name;
}
public static void main(String[] args) {
int score = 0;
String name = "SomeName";
Scanner myScanner = new Scanner(System.in);
score = getInt(score, myScanner);
name = getString(name, myScanner);
writeToFile(score, name);
}
}