Anthony Berrios
Anthony Berrios

Reputation: 53

How to parse lines of code with multiple data types

Currently working on a program that reads in stats from football players and converts the data to binary and writes it all to a file. The problem I have been having is parsing all the different data types that the file I'm reading in contains. The file I am reading in will have the following format - lastName yearsExp position height weight 40ydSpeed team active? An example of what this will look like is the file is the following: Brady, 14, QB, 1, 210, 4.9, Patriots, true I am wondering how to go about parsing the different data types, int, char, double, String, and boolean.

So far my program asks the user to enter a file and it catches any FileNotFoundExceptions from invalid files and loops until a valid file is entered. Then the program reads the file and stores it all into a list.

public static void main(String[] args) throws IOException {
     File file;
     Scanner inputFile;
     Scanner readFile;
     String line;
     String fileName;
     int x = 1;

     ArrayList<String> stats = new ArrayList<String>();

    do {
        Scanner kb = new Scanner(System.in);
        System.out.println("Please enter the name of a " +
                "file containing football player data:");
        fileName = kb.nextLine();

        try {
            file = new File(fileName);
            inputFile = new Scanner(file);
            while(inputFile.hasNext()) {

                    stats.add(inputFile.nextLine());
            }
            x=2;

        }
        catch (FileNotFoundException e)
            {
            System.out.print("File not found. ");
            }
       }
    while(x==1);

    for (String s : stats) {
        System.out.println(s);
    }

    // TODO use the following methods for writing to binary
    // TODO writeUTF, writeInt, writeChar, writeInt, writeInt, writeDbl, writeByte

Upvotes: 1

Views: 693

Answers (3)

Frontear
Frontear

Reputation: 1261

You could split the input by the , or any other common delimiter separating them, then try to parse each one by one.

String input = "Tom, 15, 6, 86.4"; // name, age, grade, mark
String[] inputs = input.split(", "); // [ Tom, 15, 6, 86.4 ]

for (String in : inputs) {
    if (isInteger(in)) {
        // something
    } else if (isString(in)) {
        // other thing
    } // etc
}

Upvotes: 0

Andreas
Andreas

Reputation: 159165

how to go about parsing the different data types, int, char, double, String, and boolean

In order:

int x = Integer.parseInt(s)
char x = s.charAt(0)
double x = Double.parseDouble(s)
String x = s
boolean x = Boolean.parseBoolean(s)

Upvotes: 2

Mario Ishac
Mario Ishac

Reputation: 5907

The current problem is tha ArrayList<String> stats = new ArrayList<String>(); forces any of the values you read to be String. If you've learned about classes, then you can create a class that models a football player: let's call it FootballPlayer as an example.

In FootballPlayer you would have many fields of different types. You would have one for lastName that is a String, yearsExp that is an int, etc.

Then, here:

 while(inputFile.hasNext()) {
     stats.add(inputFile.nextLine());
 }

You could keep an index corresponding to what field you are trying to read. lastName could correspond to index 0, yearsExpr to 1, etc. Then, based on the index value (you could use a switch statement), you could call a different method of Scanner that returns the appropiate type, like nextInt(), if the index if 1 (for yearsExpr). After calling this appropiate method, you would assign the returned value to the corresponding field in your instance of FootballPlayer.

In the future, this whole process could be handled for you by using a library to parse the data format you're using, like a CSV parser (as it appears you data is comma-separated).

Upvotes: 0

Related Questions