Varyag
Varyag

Reputation: 696

Read lines from .txt and store into different integers

So i got the following code:

    BufferedReader ind = new BufferedReader(new FileReader("Billet_priser.txt"));
    String line = ind.readLine();
    String[] bits = line.split(" "); // opdel i bidder efter mellemrum

    line = ind.readLine();
    bits = line.split(" ");
    Ticket1 = Integer.parseInt(bits[1]);
    line = ind.readLine();
    line.split(" ");
    Ticket2 = Integer.parseInt(bits[1]);
    line = ind.readLine();
    line.split(" ");
    ...
    line = ind.readLine();
    line.split(" ");
    Ticketn = Integer.parseInt(bits[1]);
}

reading from the .txt file with the following text:

Ticket1 99
Ticket2 35
...
Ticketn 60

Trying to get the second bit after a space in each line to be stored in the ticket integers.

The problem is that it only stores the first read int "99" into all the ticket integers. I want it to read the next line after storing the first int into the first ticket, then read the next line and so on.

Upvotes: 0

Views: 66

Answers (3)

flakes
flakes

Reputation: 23624

Your implementation is a bit off. You want to make a container of tickets and use the loop to fill it.

List<Integer> tickets = new ArrayList<>();
try (BufferedReader ind = new BufferedReader(new FileReader("Billet_priser.txt")){
    String line = null;
    while ((line = ind.readLine()) != null) {
        String[] bits = line.split(" ");     
        tickets.add(Integer.parseInt(bits[1]));
    }
} 
catch (IOException e) {
    e.printStackTrace();
}

Upvotes: 1

Jonah Haney
Jonah Haney

Reputation: 521

You might want to use a Scanner object instead of BufferedReader. Construct it like this:

Scanner scannerObject = new Scanner(new File("Billet_priser.txt");

Then fix your while loop to do something more like this:

while (scannerObject.hasNextLine()){
    String[] line = scannerObject.nextLine().split(" ");
    int currentTicket = Integer.parseInt(line[1]);
}

You could also store the ticket numbers in an ArrayList constructed like this:

ArrayList<Integer> tickets = new ArrayList<>();

Then your while loop would look like this:

while (scannerObject.hasNextLine()){
    String[] line = scannerObject.nextLine().split(" ");
    tickets.add(Integer.parseInt(line[1]));
}

Upvotes: 0

David
David

Reputation: 218818

You keep using this bits value to get the number:

Ticket1 = Integer.parseInt(bits[1]);

But you only ever set it once from the first line:

String[] bits = line.split(" ");     
while (line != null) {
    // bits is never updated in here
}

It sounds like you want to simply repeat that line of code to update the bits variable:

line = ind.readLine();
bits = line.split(" ");
Ticket1 = Integer.parseInt(bits[1]);

(Note also that your loop doesn't make much sense, because it looks like you're manually reading every line with repeated lines of code instead of actually looping. The three lines of code above, or whatever constitutes one iteration of your loop, should only have to exist once. Loops are designed to repeat that task over and over.)

Upvotes: 2

Related Questions