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

2012/11/1

How to use Scanner

1
2
kiarocks kiarocks

2012/11/4

#
Well, you could use my method but instead of making it a huge string, save each string to a list: In your class, define a new variable:
1
ArrayList<String> out = new ArrayList<String>();
Then, use this code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
public void load() throws Exception      
{   
    BufferedReader in = new BufferedReader(new FileReader("a.txt")); 
    int num = 0;   //File Line Count
    String t = ""
    int start = Integer.parseInt(in.readLine()); //First number in file
    while((t = in.readLine()) != null) //Ensures that there is something to read 
    {   
        num++; //Increase num by 1 
        //do stuff with the string 't' which has each line of file every time
        out.add(t); //Adds the contents of the line to the list
    }   
    in.close();   
}
To get something from the list, use this:
1
String thirdLine = out.get(2); //subtract one from wanted line.
CKL CKL

2012/11/5

#
I have fixed it, thank you so much.
You need to login to post a reply.
1
2