0

I have a text file which is a game save for my java game (cookie clickers) so the stuff in the file will be numbers without spaces. Like so:

10 20 30 40 50 

I need to read the lines and save each one to its string.

So the strings should be like this, so I can use them a lot easier:

lives = 10 kills = 20 score = 30 ... 

The saving code will be in its class file (Save.class). I only need the code, other stuff should not be a problem.

Is there some kind of easy way to make it work as I want?

10
  • Each line should be stored as a separate variable? (Or in a List<String>?) Commented Jun 13, 2015 at 21:41
  • As a separate variable please. Commented Jun 13, 2015 at 21:42
  • How are you supposed to know how many variables to use? Commented Jun 13, 2015 at 21:44
  • @TomLenc, are your variables of type String or int? Commented Jun 13, 2015 at 21:45
  • I can convert string to ints and ints to strings so.. :D and thanks for trying to help.. Already got the solution :/. Commented Jun 13, 2015 at 21:51

6 Answers 6

1

You can use a Scanner as follows:

Scanner s = new Scanner(new File("your file")); String lives = s.nextLine(); String kills = s.nextLine(); String score = s.nextLine(); ... s.close(); 
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, man! I forgot about this.. I couldn't find this anywhere. Thank you again :).
I don't understand the downvoters. I believe they haven't even tried your code out. This code is perfect and the easiest for what I'm working on.
I bet the downvoters think you should be using a list. That's why I wanted you to confirm whether my edit to your question was reasonable or not.
0

Scanner is good here.

List<String> list = new LinkedList<>(); Scanner scan = new Scanner(new File("the_file")); while (scan.hasNextLine()) list.add(scan.nextLine()); 

1 Comment

Each line should be stored in a separate variable. Not all lines in a list.
0

I recommend you to use an ArrayList. Like this:

Scanner s = new Scanner(new File(//Here the path of your file)); ArrayList<String> list = new ArrayList<String>(); while (s.hasNext()) { list.add(s.nextLine()); } 

Now you will have stored all the lines of your file so you can access each of them like this:

for(int i = 0; i < list.size(); i++) { System.out.println("The content of the line " + i + " it's " + list.get(i); } 

I expect it will be helpful for you!

4 Comments

Each line should be stored in a separate variable. Not all lines in a list.
@aioobe but in this way instead of the System.out.println you can store there all the variables (in an array or whatever you want).
Yes, but this happens to not be what the OP wants.
@aioobe but I think that, by this method, it will be easier to use them. Look that the OP saids that he wants variables that are "line1", "line2","line3" so here he don't need to create any variable. Just to access to the line by get(1), get(2),get(3). Also, he asked for the best way to read from files and I think it is the easier and faster, instead of create 1000 variables.
0
BufferedReader reader = new BufferedReader(new FileReader(FILE_PATH)); String line = null; int index = 0; while((line = reader.readLine()) != null) { if(index == 0) { line1 = line; } else if(index == 1) { line2 = line; } else if(index == 2) { line3 = line; } index++; } 

or you could read the text and save it as string array
also you might would like to use this http://ini4j.sourceforge.net

3 Comments

Why would you put it in a loop? Why not line1 = reader.readLine(); line2 = reader.readLine();...?
Yeah sorry, but isn't using ini is much better than all of this?
I did not understand that question.
0

Use readAllLines:

 File data = new File("textfile.txt"); List<String> lines = Files.readAllLines(data.toPath()); 

4 Comments

Each line should be stored in a separate variable. Not all lines in a list.
Yes, I understand that the OP think so - which is probably just because he is not used to Lists yet. Others that find this question will be helped by this answer.
@aioobe You did not actually believe that, did you? ;-)
Haha, I assumed since OP had not created a list, line1, line2, ... were placeholders for his actual variables, such as livesLeft, kills, score, ...
0

This way of saving game state smells. It seems that you're on your way on reinventing the wheel. Why don't you use a properties file where you can save the key(lives, kills, score) and the value ? It would make the code a lot more readable.

String gameStateFile = "gameState.properties"; Properties gameProperties = new Properties(); gameProperties.load(input); String lives = gameProperties.get("lives"); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.