aspose file tools*
The moose likes Beginning Java and the fly likes Code Help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Code Help" Watch "Code Help" New topic
Author

Code Help

Aretha Clark
Greenhorn

Joined: Mar 07, 2008
Posts: 21
I have been working on a assignment that requires a application that should prompt the user to input name , pay rate, and pay which i figured that part out then print out the name of the employee and the weekly pay. In the printout, display the dollar symbol ($) to the left of the weekly pay amount and format the weekly pay amount to display currency.Any help or advice would be appreciated

import java.util.Scanner; // program uses Scanner

public class Payroll
{
// main method begins program execution
public static void main( String args[] )
{
// create Scanner to obtain input from command window
Scanner input = new Scanner( System.in );


// prompt for and input employee name
System.out.println( "Please enter the employee name:" );
String employeeName = input.nextLine(); // read a line of text
System.out.println(); // outputs a blank line

// prompt for and input employee hourly wage
System.out.println( "Please enter the employee hourly wage:" );
String hourlyWage = input.nextLine(); // read a line of text
System.out.println(); // outputs a blank line

// prompt for and input employee hours worked
System.out.println( "Please enter the employee hours worked:" );
String hoursWorked = input.nextLine(); // read a line of text
System.out.println(); // outputs a blank line

sum = hourlyWage * hoursWorked; // multiply numbers ------------- This line is where i go wrong

System.out.println(); // outputs a blank line

} // end main

} // end class Payroll
Anubhav Anand
Ranch Hand

Joined: May 18, 2007
Posts: 341

Hello Aretha Clark. Welcome to JavaRanch.



This will never work because you can't multiply two strings.

Try this :



Hope that solves your problem..
[ March 07, 2008: Message edited by: Anubhav Anand ]
Anubhav Anand
Ranch Hand

Joined: May 18, 2007
Posts: 341

The Integer class is a wrapper class. You can read more about it here.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36501
    
  16
Much simpler way to do it. You declare hours and wage as numbers. Since you are a beginner, I will let you get away with floating point arithmetic, but you really ought to use BigDecimal for calculating money or anything else where precision is needed.

Go through the Scanner class in the API documentation, and you will find it has lots of methods which allow you to read the next number. Look through its methods section, and they usually begin with "next". That way you can get numbers to multiply and you don't need to mess around with parsing methods; Scanner does all that for you.
Aretha Clark
Greenhorn

Joined: Mar 07, 2008
Posts: 21
Thank you all very much i will also take a look at the API documentation
Aretha Clark
Greenhorn

Joined: Mar 07, 2008
Posts: 21
When i go in to add a less than zero code i get a new error

Operator <cant be applied to java.lang.string double if hours worked < 0.0

Please help.. Also if anyone have any advice for someone just starting java as to a decent beginners book or website.

import java.util.Scanner; // program uses Scanner

public class Payroll
{
// main method begins program execution
public static void main( String args[] )
{
// create Scanner to obtain input from command window
Scanner input = new Scanner( System.in );



// prompt for and input employee name
System.out.println( "Please enter the employee name:" );
String employeeName = input.nextLine(); // read a line of text
System.out.println(); // outputs a blank line
if (employeeName.equals("stop"))



// prompt for and input employee hourly wage
System.out.println( "Please enter the employee hourly wage:" );
String hourlyWage = input.nextLine(); // read a line of text
if ( hourlyWage < 0.0 )
System.out.println( "hourlyWage must be a positive number. Please enter a positive for wage:" );
System.out.println(); // outputs a blank line


// prompt for and input employee hours worked
System.out.println( "Please enter the employee hours worked:" );
String hoursWorked = input.nextLine(); // read a line of text
if ( hoursWorked < 0.0 )
System.out.println( "hoursWorked must be a positive number. Please enter a positive number of hours:" );
System.out.println(); // outputs a blank line

// display person name and hours worked multiplied by hourly wage
System.out.println(employeeName + " $" + (Integer.parseInt(hourlyWage) * Integer.parseInt(hoursWorked)));



} // end main

} // end class Payroll
Nicholas Jordan
Ranch Hand

Joined: Sep 17, 2006
Posts: 1282
Also if anyone have any advice for someone just starting java as to a decent beginners book or website.

Well I can sure fix you up there. Due to the level of work you are trying to do I am going to recommend Cay S. Horstmann - Core Java Series. The standard work here is a Heads First title co-authored by one of the participants at Java Ranch, but I use the H.F. style when I first examine a new coding language. It is the parseInt(hourlyWage) and it's similar methods in Long,Integer,Float,Double classes that will do much of the conversion, but issues like // outputs a blank line which become apparent to be System.out.println(employeeName.toString()); for the person who has been coding awhile only become apparent after writing program after program and I get much more from Mr. Horstmann's text when I get to the level of coding you post.

Do not tear up keyboards in frustration, that skill is only attained after a great deal of work.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36501
    
  16
You can only apply operators like < and > to numbers. You have a String to the left of the < so it can't take that operator. You need to get the hours worked into a number.

Please use ctrl-shift-C (or ctrl-C) and ctrl-V to copy messages and code; that saves you from spelling errors like string for String. On command prompt on Windows, use edit->mark->highlight->enter to copy code. Please use code tags; they make the code easier to read.

Anubhav Anand and I have already dropped hints about how you can get a number out of a String; have you had any luck working that out. I think that is your immediate problem.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36501
    
  16
. . . and as for books, although I like Horstmann and Cornell (I have a copy on my shelf) I don't think it is a good book for beginners. I have never used Head First, but people who have used it speak very highly of it. Make sure to get the 2nd edition. Another book I like is Deitel and Deitel; it will set you back about �45 or $90, but you can get a second-hand copy of the 6th edition for much less than that. As a beginner you won't notice much difference between the 6th and 7th editions, but don't get the 5th edition. It is designed with the raw beginner in mind.

For an online resource for training, try the Java Tutorials.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Code Help
 
Similar Threads
NoClassDefFoundError
Else without if error
illegal start of expression
Code problems
condition to leave all your loops