Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsing txt file

I have to write a program that will parse baseball player info and hits,out,walk,ect from a txt file. For example the txt file may look something like this: Sam Slugger,h,h,o,s,w,w,h,w,o,o,o,h,s Jill Jenks,o,o,s,h,h,o,o Will Jones,o,o,w,h,o,o,o,o,w,o,o

I know how to parse the file and can get that code running perfect. The only problem I am having is that we should only be printing the name for each player and 3 or their plays. For example: Sam Slugger hit,hit,out Jill Jenks out, out, sacrifice fly Will Jones out, out, walk

I am not sure how to limit this and every time I try to cut it off at 3 I always get the first person working fine but it breaks the loop and doesn't do anything for all the other players.

This is what I have so far:

import java.util.Scanner;
import java.io.*;

public class ReadBaseBall{

public static void main(String args[]) throws IOException{

    int count=0;
    String playerData;
    Scanner fileScan, urlScan;

    String fileName = "C:\\Users\\Crust\\Documents\\java\\TeamStats.txt";
    fileScan = new Scanner(new File(fileName));

    while(fileScan.hasNext()){

        playerData = fileScan.nextLine();
        fileScan.useDelimiter(",");

        //System.out.println("Name: " + playerData);

        urlScan = new Scanner(playerData);
        urlScan.useDelimiter(",");


        for(urlScan.hasNext(); count<4; count++)

            System.out.print(" " + urlScan.next() + ",");

        System.out.println();

        }
    }
}

This prints out: Sam Slugger, h, h, o, but then the other players are voided out. I need help to get the other ones printing as well.

like image 607
cweidner55 Avatar asked Nov 10 '22 15:11

cweidner55


1 Answers

Here, try this one using FileReader Assuming your file content format is like this

 Sam Slugger,h,h,o,s,w,w,h,w,o,o,o,h,s
 Jill Johns,h,h,o,s,w,w,h,w,o,o,o,h,s

with each player in the his/her own line then this can work for you

 BufferedReader reader;
    try {
        reader = new BufferedReader(new FileReader(new File("file.txt")));
        String line = "";
        while ((line = reader.readLine()) != null) {
            String[] values_per_line = line.split(",");
            System.out.println("Name:" + values_per_line[0] + " "
                    + values_per_line[1] + " " + values_per_line[2] + " "
                    + values_per_line[3]);
            line = reader.readLine();
        }
        reader.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

otherwise if they are lined all in like one line which would not make sense then modify this sample.

 Sam Slugger,h,h,o,s,w,w,h,w,o,o,o,h,s| John Slugger,h,h,o,s,w,w,h,w,o,o,o,h,s

 BufferedReader reader;
    try {
        reader = new BufferedReader(new FileReader(new File("file.txt")));
        String line = "";
        while ((line = reader.readLine()) != null) {
            // token identifier is a space
            String[] data = line.trim().split("|");
            for (int i = 0; i < data.length; i++)
                System.out.println("Name:" + data[0].split(",")[0] + " "
                        + data[1].split(",")[1] + " "
                        + data[2].split(",")[2] + " "
                        + data[3].split(",")[3]);
            line = reader.readLine();
        }
        reader.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
like image 187
Zuko Avatar answered Nov 15 '22 12:11

Zuko