5 Replies - 737 Views - Last Post: 08 February 2013 - 10:58 AM Rate Topic: -----

#1 Sonic94   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 07-February 13

Payroll Assignment

Posted 07 February 2013 - 04:29 PM

Hello guys !

I need some help with my Java Assignment ...

Assignment: http://screencast.com/t/NmLcx7OlA

I'm stuck from the begging until the c part.

I have no idea how to do a loop that reads every line from a file and how do I take those fields and add them into a Object ?

Thank you for your help !!!

Is This A Good Question/Topic? 0
  • +

Replies To: Payroll Assignment

#2 bgammill   User is offline

  • New D.I.C Head

Reputation: 14
  • View blog
  • Posts: 41
  • Joined: 11-June 12

Re: Payroll Assignment

Posted 07 February 2013 - 05:04 PM

It sounds like you need to reference lectures, slides, or your textbook. Most professors (I've yet to encounter one) don't just assign something without having a lecture on it.
Was This Post Helpful? 0
  • +
  • -

#3 Chrisangel29   User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 89
  • Joined: 25-January 13

Re: Payroll Assignment

Posted 07 February 2013 - 05:07 PM

View PostSonic94, on 07 February 2013 - 04:29 PM, said:

Hello guys !

I need some help with my Java Assignment ...

Assignment: http://screencast.com/t/NmLcx7OlA

I'm stuck from the begging until the c part.

I have no idea how to do a loop that reads every line from a file and how do I take those fields and add them into a Object ?

Thank you for your help !!!


Use a while loop and a scanner to read the file and save the input from the file in an array like this..

 while( scanner.hasNextLine() ) 
       
        String line[]  = scanner.nextLine(); 
        String x = line[0];
        .........
        //and then to create an object with the input
        Object ob = new Object(x);
        

Was This Post Helpful? 0
  • +
  • -

#4 Sonic94   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 07-February 13

Re: Payroll Assignment

Posted 07 February 2013 - 06:27 PM

Use a while loop and a scanner to read the file and save the input from the file in an array like this..

 while( scanner.hasNextLine() ) 
       
        String line[]  = scanner.nextLine(); 
        String x = line[0];
        .........
        //and then to create an object with the input
        Object ob = new Object(x);
        

[/quote]

I manage to do it like this:

String name1 = s.next();
String name2 = s.next();
String fullName = name1 + " " + name2;
int id = s.nextInt();
double rate = s.nextDouble();
double workHours = s.nextDouble();



The thing is that rate and workHours has all the values stored in them, I don't see how I can do this:

a. The total gross pay for all employees.
b. The total number of hours worked by all employees

The teacher doesn't want that we use Arrays ...
Was This Post Helpful? 0
  • +
  • -

#5 Chrisangel29   User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 89
  • Joined: 25-January 13

Re: Payroll Assignment

Posted 08 February 2013 - 10:41 AM

View PostSonic94, on 07 February 2013 - 06:27 PM, said:

Use a while loop and a scanner to read the file and save the input from the file in an array like this..

 while( scanner.hasNextLine() ) 
       
        String line[]  = scanner.nextLine(); 
        String x = line[0];
        .........
        //and then to create an object with the input
        Object ob = new Object(x);
        


I manage to do it like this:

String name1 = s.next();
String name2 = s.next();
String fullName = name1 + " " + name2;
int id = s.nextInt();
double rate = s.nextDouble();
double workHours = s.nextDouble();



The thing is that rate and workHours has all the values stored in them, I don't see how I can do this:

a. The total gross pay for all employees.
b. The total number of hours worked by all employees

The teacher doesn't want that we use Arrays ...
[/quote]

well if i understand well ..
a. You have to create a local variable to sum the total gross pay.. int sum = sum + grosspay;
b. You have to do the same int totalHours = totalHours + workHours;
Was This Post Helpful? 0
  • +
  • -

#6 Sheph   User is offline

  • D.I.C Lover
  • member icon

Reputation: 447
  • View blog
  • Posts: 1,032
  • Joined: 12-October 11

Re: Payroll Assignment

Posted 08 February 2013 - 10:58 AM

Create an employee object for each entry read from your scanner and add it to a list. After you're done reading the file, you can iterate over the list and sum up all of the hours and pay.

ArrayList<Employee> payroll = new ArrayList<Employee>();

while(s.hasNextLine()) {
    String name1 = s.next();
    String name2 = s.next();
    String fullName = name1 + " " + name2;
    int id = s.nextInt();
    double rate = s.nextDouble();
    double workHours = s.nextDouble();

    payroll.add( new Employee(fullName, id, rate, workHours) );
}

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1