-5

I am trying to make the program access a file name "highscore.txt", then:

  1. Write a new number in the file

  2. Get the lowest value of a list of numbers in the file.

I am new to Java so I barely know how to even access the file.

I got code that I believe should work, but when I run the program it says

Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Scanner.java:864)
    at java.util.Scanner.next(Scanner.java:1485)
    at java.util.Scanner.nextDouble(Scanner.java:2413)
    at game.game.main(game.java:91)
Java Result: 1

Here is line 84 - 94, and the code I got.

84: Writer wr = new FileWriter("highscore.txt");
85:            wr.write(tries);
86:            wr.close();
87:            lowest = tries;
88:            File file = new File("highscore.txt");
89: Scanner inputFile = new Scanner(file);
90: while (inputFile.hasNext()) {
91:    numb = inputFile.nextDouble();
92:    if (numb < lowest) lowest = number;
93: }
94: inputFile.close();
The Guy with The Hat
  • 10,836
  • 8
  • 57
  • 75
  • 1
    Welcome to Stack Overflow! This question looks like homework to me. While asking homework questions is perfectly fine, there are some good guidelines for asking homework questions here: [How do I ask and answer homework questions?](http://meta.stackexchange.com/a/10812). Summarized, they are: make an attempt to solve the problem yourself first; ask about specific problems with your existing code; let us know that the question is homework; make sure your class allows using Q&A for help; don't copy and paste an answer's code without first understanding what it does and how it works. – The Guy with The Hat Jul 26 '14 at 22:51
  • Even if this isn't homework, the rules "make an attempt to solve the problem yourself first" and "ask about _specific_ problems with your _existing_ code" still apply. – The Guy with The Hat Jul 26 '14 at 22:52
  • Just a suggestion, try overwriting the value in the file instead of writing a new line – Moddl Jul 26 '14 at 22:53
  • In addition to the homework guidelines above, make sure to ask you teacher to TA. They need to know what you are having trouble with in class so they can help you learn better. – markspace Jul 26 '14 at 22:54
  • Read all of the file, add your new entry, sort the entries, discard least, write out new file. – eckes Jul 26 '14 at 22:55
  • Actually, I am needing this to make a game. I program as a hobby. – Daniel Allen Jul 26 '14 at 23:03
  • Can you show the contents of your file? The next token you are trying to read with `inputFile.nextDouble()` probably isn't a double. – The Guy with The Hat Jul 27 '14 at 00:07

1 Answers1

0

Use the Collections class. It implements a static sort() method that does what you want: sorting numbers. A quick and dirty code would look like this (supposing you've got a number in each line):

String line = readLine();
while(line != null){
  numberList.add(Integer.parseInt(line));
  line = readLine();
}
Collections.sort(numberList);

Now just pick the first number from numberList.

Ander Juaristi
  • 328
  • 3
  • 12