'Is there a way of writing and reading two linked lists from a single text file in Java?
I am writing a program in java, which involves saving the user's name and their high score. The score would be saved on an even line and the name on the odd. For example:
Horace
2203
Rufus
435
Bertie
4725
Lawrence
174
Kane
...
Would this be possible? Would any libraries need to be imported? And would the text file need to be inside of the project in eclipse?
Thank you ever so much.
So far I have created the two lists:
LinkedList<String> listName = new LinkedList<String>();
LinkedList<Integer> listScore = new LinkedList<Integer>();
And saved the data to them:
listName.add(answer);
listScore.add(score);
Solution 1:[1]
It would be possible, but conceptually: that is the wrong approach.
You see, the information that you have there belongs together. I guess you imagine to create
A) List<String> players
and
B) List<Integer> scores
for example.
And then "same index" would mean: the score for player X
Don't do that. Instead create a Player class that has two attributes (name and score); and then use/fill a List<Player>
But beyond the question how you model your data; things are really easy:
open your file
loop:
read one line --- which should contain a String (name)
read one line --- which should contain a number
As you can see from that pseudo-code; there is really no magic in there. You know the structure that your data has; so just work with that!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
