5 Replies - 1579 Views - Last Post: 17 September 2011 - 11:47 AM Rate Topic: -----

#1 b.netana  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 92
  • Joined: 11-September 11

Payroll program stuck in infinite loop

Posted 17 September 2011 - 12:08 AM

// Payroll2.java
// A program to calculate and print the department, name, and pay of an employee.
import java.util.Scanner; //program uses the Scanner class
import java.io.*;

public class Payroll2

{

   // main method begins execution of Java program.
   public static void main(String args[])
   {
       // create Scanner to obtain input from the command window
	   Scanner input = new Scanner (System.in);
	   
	   double number1; // first number to multiply
	   double number2; // second number to multiply
	   double product; // product of number1 and 

	   
	   String name; // give the variable a name ( "name" in this case) 
	   do
	   {
	   System.out.print("Enter Department name: "); //prompt
	   name = input.nextLine(); // read name from user
	   
	   System.out.print("Enter number of employees: "); // prompt
	   number1 = input.nextDouble(); // read first number from user
	   
	   System.out.print("Enter average salary: "); // prompt
	   number2 = input.nextDouble(); // read second number from user
	   
	   product = number1 * number2; // multiply numbers
	   
	   System.out.println("Department name:" + name); // display Department name
	   System.out.printf("Payroll is: $%.2f\n", product); // display product
	   }
	      while (!name.equals("stop"));
	   
   } // end method main
   
} // end class Payroll2



Hello, I am new to programming, but I have had minor success in learning through trial and error (as well as looking at code snippets). I seem to be having a bit of trouble understanding where I am going wrong and why I have encountered an infinite loop here. Any help that could be given would be greatly appreciated

Is This A Good Question/Topic? 0
  • +

Replies To: Payroll program stuck in infinite loop

#2 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7576
  • View blog
  • Posts: 12,729
  • Joined: 19-March 11

Re: Payroll program stuck in infinite loop

Posted 17 September 2011 - 12:16 AM

The problem is with the way Scanner handles nextDouble, or other parsed tokens. In simple terms, it doesn't eat the newline unless you tell to get nextLine(). This is correct behavior for files, but causes some confusion when reading user input, and it really could be better documented.
Don't worry, you're not the first to get caught in this one.

The solution is to put a call to input.nextLine() after your last read in the loop: this will clear the newline character from System.in, and allow you to read the nextLine() token at the top of the next go round.
Was This Post Helpful? 2
  • +
  • -

#3 b.netana  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 92
  • Joined: 11-September 11

Re: Payroll program stuck in infinite loop

Posted 17 September 2011 - 12:41 AM

Thank you so much. One more thing, does anyone know where I might find a reference for the different statements in Java programming? My teacher neglected to post the appendices to the book, and I feel like I might not have fallen to this if I'd have had them.
Was This Post Helpful? 0
  • +
  • -

#4 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2197
  • View blog
  • Posts: 5,224
  • Joined: 10-September 10

Re: Payroll program stuck in infinite loop

Posted 17 September 2011 - 02:38 AM

What you're asking for is commonly referred to as The API, The Java API, etc. It is an online resource, though you can download it or it may be included with the JDK installation, buried in a folder somewhere. As long as you have an Internet connection, the online resource is convenient and kept up to date.

This link will take you to the beginning page of Java 6 API. What I do when I want to jump to a specific page is just Google it. For example, if I want to check the methods for the String class, I'd just Google, "Java String" and pick the result at the Oracle site that looks like the API page for the version of Java I'm using. You'll get better at recognizing which page to select after some practice.
Was This Post Helpful? 2
  • +
  • -

#5 b.netana  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 92
  • Joined: 11-September 11

Re: Payroll program stuck in infinite loop

Posted 17 September 2011 - 10:07 AM

Thank you both
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Payroll program stuck in infinite loop

Posted 17 September 2011 - 11:47 AM

Greg mention the API but I guess, that if it fits in an appendice, you want the statements by themselve.
Here are the reserve words from Oracle tutorials

http://download.orac.../_keywords.html

And here is the DreamInCode cheat sheet that has a printable version

http://www.dreaminco...ce-cheat-sheet/
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1