user3036658
user3036658

Reputation: 13

Java file input

I am currently working through a task in my new placement. Basically what I need to do is get numbers from a file and multiply them. My file read as follows

Dave

7 35

Lucy 

6 19

I need to get the numbers for each person and multiply them so I can use them later on, I have this code so far, I'm sure there is a simpler way as this seems very long winded. The print to screen is there simply to test if it works.

import java.util.*; 
import java.io.*; 
import javax.swing.JOptionPane;
public class Task1 
{
  public static void main(String[] args) throws FileNotFoundException
  {
    Scanner inFile = new Scanner(new FileReader("ExternalData.txt")); 
    String name; 
    double rate; 
    double hours; 
    name = inFile.next (); 
    rate = inFile.nextDouble(); 
    hours = inFile.nextDouble(); 
    double weeklypay1 = rate * hours;
    String name2;
    double rate2;
    double hours2;
    name2 = inFile.next (); 
    rate2 = inFile.nextDouble(); 
    hours2 = inFile.nextDouble();
    double weeklypay2 = rate * hours;

    System.out.println("Daves pay:" + weeklypay2);

    }
}

My question is basically does this look like a good way to lay it out or am I going completely the wrong way about it, very new to java so would appreciate any help, thanks in advance!

Upvotes: 0

Views: 255

Answers (3)

Jason Sperske
Jason Sperske

Reputation: 30416

Here is a quick attempt at cleaning this up:

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Task1 {
  @SuppressWarnings("resource")
  public static void main(String[] args) throws FileNotFoundException {
    Scanner inFile = new Scanner(new FileReader("ExternalData.txt"));
    List<Employee> employees = new ArrayList<>();
    while(inFile.hasNext()) {
      employees.add(new Employee(inFile.next(), inFile.nextDouble(), inFile.nextDouble()));
    }
    for(Employee employee : employees) {
      System.out.println(employee);
    }
  }
}

With this class (to hold employee data and to cleanly print it):

public class Employee {
  public final String name;
  public final double rate;
  public final double hours;
  public final double weeklypay;

  public Employee(String name, double rate, double hours) {
    this.name = name;
    this.rate = rate;
    this.hours = hours;
    this.weeklypay = this.rate*this.hours;
  }

  public String toString() {
    return name+"'s pay:" + weeklypay;
  }
}

With your text file it produces this output:

Dave's pay:245.0
Lucy's pay:114.0

Or you can store them in a Map (with the name as the key) and do stuff like this:

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class Task1 {
  @SuppressWarnings("resource")
  public static void main(String[] args) throws FileNotFoundException {
    Scanner inFile = new Scanner(new FileReader("/ExternalData.txt"));
    Map<String, Employee> employees = new HashMap<>();
    while(inFile.hasNext()) {
      Employee employee = new Employee(inFile.next(), inFile.nextDouble(), inFile.nextDouble()); 
      employees.put(employee.name, employee);
    }
    System.out.println(employees.get("Dave"));
  }
}

Upvotes: 1

gmansoor
gmansoor

Reputation: 509

Try something like this

import org.apache.commons.io.FileUtils;

import org.apache.commons.io.StringUtils;
...
LineIterator it = FileUtils.lineIterator(file, "UTF-8");
try {
    while (it.hasNext()) {
        String line = it.nextLine();
        if (!StringUtils.startsWithAny(line, 
                    new String[] {"1","2","3","4","5","6","7", "8","9"})) {
               //dosomething with the name
        }
        else {
            //do something with the numeric
        }
    }
} finally {
    LineIterator.closeQuietly(iterator);
}

Upvotes: 0

Paul Samsotha
Paul Samsotha

Reputation: 208964

Your line spaces in the file are causing the exception. You need to add line breaks accordingly. Try:

....
name = inFile.next();
infile.nextLine();      <------------- line break
rate = inFile.nextDouble(); 
hours = inFile.nextDouble(); 
double weeklypay1 = rate * hours;
infile.nextLine();      <------------- line break
String name2;
double rate2;
double hours2;
name2 = inFile.next();  <------------- line break
infile.nextLine(); 
rate2 = inFile.nextDouble(); 
hours2 = inFile.nextDouble();
....

If this doesn't work, you may need an extra line. Try it out and see if it works.

Upvotes: 0

Related Questions