6 Replies - 6047 Views - Last Post: 25 February 2009 - 01:57 PM Rate Topic: -----

#1 jv44heinzbar   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 24-February 09

Having trouble with packages and input/output

Posted 24 February 2009 - 10:39 AM

Hi,
Well, I'm just starting out in java by taking a class at the local university. I must say that I've been out of school for sometime. I'm completely new to the whole programming concept.

I've asked my instructor for help, but she referred me to the TA. I'm having a hard time understanding the TA, and the questions I ask him are answered in 'geekspeak' for a newbie like myself. I haven't been able to get the TA to spoon feed me about the subtle nuiances of java.

I'm having a hard time working with the scanner class. I haven't completed the entire code, but I've posted what I've done so far. Currently, I can't seem to get past an error which states: "variable earnings might not have been initialized." I've been working on this problem for the past couple of days, searching the internet, reading my lab manual, and I still can't seem to find the answer on how to correct the problem.

Scanner class: I'm not 100% sure how to go about setting up my out put to display correctly. I understand simple concepts like, System.out.printf("Pi= %7.3f\n", Pi); But, I get confused when it comes to combining variables into a printf option and getting the output to line up properly as in the example below.

Please review my code. I must say that I'm more of visual learner. I understand the policy about 'not doing your homework'. However, I pick up things much quicker by example. Unfortunately, examples are poorly documented in my textbook. Additionally, the instructor is following her own guidelines rather than the book's. So, I'm at a loss right now.


Thanks,
Heinz

PS, the columns are suppose line up unlike the posted example ;)

My current code:

/*
Write a Java program to calculate and print the monthly paycheck for an employee. The net pay is calculated after taking the following deductions: 
  
	Federal income tax  = 15% 
	State income tax	=  5% 
	Social security tax =  5% 
	Medicare/Medicaid   =  3% 
	Pension plan		=  8% 
	Health insurance	= $125.00
  
Your program should ask the user to input the employee ID, last name, first name, and the earnings (gross amount) for current month.
The output should be the net pay, after deductions. Format the output to have 2 decimal places. 
  
Sample output: 
Employee ID:					 12345 
Employee Name:				John Doe 
====================================== 
EARNINGS						   4500.00 
Federal Tax						   675.00 
State Tax							  225.00 
Social Security					  225.00 
Medicare/Medicaid				 135.00 
Pension Plan						 360.00 
Health Insurance				  125.00 
====================================== 
NET PAY							 2755.00


*/


import java.util.Scanner;
  public class Assignment1_Prob2 
  {
	public static void main (String[] args)
	{
	  //Declarations
	  Scanner input = new Scanner (System.in);
	  
	  int emplyId;			//employee ID number
	  String firstName;	   // employee first name
	  String lastName;	  // employee last name
	  double earnings;		 // gross monthly earnings 
	  double fedTax;	  // federal tax
	  double staTax;	  // state tax
	  double socsec;	 // social security
	  double medcc;	  // medicare & medicaid
	  double penPlan;	// pension plan
	  double hlthins = 125.00;  // health insurance
	  double netPay; // pay after all deductions
		
	  //Input
	  
	  System.out.println("Please enter the employee's identification number.");
	  emplyId = input.nextInt();
	  
	  System.out.println("Please enter the employee's first name.");
	  firstName = input.next();
	  
	  System.out.println("please enter the employee's last name.");
	  lastName = input.next();
	  
	  System.out.printf("Please enter the employee's monthly gross earnings. %38.2f\n");
	  
	 
	  //Calculations
	  fedTax = earnings * .15;
	  staTax = earnings * .05;
	  socsec = earnings * .05;
	  medcc = earnings * .03;
	  penPlan = earnings * .08;
	  netPay = (earnings - fedTax - staTax - socsec - medcc- penplan);
	  
	  //Output
	 
	  System.out.printf("Employee ID Number: %40.0dfn", emplyId ); // HELP!!
	  System.out.println("Employee Name: " + firstName + " " + lastName);
	  System.out.printf("Monthly Gross Earnings: " + earnings);
	  System.out.println("========================================");
	  
	}
}



