Page 2 of 2 FirstFirst 12
Results 21 to 33 of 33
  1. #21
    s_dawg101 is offline Member
    Join Date
    Oct 2009
    Posts
    49
    Rep Power
    0

    Default

    Java Code:
    import java.util.Scanner;
    import java.text.DecimalFormat;
    
    public class EmployeeDemo
     {
     	public static void main(String[] args) 
    	{
     	DecimalFormat dollar = new DecimalFormat("#,##0.00");
    	Scanner keyboard = new Scanner (System.in);
    	
     	//variables are declared here
    	int newWorkers, totalWorkers;
    	String newEmployee;
    	double hours;
    	double hourlyRate;
    	double salary;
    	String choice;
    	
    	//Get the number of workers from the user
    	System.out.print("How many employee's pay do you want to calculate?  ");
    	newWorkers = keyboard.nextInt();
    	
    	totalWorkers = 0;
    
    	
    	for (int count = 1; count <= newWorkers; count++){
    		System.out.print("What is the employee's name?  ");
    		newEmployee = keyboard.next();
    		System.out.print("Is the employee payed on a hourly rate or salary?   ");
    		choice = keyboard.next();
    		if(choice == "hourly rate" || choice == "Hourly Rate"){
    			System.out.print("How many hours did the employee work?  ");
    			hours = keyboard.nextDouble();
    		}
    		else{
    			System.out.print("How much is this persons salary?  ");
    			salary = keyboard.nextDouble();
    		}
    		
    	}
    		
     	} 
     }
    ok this is what i have so far and it compiles but this is the error i get when i try to tell the program that the employee is payed on an hourly rate.
    Java Code:
    ----jGRASP exec: java EmployeeDemo
    
    How many employee's pay do you want to calculate?  1
    What is the employee's name?  Grey
    Is the employee payed on a hourly rate or salary?   Hourly Rate
    How much is this persons salary?  Exception in thread "main" java.util.InputMismatchException
    	at java.util.Scanner.throwFor(Scanner.java:840)
    	at java.util.Scanner.next(Scanner.java:1461)
    	at java.util.Scanner.nextDouble(Scanner.java:2387)
    	at EmployeeDemo.main(EmployeeDemo.java:37)
    
     ----jGRASP wedge2: exit code for process is 1.
     ----jGRASP: operation complete.
    what does this error mean and how can i correct it???

  2. #22
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,458
    Blog Entries
    7
    Rep Power
    20

    Default

    Why do people always think they can compare Strings for equality with the '==' operator? Java is not Basic. You have to use the String.equals( ... ) method and please do read the API documentation before you start coding.

    kind regards,

    Jos

  3. #23
    s_dawg101 is offline Member
    Join Date
    Oct 2009
    Posts
    49
    Rep Power
    0

    Default

    Java Code:
    //this program reads data from a file
    import java.util.Scanner;
    import java.text.DecimalFormat;
    import java.io.*;
    
    public class Employee2Demo
     {
     	public static void main(String[] args) throws IOException
    	 {
    	 	//allows the user to input something
    		//changes the number format to #,###.000
    	 	DecimalFormat numberformat = new DecimalFormat("#,###.000");
    	 	Scanner keyboard = new Scanner (System.in);
    		
    		//variables are declared here
    		String filename, Employee;		//Employee's name
    		double hours;
    		double salary;
    		
    		System.out.println("Please open the file that contains your employee data.");
    		
    		System.out.print("What is the name of the file:  ");
    		filename = keyboard.nextLine();
    		
    		//reads from the file that the user chooses
    		FileReader freader = new FileReader (filename);
    		BufferedReader inputFile = new BufferedReader(freader);
    		
    		Employee = inputFile.readLine();
    		
    		while (Employee != null){
    			System.out.println(Employee);
    			
    			Employee = inputFile.readLine();
    		}
    		
    		inputFile.close();
    	 }	
     }
    ok i changed up my strategy. we were giving a data file and the program must read from the data file. we must include a boolean statement and a "if" statement that differs if the employee is payed by an hourly rate or by a salary.

    how and where would i put those???

  4. #24
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    You are doing it again with Employee = inputFile.readLine();
    The line does not contain an Employee. It contains employee data that you need to split (using the String.split method) and then you create an Employee for the resultant array by calling a relevant constructor.

  5. #25
    s_dawg101 is offline Member
    Join Date
    Oct 2009
    Posts
    49
    Rep Power
    0

    Default

    how would i go about do this???

  6. #26
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    You read my reply above again.

  7. #27
    s_dawg101 is offline Member
    Join Date
    Oct 2009
    Posts
    49
    Rep Power
    0

    Default

    yea i did and this shit is like a foreign language to me. i think i need to create a constructor. maybe something like this??
    Java Code:
    public Employee2Demo(String Employee, double hours){
    }
    do i need anything in the curly braces???

  8. #28
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    I'm out of this thread. I don't like discussing with people who use bad language.

  9. #29
    Lizzip is offline Member
    Join Date
    Oct 2009
    Posts
    12
    Rep Power
    0

    Default

    Instead of Employee = inputFile.readLine();

    Something more along the lines of

    String line = inputFile.readLine();
    String[] splitLine = line.split(" ");

    Then you can use
    splitLine[0] = whatever;
    splitLine[1] = whatever;

    and so on.

  10. #30
    s_dawg101 is offline Member
    Join Date
    Oct 2009
    Posts
    49
    Rep Power
    0

    Default

    Java Code:
    //this program reads data from a file
    import java.util.Scanner;
    import java.text.DecimalFormat;
    import java.io.*;
    
    public class Employee2Demo
     {
     	public static void main(String[] args) throws IOException
    	 {
    	 	//allows the user to input something
    		//changes the number format to #,###.000
    	 	DecimalFormat numberformat = new DecimalFormat("#,###.000");
    	 	Scanner keyboard = new Scanner (System.in);
    		
    		//variables are declared here
    		String filename, Employee;		//Employee's name
    		double hours;						//hours employee worked
    		double salary;						//salary employee is payed
    		double hourlyRate;				//hourly rate that employee is payed
    		double caculateWeeklyGrossPay;//calculated the weekly pay for employees that are payed on an hourly rate
    		
    		//tells the user to open the file so the program can establish a connection with it
    		System.out.println("Please open the file that contains your employee data.");
    		
    		//asks the user what the file name is
    		System.out.print("What is the name of the file:  ");
    		filename = keyboard.nextLine();
    		
    		//reads from the file that the user chooses
    		FileReader freader = new FileReader (filename);
    		BufferedReader inputFile = new BufferedReader(freader);
    		
    		String line = inputFile.readLine();
    		String[] splitLine = line.split(" ");
    		
    		while (Employee != null){
    			System.out.println(Employee);
    			
    			Employee = inputFile.readLine();
    		}
    		
    		inputFile.close();
    	 }	
     }
    this is what i have and it give me this error
    Java Code:
     ----jGRASP exec: javac -g D:\Student Data\Documents\Fall 09\CS-144\Employee2Demo.java
    
    Employee2Demo.java:36: variable Employee might not have been initialized
    		while (Employee != null){
    		       ^
    1 error
    
     ----jGRASP wedge2: exit code for process is 1.
    how would i correct this?? i think i need to change something but i am not sure what to change

  11. #31
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I haven't read your code, but if it were me, I'd do just what the error message tells you to do: Initialize Employee first, even if it means initializing it to null.

  12. #32
    s_dawg101 is offline Member
    Join Date
    Oct 2009
    Posts
    49
    Rep Power
    0

    Default

    ok i changed the code around a bit. how would i use the splitLine = method to read the line and determine whether it says an Employee's name or the salary or the hourly rate.

  13. #33
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    There is no such thing as a splitLine method. If you mean String split, most of us with this challenge would experiment with String split and the array it returns. Print the array out with a for loop and see the results! Then use this information to figure out a solution.

    Much luck!

Page 2 of 2 FirstFirst 12

Similar Threads

  1. In need of some assistance
    By Boer84 in forum New To Java
    Replies: 2
    Last Post: 07-08-2008, 04:14 PM
  2. X-Tremely new to this...Need assistance...
    By Johnny562 in forum New To Java
    Replies: 5
    Last Post: 07-01-2008, 09:17 PM
  3. [SOLVED] Your assistance required please!
    By crazydeo in forum New To Java
    Replies: 1
    Last Post: 05-21-2008, 11:58 AM
  4. i need assistance with a string triggered loop please!
    By Phobos0001 in forum New To Java
    Replies: 9
    Last Post: 11-14-2007, 02:44 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •