0

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!

7
  • 1
    Does the program work the way you want it to? Commented Nov 26, 2013 at 17:57
  • It just depends upon you. Commented Nov 26, 2013 at 17:58
  • You could try putting it in a loop, can reduce a few lines of code with that. But overall it looks fine. Commented Nov 26, 2013 at 17:58
  • I'm having a problem with the file input which i had working a second ago so just sorting that out to find out before i can check. I have about 10 names so i was just thinking it would get a bit long, i'll have a look at loops now is there any tutorials you could recommend? Commented Nov 26, 2013 at 18:00
  • i have this error Exception in thread "main" java.util.InputMismatchException at java.util.Scanner.throwFor(Unknown Source) at java.util.Scanner.next(Unknown Source) at java.util.Scanner.nextDouble(Unknown Source) at Task1.main(Task1.java:13) Commented Nov 26, 2013 at 18:03

3 Answers 3

1

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"));
  }
}
Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

0

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);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.