This site requires JavaScript, please enable it in your browser!
Greenfoot back
Limthu
Limthu wrote ...

2019/5/4

Reading int with prefix from a file

Limthu Limthu

2019/5/4

#
I want to save a my options.txt with a prefix like Volume: 50 right now it can only read it, if there is ony the numer.
danpost danpost

2019/5/4

#
As long as the format for each line is consistent, you can readLine, then Integer.parseInt(text.substring(text.indexOf(":")+2));
Limthu Limthu

2019/5/4

#
so this is what I've already imported and written:
import java.io.FileWriter;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.FileNotFoundException;
import java.util.List;
import java.util.ArrayList;
import java.util.Arrays;



try
        {
        BufferedReader input = new BufferedReader(new FileReader("options.txt"));
        volume = Integer.parseInt(input.readLine());
        Integer.parseInt(text.substring(text.indexOf(":")+2));
        input.close();
        }
is this what u meant? cause it's not working :c or do I have to replace 'text' with 'Volume'? I've tried that too, but there was still an Error..
danpost danpost

2019/5/4

#
I meant something like this for lines 16 and 17:
String text = input.readLine();
volume = Integer.parseInt(text.substring(text.indexOf(": ")+2));
Limthu Limthu

2019/5/5

#
perfect, It works, thank u so much! :3
You need to login to post a reply.