2

How would I implement Bufferedreader to read in a file with multiple fields? I understand that br reads line by line, but the format of the file is making it difficult for me to understand because each line varies in length. Here is an example of the text format. How do I get bufferedReader to interpret the 4 variable line every time it occurs? Then the 2 variabled lines that proceed afterward?

var0// number of test cases

var1 var2 var3 var4 

num1 num2

num3 num4

... ...

... ...

var6 var7 var8 var9

... ...

... ...

...

...

Here is my basic BufferedReader code that reads the file

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;

public class FileHandling {

    public static void main(String[ ] args) {

        Scanner scanner = new Scanner(System.in);
        BufferedReader br = null;
        String line;

        //System.out.println("New.txt");

        try{
            br = new BufferedReader(new FileReader("Part1.txt"));
        } catch (FileNotFoundException fileex)  {
            System.out.println(fileex.getMessage() + "This File Was Not Found");
            System.exit(0);
        }

        //Read the lines

        try {
            while((line = br.readLine()) != null){
            System.out.println(line);
    }
        } catch (IOException ioex) {
            System.out.println(ioex.getMessage() + "Error Reading File");
        }finally {
            System.exit(0);
        }

}

A few things to note. I understand this website and its uses. I've thoroughly researched for hours through other similar questions on this site. I've tried khan academy, youtube, everything I could think to solve my issue. I'm not looking for anyone to do my work for me. I'm just a lost CS student looking for tips in the right direction. My java skills are weak so maybe I'm just not approaching the problem in a way some one else could explain better. I appreciate any advice you all can give me. Thanks.

Andy Turner
  • 137,514
  • 11
  • 162
  • 243
Sam F
  • 21
  • 1
  • 3
    You're looking for `String.split`. Split the line up on the spaces. – Andy Turner Nov 18 '16 at 21:15
  • 2
    Or use `java.util.Scanner`. – user207421 Nov 18 '16 at 21:16
  • Going off of what @AndyTurner said, you want to use `String.split(' ')`, this will take each line and split on a space and then you can store it in an Array. Here's an example, on how to use it: https://stackoverflow.com/questions/3481828/how-to-split-a-string-in-java – Matthew Brzezinski Nov 18 '16 at 21:18

1 Answers1

0

Expanding on the comments some more, this code will split each line on a space character, and store the results inside of an array. From there you can manipulate the data, for example if your input will be:

John Goodman
Kate Beckinsale
George Foreman

You can set a variable String firstName = array[0]; and String lastName = array[1];

Here's the code that you would need to change from your example,

try {
    while((line = br.readLine()) != null){
    String[] splitLine = line.split(' ');
    for(String str : splitLine) {
        // Do something with each string
        System.out.println(str);
    }
}
Matthew Brzezinski
  • 1,685
  • 4
  • 29
  • 53