简体   繁体   中英

Parsing input from a text file

I have a text file that contains employee information. The first word is the Employee's last name and the second word is the first name. The character code h or s tells me what kind of employee they are, salaried or hourly. Lastly the number after the character code is hourly wage if an hourly employee or yearly salary if a salaried employee.

Smith, John   h   5.00
Potter, Harry   s   10000

What I want to do with this information is scan the text file to automatically create a new salaried employee object or hourly employee object depending on what character code is recognized.

These are the parameters for an hourly employee object.

public HourlyEmployee(String first, String last, double wage)

This is what I came up with.

File input = new File("EmployeesIn.txt"); 
Scanner in = new Scanner(input);


while(in.hasNextLine()) {
    int i = 1;
    String line =(in.nextLine());
    if (line.contains(" h ")) {
        HourlyEmployee Employee1 = new HourlyEmployee(in.next(),in.next(),in.nextDouble());
        System.out.println(Employee1);  

The problem with this code, is that I get an InputMismatchException from the in.nextDouble();.

So I edited my code to manually assign the wage to 1 to at least see if it correctly assigned the last name and first name, but it didn't even do that correctly. It used the wrong line to assign the values, and assigned the First Name as the last name and Last Name as the first name.

Harry, Potter,  $1.0/hour

So my question is, how do I correctly do this? Based on the text file I provided I want to create an HourlyEmployee object with these parameters

HourlyEmployee Employee1 = new HourlyEmployee(Smith,John,5.00);

Two obvious problems:

Since you read the file in order, the first thing you read in is the last name; but the first parameter your HourlyEmployee needs is the first name. Secondly, your third attempt to read the file will end up reading the 'h' or 's' flag, not the pay. Solution: read the names in separately, then call the function. Something like this:

while(in.hasNextLine()) {
  lastName = in.next();
  firstName = in.next();
  payType = in.next(); // read the 'h' or 's'
  pay = in.nextDouble();
  switch(payType) {
    case 'h': 
      Employee1 = new HourlyEmployee(firstName, lastName, pay);
      break;
    case 's':
      Employee1 = new SalariedEmployee(firstName, lastName, pay);
      break;
    default:
      // warn about invalid format
  }
  System.out.println(Employee1);  
}

You may need to trim the comma off the lastName variable - not sure if your function already takes care of that.

Why just to load file and read line by line?

Since your file structure - the same, its easy to parse/split each line

public static String loadFile(String filePath){



    try {           

        // Open the file that is the first command line parameter
        FileInputStream fstream = new FileInputStream(filePath);
        BufferedReader br = new BufferedReader(new InputStreamReader(fstream));

        String strLine = null;

        //Read File Line By Line
        while ((strLine = br.readLine()) != null  )   {

                            if(strLine.split("[ ]+").length > 3){
                               // do stuff here
                             }

        }// end while

        //Close the input stream            
        br.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return buff.toString();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM