Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: 46: Unreachable Statement

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    23
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default 46: Unreachable Statement

    import java.util.Scanner;
     
    public class Quiz9
    {
    	public static void main(String[] args)
    	{
    		Scanner kb = new Scanner(System.in);
    		Utility util1 = new Utility();
    		Payroll emp1 = new Payroll();
    		String strFirstName;
    		String strLastName;
    		String strSocialSecurityNumber;
    		String strInfo;
    		double dTotalPay;
    		double dTotalAverage;
     
    		double dRateOfPay;
    		double dHoursWorked;
    		int iNumberOfDependents;
    		while(true)
    		{
    		util1.clearScreen();
    		System.out.print("What is the users first name?");
    		strFirstName = kb.nextLine();
    		emp1.setFirst(strFirstName);
    		System.out.print("What is the user's last name?");
    		strLastName = kb.nextLine();
    		emp1.setLast(strLastName);
    		System.out.print("What is the user's Social Security Number?");
    		strSocialSecurityNumber = kb.nextLine();
    		emp1.setSSN(strSocialSecurityNumber);
    		System.out.print("What is the user's rate of pay?");
    		dRateOfPay = kb.nextDouble();
    		emp1.setRate(dRateOfPay);
    		System.out.print("How many hours did the user work?");
    		dHoursWorked = kb.nextDouble();
    		emp1.setHours(dHoursWorked);
    		System.out.print("How many dependants does the user have?");
    		iNumberOfDependents = kb.nextInt();
    		emp1.setDependents(iNumberOfDependents);
    		strInfo = emp1.toString();
    		System.out.print(strInfo);
    		util1.pressEnterToContinue();
    		util1.clearScreen();
    		}//end of while true
    		if(dHoursWorked == 0)
    		{
    			System.exit(0);
     
    		}
     
    	}
    }

    Why is my if statement unreachable?
    Also, how would I take the total of all the wages entered and output them at the end of the program?
    Also, why can I not input a first name for an employee after the first run?

    Thanks for the help!


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: 46: Unreachable Statement

    When do you expect that while loop to exit?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Oct 2011
    Posts
    23
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: 46: Unreachable Statement

    I want the if loop to end when I enter 0 for the amount of hours worked.

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: 46: Unreachable Statement

    That's a while loop- there is no such thing as an "if loop". But that while loop is while(true), which will never exit. Your System.exit() call is after that while loop, and since that while loop will never exit, that call will never be reachable.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Junior Member
    Join Date
    Oct 2011
    Posts
    23
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: 46: Unreachable Statement

    Alright, I have another questions, and thanks. I am calling the emp1.setRate from a different java class, and I want to know how to total all of the numbers that I input. Can you be of any assistance with that?

  6. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: 46: Unreachable Statement

    Maybe, but why are you setting the rate of a single instance of Employee multiple times? Don't you want multiple Employees, each with a different rate?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Methods Help - Unreachable statement error
    By jessica202 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 14th, 2011, 10:24 AM
  2. Unreachable statements
    By Marcus in forum Loops & Control Statements
    Replies: 7
    Last Post: December 10th, 2009, 02:02 PM
  3. another unreachable statement..
    By chronoz13 in forum Loops & Control Statements
    Replies: 3
    Last Post: October 2nd, 2009, 12:15 PM
  4. unreachable statement Again?
    By chronoz13 in forum Loops & Control Statements
    Replies: 3
    Last Post: October 1st, 2009, 10:23 AM
  5. unreachable statement?
    By chronoz13 in forum Loops & Control Statements
    Replies: 1
    Last Post: September 11th, 2009, 10:06 AM