Can anyone help me dubug this?? data/hours.txt Brown 46 315 Chen 40 96 Fischer 6
ID: 3600122 • Letter: C
Question
Can anyone help me dubug this??
data/hours.txt
Brown 46 315
Chen 40 96
Fischer 62 350
Garcia 12 104
Jones 13 67
Mitchell 62 333
Nguyen 11 415
Sanchez 41 400
Thompson 22 77
Wei 0 0
Wilson 30 0
expected output:
Brown: $457.2
Chen: $360.0
Fischer: $652.0
Garcia: $113.2
Jones: $117.0
Mitchell: $650.64
Nguyen: $140.5
Sanchez: $404.0
Thompson: $198.0
Wei: $0.0
Wilson: $270.0
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
Here is the code:
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Demo {
public static void main(String args[]){
Scanner f=null;
//Activity 2
/** This code should compute the total wages owed to workers in a cell phone store.
* It reads the hours worked and amount in sales for each employee from a file, then
* calculates total wage as follows:
* - If total hours worked if over 40, pay them $9 for each of the first 40 hours ($360
* total) and $12 for each hour worked past 40 hours. Furthermore, if amount in sales
* is over $300, pay them a bonus of 8% the amount in sales, or if amount in sales is
* between $100 and $300, pay them a bonus of 5% the amount in sales.
* - If total hours worked is under 40, pay them $9 for each hour worked. Furthermore,
* if amount in sales is over $300, pay them a bonus of 10% the amount in sales, or if
* amount in sales is between $100 and $300, pay them a bonus of 5% the amount in sales.
*/
//TODO: Debug this code using the Eclipse debugger
try{
f = new Scanner(new File("data/hours.txt"));
}catch(FileNotFoundException e){
e.printStackTrace();
}
double hourlyWage = 9.00; // paying $9 per hour
double overtimeWage = 12.00; // $12 per hour for overtime work
double wage = 0;
while(f.hasNext()){
String line[] = f.nextLine().split(" "); //split on spaces
String name = line[0]; //employee name
double hours = Double.parseDouble(line[1]);
double sales = Double.parseDouble(line[2]);
if(hours>40){
wage += ( (40 * hourlyWage) + ((hours-40) * overtimeWage) );
if(sales>300){
wage += sales*.08;
}else if(sales>100);
wage += sales*.05;
}else{
wage += hours*hourlyWage;
if(sales>300){
wage += sales*.1;
}else if(sales>100);
wage += sales*.05;
}
System.out.println(name+": $" + wage);
}
Thank you so much for helping :)
Explanation / Answer
you did a mistake in else if after the else if() you put the semi colon ;
use this code it will work : ---------------------------------->>>>>>>>>>>>>>>>>>>>>>
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
public class Demo1{
public static void main(String[] args) {
Scanner f = null;
try{
f = new Scanner(new File("data/hours.txt"));
}
catch(FileNotFoundException e){e.printStackTrace();}
double hourlyWage = 9.00;
double overtimeWage = 12.00;
double wage = 0;
while(f.hasNext()){
String line[] = f.nextLine().split(" ");
String name = line[0];
double hours = Double.parseDouble(line[1]);
double sales = Double.parseDouble(line[2]);
if(hours>40){
wage += ( (40 * hourlyWage) + ((hours-40) * overtimeWage) );
if(sales>300){
wage += sales*.08;
}else if(sales>100){
wage += sales*.05;
}}else{wage += hours*hourlyWage;
if(sales>300){
wage += sales*.1;
}else if(sales>100)
wage += sales*.05;
}
System.out.println(name+": $" + wage);
}
}
}