Is This A Good Question/Topic? 0
  • +

Replies To: Having trouble with packages and input/output

#2 mostyfriedman   User is offline

  • The Algorithmi
  • member icon

Reputation: 729
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: Having trouble with packages and input/output

Posted 24 February 2009 - 10:50 AM

you have to initialize 'earnings' first, before you can do any calculations with em otherwise you'll get an error
Was This Post Helpful? 0
  • +
  • -

#3 ayman_mastermind   User is offline

  • human.setType("geek");
  • member icon

Reputation: 127
  • View blog
  • Posts: 1,860
  • Joined: 12-December 08

Re: Having trouble with packages and input/output

Posted 24 February 2009 - 10:55 AM

@jv44heinzbar, you are gettning such an error because you are engaging the variable earnings into a multiplication without having any prior value.. to solve this problem you will have to add a value for your earnings variable which is i guess is supposed to be taken as input from the user when the user inputs the id,name, last name, and monthly earnings, it seems you did add the question to the user for that but not the input, it can be done this way:
System.out.printf("Please enter the employee's monthly gross earnings. %38.2f\n");
earnings= input.nextDouble();


I hope this helps, good luck in your programming :)

This post has been edited by ayman_mastermind: 24 February 2009 - 10:57 AM

Was This Post Helpful? 0
  • +
  • -

#4 jv44heinzbar   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 24-February 09

Re: Having trouble with packages and input/output

Posted 24 February 2009 - 12:13 PM

Ok folks, I'm making headway. I'm slowly figuring this problem out, but I still can't seem to get the first and last name to concatenate properly. I was told this is what I'm suppose to do, but after looking through my notes, I can't find it. Could anyone help me w/ concatenation?

[code]


/*
Write a Java program to calculate and print the monthly paycheck for an employee. The net pay is calculated after taking the following deductions:

Federal income tax = 15%
State income tax = 5%
Social security tax = 5%
Medicare/Medicaid = 3%
Pension plan = 8%
Health insurance = $125.00

Your program should ask the user to input the employee ID, last name, first name, and the earnings (gross amount) for current month.
The output should be the net pay, after deductions. Format the output to have 2 decimal places.

Sample output:
Employee ID: 12345
Employee Name: John Doe
======================================
EARNINGS 4500.00
Federal Tax 675.00
State Tax 225.00
Social Security 225.00
Medicare/Medicaid 135.00
Pension Plan 360.00
Health Insurance 125.00
======================================
NET PAY 2755.00

*/

import java.util.Scanner;
public class Assignment1_Prob2
{
public static void main (String[] args)
{
//Declarations
Scanner input = new Scanner (System.in);

int emplyId; //employee ID number
String firstName; // employee first name
String lastName; // employee last name
double earnings; // gross monthly earnings
double fedTax; // federal tax
double staTax; // state tax
double socsec; // social security
double medcc; // medicare & medicaid
double penPlan; // pension plan
double hlthins = 125.00; // health insurance
double netPay; // pay after all deductions

//Input

System.out.println("Please enter the employee's identification number.");
emplyId = input.nextInt();

System.out.println("Please enter the employee's first name.");
firstName = input.next();

System.out.println("please enter the employee's last name.");
lastName = input.next();

System.out.println("Please enter the employee's monthly gross earnings.");
earnings = input.nextDouble();


//Calculations
fedTax = earnings * .15;
staTax = earnings * .05;
socsec = earnings * .05;
medcc = earnings * .03;
penPlan = earnings * .08;
netPay = (earnings - fedTax - staTax - socsec - medcc- penPlan);

//Output

System.out.printf("Employee ID Number: %16.2s\n", emplyId );
System.out.println("Employee Name: " + firstName);//needs concatenation: first + last name and proper spacing.
System.out.println("========================================");
System.out.printf("Earnings: %16.2f\n", earnings);
System.out.printf("Federal Tax: %16.2f\n", fedTax);
System.out.printf("State Tax: %16.2f\n", staTax);
System.out.printf("Social Security: %16.2f\n", socsec);
System.out.printf("Medicare/Mediaid: %16.2f\n", medcc);
System.out.printf("Pension Plan: %16.2f\n", penPlan);
System.out.printf("Net Pay: %16.2f\n", netPay);



}
}
Was This Post Helpful? 0
  • +
  • -

#5 ayman_mastermind   User is offline

  • human.setType("geek");
  • member icon

Reputation: 127
  • View blog
  • Posts: 1,860
  • Joined: 12-December 08

Re: Having trouble with packages and input/output

Posted 24 February 2009 - 12:34 PM

Here is how you shoudl concatenate your first name with the last:

System.out.println("Employee Name: " + firstName +" "+ lastName);
//the " " denotes the space between the first and last name


here is your code edited and working:
import java.util.Scanner;
public class Assignment1_Prob2
{
public static void main (String[] args)
{
//Declarations
Scanner input = new Scanner (System.in);

int emplyId; //employee ID number
String firstName; // employee first name
String lastName; // employee last name
double earnings; // gross monthly earnings
double fedTax; // federal tax
double staTax; // state tax
double socsec; // social security
double medcc; // medicare & medicaid
double penPlan; // pension plan
double hlthins = 125.00; // health insurance
double netPay; // pay after all deductions

//Input

System.out.println("Please enter the employee's identification number.");
emplyId = input.nextInt();

System.out.println("Please enter the employee's first name.");
firstName = input.next();

System.out.println("please enter the employee's last name.");
lastName = input.next();

System.out.println("Please enter the employee's monthly gross earnings.");
earnings = input.nextDouble();


//Calculations
fedTax = earnings * .15;
staTax = earnings * .05;
socsec = earnings * .05;
medcc = earnings * .03;
penPlan = earnings * .08;
netPay = (earnings - fedTax - staTax - socsec - medcc- penPlan);

//Output

System.out.printf("Employee ID Number: %16.2s\n", emplyId );
System.out.println("Employee Name: " + firstName +" "+ lastName);//needs concatenation: first + last name and proper spacing.
System.out.println("========================================");
System.out.printf("Earnings: %16.2f\n", earnings);
System.out.printf("Federal Tax: %16.2f\n", fedTax);
System.out.printf("State Tax: %16.2f\n", staTax);
System.out.printf("Social Security: %16.2f\n", socsec);
System.out.printf("Medicare/Mediaid: %16.2f\n", medcc);
System.out.printf("Pension Plan: %16.2f\n", penPlan);
System.out.printf("Net Pay: %16.2f\n", netPay);



}
} 


i hope this helps, good luck ;)

This post has been edited by ayman_mastermind: 24 February 2009 - 12:36 PM

Was This Post Helpful? 0
  • +
  • -

#6 jv44heinzbar   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 24-February 09

Re: Having trouble with packages and input/output

Posted 24 February 2009 - 01:55 PM

Thanks folks.

I'm sure you'll see more of me as I continue to explore java :)

HB
Was This Post Helpful? 0
  • +
  • -

#7 jv44heinzbar   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 24-February 09

Re: Having trouble with packages and input/output

Posted 25 February 2009 - 01:57 PM

I just wanted to say thanks again. I posted my code to my professor since I can never get a decent answer from my TA. Anyhow, I will quote her here:

"Why do you print empId with a string format? If declared as int, print it as int! No printf needed when printing empId."

When I tried to use %XXXXf\n in my printf statement, I get a series of errors. So, I actually did a simple println statement to output the Employee's ID number. Additionally, I did the same for Employee's name.

My question, using the code above, could anyone show me the proper way to format the output for emplyID and emplyname? I've been trying to get these two outputs to line up properly, but I'm not having much luck. I'd like to get a better understanding of printf rather than counting spaces to align outputs.

System.out.printf("Employee ID Number: %16.2s\n", emplyId );		   //I tried using %16.2f\n" but get errors. Why?
System.out.println("Employee Name: " + firstName +" "+ lastName);	//Can I use printf to align proper? If so, how can I use printf and concatenate at the same time?



I have another problem that is related to the above problem in regards to aligning columns, but with multiple field headings. I guess once I understand printf and its properties, I'll be better off in creating similar programs.

Thanks,
Heinz
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1