944,119 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 5910
  • Java RSS
Jun 6th, 2007
0

Store and Retrieve Information

Expand Post »
I am trying to store information then retrieve it on my program. I know I have to use a class and a constructor. The problem is that my text doesn't have any good examples that I can view. I am truly lost. I am not sure
1. where to build this info
2. how to retrieve it

Here is my best try:

1. I believe I need to create the class, EmployeeData (?).
2. I need to store employee info, rate, and hours worked.
3. I believe the class needs to be created between -->>
public class PayrollProgram
{
DEFINE CLASS HERE?????
//main method begins execution of Payroll Program java application
public static void main (String[]args)
{


Any pointers/help is truly appreciated.

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

/** Creates a new instance of PayrollProgram */
public class PayrollProgram
{

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

int number1; // hourly rate
int number2; // hours
int product; // product of number1 and number2


System.out.println("Please enter your name or stop to quit the program:");//prompt
String nameOfEmployee=input.nextLine();

while(!(nameOfEmployee = input.next()).equalsIgnoreCase("stop"))

{

System.out.println("Enter hourly rate");//prompt
number1 = input.nextInt(); // read first number
while (number1 <=0)//prompt until user enters positive value
{
System.out.println ("Hourly rate must be positive. Please enter hourly rate again");//prompt user to enter in hourly rate again
number1 = input.nextInt(); // read first number
}
System.out.println( "Enter hours: " ); // prompt
number2 = input.nextInt();//read second number
while (number2 <=0)//prompt until user enters positive value
{
System.out.println ("Hours must be positive. Please enter hours worked again");//prompt user to enter in hours worked again
number2 = input.nextInt();//read second number

}

product = number1 * number2; // multiply numbers
System.out.print( nameOfEmployee ); // display employee name
System.out.printf("'s weekly pay is $%d\n",product);

System.out.println("Please enter your name or stop to quit the program:");//prompt
nameOfEmployee = input.nextLine();

}//end while
System.out.println("Ending program");

}//end method main

}//end class PayrollProgram
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
4zlimit is offline Offline
5 posts
since Jun 2007
Jun 6th, 2007
0

Re: Store and Retrieve Information

Click to Expand / Collapse  Quote originally posted by 4zlimit ...
I am trying to store information then retrieve it on my program. I know I have to use a class and a constructor. The problem is that my text doesn't have any good examples that I can view. I am truly lost. I am not sure
1. where to build this info
2. how to retrieve it

Here is my best try:

1. I believe I need to create the class, EmployeeData (?).
2. I need to store employee info, rate, and hours worked.
3. I believe the class needs to be created between -->>
public class PayrollProgram
{
DEFINE CLASS HERE?????
//main method begins execution of Payroll Program java application
public static void main (String[]args)
{


Any pointers/help is truly appreciated.

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

/** Creates a new instance of PayrollProgram */
public class PayrollProgram
{

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

int number1; // hourly rate
int number2; // hours
int product; // product of number1 and number2


System.out.println("Please enter your name or stop to quit the program:");//prompt
String nameOfEmployee=input.nextLine();

while(!(nameOfEmployee = input.next()).equalsIgnoreCase("stop"))

{

System.out.println("Enter hourly rate");//prompt
number1 = input.nextInt(); // read first number
while (number1 <=0)//prompt until user enters positive value
{
System.out.println ("Hourly rate must be positive. Please enter hourly rate again");//prompt user to enter in hourly rate again
number1 = input.nextInt(); // read first number
}
System.out.println( "Enter hours: " ); // prompt
number2 = input.nextInt();//read second number
while (number2 <=0)//prompt until user enters positive value
{
System.out.println ("Hours must be positive. Please enter hours worked again");//prompt user to enter in hours worked again
number2 = input.nextInt();//read second number

}

product = number1 * number2; // multiply numbers
System.out.print( nameOfEmployee ); // display employee name
System.out.printf("'s weekly pay is $%d\n",product);

System.out.println("Please enter your name or stop to quit the program:");//prompt
nameOfEmployee = input.nextLine();

}//end while
System.out.println("Ending program");

}//end method main

}//end class PayrollProgram
well... you still have some errors in your code. Just try the one I've posted under here, and try and find the differences with your code. Maybe it 'll help you understand where your code went wrong

Java Syntax (Toggle Plain Text)
  1. import java.util.Scanner; // program uses class Scanner
  2.  
  3. /** Creates a new instance of PayrollProgram */
  4. public class PayRollProgram
  5. {
  6.  
  7. //main method begins execution of Payroll Program java application
  8. public static void main (String[]args)
  9. {
  10. // create Scanner to obtain input from command window
  11. Scanner input = new Scanner( System.in );
  12.  
  13. int number1; // hourly rate
  14. int number2; // hours
  15. int product; // product of number1 and number2
  16.  
  17.  
  18. System.out.println("Please enter your name or stop to quit the program:");//prompt
  19. String nameOfEmployee=input.nextLine();
  20.  
  21. while(!(nameOfEmployee).equalsIgnoreCase("stop"))
  22.  
  23. {
  24.  
  25. System.out.println("Enter hourly rate");//prompt
  26. number1 = input.nextInt(); // read first number
  27. while (number1 <=0)//prompt until user enters positive value
  28. {
  29. System.out.println ("Hourly rate must be positive. Please enter hourly rate again");//prompt user to enter in hourly rate again
  30. number1 = input.nextInt(); // read first number
  31. }
  32. System.out.println( "Enter hours: " ); // prompt
  33. number2 = input.nextInt();//read second number
  34. while (number2 <=0)//prompt until user enters positive value
  35. {
  36. System.out.println ("Hours must be positive. Please enter hours worked again");//prompt user to enter in hours worked again
  37. number2 = input.nextInt();//read second number
  38.  
  39. }
  40.  
  41. product = number1 * number2; // multiply numbers
  42. System.out.print( nameOfEmployee ); // display employee name
  43. System.out.printf("'s weekly pay is $%d\n",product);
  44.  
  45. System.out.println("Please enter your name or stop to quit the program:");//prompt
  46. nameOfEmployee = "";
  47. while(nameOfEmployee.equals("")){
  48. nameOfEmployee = input.nextLine();
  49. }
  50.  
  51. }//end while
  52. System.out.println("Ending program");
  53.  
  54. }//end method main
  55.  
  56. }//end class PayrollProgram
Reputation Points: 938
Solved Threads: 357
Posting Maven
stultuske is offline Offline
2,528 posts
since Jan 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Help is needed for newbie in class!!
Next Thread in Java Forum Timeline: Function Arguments





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC