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.

Page 1 of 2 12 LastLast
Results 1 to 25 of 28

Thread: Absolute Beginner Here, should be an easy question for anyone really.

  1. #1
    Member
    Join Date
    Jul 2012
    Posts
    56
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default Absolute Beginner Here, should be an easy question for anyone really.

    // Thomas Harrald IT215
    // Checkpoint Payroll Program Two
     
    import java.util.Scanner; //Import Scanner
     
    public class PayrollProgramtwo
    {
    	public static void main( String[] args )
    	{
    	Scanner input = new Scanner (System.in);
     
    	String deptName;
    	int numberEmployees;
    	float avgSalary;
    	float totalDept;
    	boolean end = false;
     
    	System.out.println( "Welcome to the Payroll Program" );
     
    	while (end  == false)
     
    	{
     
    		System.out.println( "Please input the name of the department type stop to end." );
    		deptName = input.next();	
     
    		if(deptName.toLowerCase().equals( "stop"))
    			end = true;
     
    		System.out.println( "Please input the number of employees" );
    		numberEmployees = input.nextInt();
    		while (numberEmployees < 0 )
    			{
    				System.out.println ( "Please input a positive number");
    				numberEmployees = input.nextInt();
    			}
     
    		System.out.println( "Please input the average employee salary" );
    		avgSalary = input.nextFloat();
    		while ( avgSalary < 0 )
    			{
    				System.out.println ( " Please input a positive number");
    				avgSalaray = input.nextFloat();
    			}
     
    		totalDept = numberEmployees * avgSalary;
     
    		System.out.printf( "The name of the department is %s\n ", deptName );
    		System.out.printf( "The total payroll is $%sUSD ", totalDept);
     
     
    	}
     
     
     
    	System.out.println( "Thank you for using the Payroll Program!");
    	}
    }
    I cannot for the life of me figure out what I am doing wrong, I feel like it is something realllllyyyyyyy tiny. It runs fine the first time, but then it skips the part where I ask for user to input Dept Name. When I type stop I get some crazy error. Also there is something wrong with my validation while loops, as it prompts the user to input a positive value, but immediately exits the loop and continues to the next line.

    This is a homework assignment, and no it's not due today. I turned this is a while ago and got most credit, but I really want to know where I went wrong. Please, any help would be greatly appreciated.

    Thanks!
    Last edited by Harrald; July 31st, 2012 at 09:18 PM.


  2. #2
    Member
    Join Date
    Jul 2012
    Posts
    69
    My Mood
    Relaxed
    Thanks
    1
    Thanked 6 Times in 6 Posts

    Default Re: Absolute Beginner Here, should be an easy question for anyone really.

    What exactly does it give for error?
    Also try using the code taggs.
    .code=java]
    your code here
    ./code]

    replace the . with [

  3. #3
    Member
    Join Date
    Jul 2012
    Posts
    56
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default Re: Absolute Beginner Here, should be an easy question for anyone really.

    Fixed the code. Will do that from now on, I plan on being here a lot.

    Error:

    Exception in thread "main" java.util.InputMisMatchException

    Thanks for the tip on the code tags.

  4. #4
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Absolute Beginner Here, should be an easy question for anyone really.

    Perhaps better than telling you what is wrong, how about helping you figure out what is wrong. Next time you will be better prepared to find and fix. Lets see if I can steer you in a positive direction.

    Lets run through the flow of the program:

    public static void main {
    check
    variables declared (however only one initialized)
    check
    print welcome to the user
    check
    while (end == false)
    variable was initialized to false, so the loop will run
    check
    deptName = input.next();
    //What is stored in the variable deptName at this point? ...are you sure? Try a println(deptName); and verify

    if (deptName.toLowerCase().equals("stop"))
    				end = true;
    While it is legal to do this, I have two complaints. One, if you just always use brackets, you never have to worry about not using them. As in:
    if (deptName.toLowerCase().equals("stop")) {
        end = true;
    }
    What will happen when when you type stop? Will it just stop? Or will it run the next line of code in the while loop? Why does it happen the way it does?
    Put another println("Did it stop, or run the next line of code in the loop?"); after end=true; and run it, and type stop...
    You will have to fix the typo on a variable name before you can run it and test these things. But give it a shot and post any questions you have.

    Edit: I will get to the other complaint on not using brackets in the next run

  5. The Following User Says Thank You to jps For This Useful Post:

    Harrald (July 31st, 2012)

  6. #5
    Member
    Join Date
    May 2011
    Location
    west palm beach, FL
    Posts
    189
    My Mood
    Tired
    Thanks
    41
    Thanked 11 Times in 10 Posts

    Default Re: Absolute Beginner Here, should be an easy question for anyone really.

    in the avgsalary while loop inside the loop you spelled avgsalary "avgsalaray"

  7. The Following User Says Thank You to derekxec For This Useful Post:

    Harrald (July 31st, 2012)

  8. #6
    Member
    Join Date
    Jul 2012
    Posts
    56
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default Re: Absolute Beginner Here, should be an easy question for anyone really.

    Hey I am always up for a little guiding!

    I am however not sure with what you mean by "check"? Does that mean I should check it? Or like a checklist check?

    At the end in the
     System.out.printf( "The name of the department is %s\n ", deptName );

    It tells me what the deptName is correctly.

    When I type stop I get the error "Exception in thread "main" java.util.InputMismatchException.

  9. #7
    Member
    Join Date
    Jul 2012
    Posts
    56
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default Re: Absolute Beginner Here, should be an easy question for anyone really.

    Quote Originally Posted by derekxec View Post
    in the avgsalary while loop inside the loop you spelled avgsalary "avgsalaray"
    lol Yeah, fixed it. Now the loop actually stops enough to let me input the Dept Name now, it would just skip automatically and go to employees.

    It runs good except the "stop" part. It displayed "Did it stop?" and then moves to the next line.

  10. #8
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Absolute Beginner Here, should be an easy question for anyone really.

    Quote Originally Posted by Harrald View Post
    ..I am however not sure with what you mean by "check"? Does that mean I should check it? Or like a checklist check?...
    By check, I just mean we can assume all is well so far, as far as the program advancing to the next place you intended for it to advance to.

  11. The Following User Says Thank You to jps For This Useful Post:

    Harrald (July 31st, 2012)

  12. #9
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Absolute Beginner Here, should be an easy question for anyone really.

    Quote Originally Posted by Harrald View Post
    lol Yeah, fixed it. Now the loop actually stops enough to let me input the Dept Name now, it would just skip automatically and go to employees.

    It runs good except the "stop" part. It displayed "Did it stop?" and then moves to the next line.
    Ahh so by changing your boolean to true, your loop continues to execute. The reason why:

    When you run a while loop you say, while (thisCondition), then you have your body of the loop, defined by the { and continuing to the corresponding }. The scope of the loop. Even though you changed the value of the variable, the value is not checked until the loop passes the end of its scope and tries to start it again. Only at that point does it realize the variable has changed. So on top of changing the variable you have to break out of the loop if you want to stop immediately. you can use break; to do just that. When a break is encountered, the current loop will exit without trying to continue.

  13. #10
    Member
    Join Date
    Jul 2012
    Posts
    56
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default Re: Absolute Beginner Here, should be an easy question for anyone really.

    Quote Originally Posted by jps View Post
    By check, I just mean we can assume all is well so far, as far as the program advancing to the next place you intended for it to advance to.
    Roger. Then I am all good except the stop part. Still working on that.

  14. #11
    Member
    Join Date
    Jul 2012
    Posts
    56
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default Re: Absolute Beginner Here, should be an easy question for anyone really.

    Good to go!

    It works just as designed. Thank you kindly all for the help. I think the core of the problem of it skipping parts was avgSalaray, which I guess was messing with the while loop input.next().

    Using break; the program stops and displays the exit message. I spent ridiculous amounts of hours trying to get this to work.

  15. #12
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Absolute Beginner Here, should be an easy question for anyone really.

    Quote Originally Posted by Harrald View Post
    Roger. Then I am all good except the stop part. Still working on that.
    Try this:
    Replace your if with this:
    if (deptName.toLowerCase().equals("stop")) {
    	end = true;
    	break;
    }

    and then replace that with this:
    if (deptName.toLowerCase().equals("stop")) {
    	//end = true;
    	break;
    }

  16. The Following User Says Thank You to jps For This Useful Post:

    Harrald (July 31st, 2012)

  17. #13
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Absolute Beginner Here, should be an easy question for anyone really.

    Quote Originally Posted by Harrald View Post
    Good to go!

    It works just as designed. Thank you kindly all for the help. I think the core of the problem of it skipping parts was avgSalaray, which I guess was messing with the while loop input.next().

    Using break; the program stops and displays the exit message. I spent ridiculous amounts of hours trying to get this to work.
    Don't rush off to happy land so fast. You have made progress but there are more things I would like to go over.

  18. #14
    Member
    Join Date
    Jul 2012
    Posts
    56
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default Re: Absolute Beginner Here, should be an easy question for anyone really.

    Quote Originally Posted by jps View Post
    Don't rush off to happy land so fast. You have made progress but there are more things I would like to go over.
    lol happy land. You have to remember the first time your VERY basic programs worked and how good that felt.

    But please, do continue.

  19. #15
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Absolute Beginner Here, should be an easy question for anyone really.

    Did you try the two if blocks above in place of the if block you had?

    What does that tell you about the boolean variable?

  20. The Following User Says Thank You to jps For This Useful Post:

    Harrald (July 31st, 2012)

  21. #16
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Absolute Beginner Here, should be an easy question for anyone really.

    Quote Originally Posted by Harrald View Post
    lol happy land. You have to remember the first time your VERY basic programs worked and how good that felt.

    But please, do continue.
    Yes, I remember. I get that same feeling every time I am able to help someone else understand something. Feels nice

  22. #17
    Member
    Join Date
    Jul 2012
    Posts
    56
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default Re: Absolute Beginner Here, should be an easy question for anyone really.

    Quote Originally Posted by jps View Post
    Did you try the two if blocks above in place of the if block you had?

    What does that tell you about the boolean variable?
    Works out exactly the same. I was thinking about that too, and wanted to try removing the entire boolean system. Let me try that.

  23. #18
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Absolute Beginner Here, should be an easy question for anyone really.

    Quote Originally Posted by Harrald View Post
    ... wanted to try removing the entire boolean system. Let me try that.
    Let me know how it works out for you. Then if you don't mind post your revised code so other readers and I can all be on the same page again.

  24. #19
    Member
    Join Date
    Jul 2012
    Posts
    56
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default Re: Absolute Beginner Here, should be an easy question for anyone really.

    // Thomas Harrald IT215
    // Checkpoint Payroll Program Two
     
    import java.util.Scanner; //Import Scanner
     
    public class PayrollProgramtwo
    {
    	public static void main( String[] args )
    	{
    	Scanner input = new Scanner (System.in);
     
    	String deptName="null";
    	int numberEmployees;
    	float avgSalary;
    	float totalDept;
     
    	System.out.println( "Welcome to the Payroll Program" );
     
    	while (!deptName.toLowerCase().equals("stop"))
     
    	{
     
    		System.out.println( "Please input the name of the department type stop to end." );
    		deptName = input.next();	
     
    		if(deptName.toLowerCase().equals( "stop"))
    			{
    			break;
     			}
    		System.out.println( "Please input the number of employees" );
    		numberEmployees = input.nextInt();
    		while (numberEmployees < 0 )
    			{
    				System.out.println ( "Please input a positive number");
    				numberEmployees = input.nextInt();
    			}
     
    		System.out.println( "Please input the average employee salary" );
    		avgSalary = input.nextFloat();
    		while ( avgSalary < 0 )
    			{
    				System.out.println ( " Please input a positive number");
    				avgSalary = input.nextFloat();
    			}
     
    		totalDept = numberEmployees * avgSalary;
     
    		System.out.printf( "The name of the department is %s\n ", deptName );
    		System.out.printf( "The total payroll is $%sUSD ", totalDept);
     
     
    	}
     
     
     
    	System.out.println( "Thank you for using the Payroll Program!");
    	}
    }

    Ha!

  25. #20
    Member
    Join Date
    Jul 2012
    Posts
    56
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default Re: Absolute Beginner Here, should be an easy question for anyone really.

    I honestly can't believe I figured that out lol.

  26. #21
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Absolute Beginner Here, should be an easy question for anyone really.

    Run the program with the following input:
    dept name = test
    #employees = 5
    avgSalary = 3.3333
    and see what you get. (Maybe this is not been covered in your class yet)

  27. #22
    Member
    Join Date
    Jul 2012
    Posts
    56
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default Re: Absolute Beginner Here, should be an easy question for anyone really.

    I got 16.6665USD. I just read in my readings a way to format an answer to two decimals. I think it was .2?

  28. #23
    Member
    Join Date
    Jul 2012
    Posts
    69
    My Mood
    Relaxed
    Thanks
    1
    Thanked 6 Times in 6 Posts

    Default Re: Absolute Beginner Here, should be an easy question for anyone really.

    That is a good solution, you could've also made an if-else statement. No particular advantages to that, but i find it easier to understand my own code that way:
    import java.util.Scanner; //Import Scanner
     
    public class PayrollProgramtwo {
    	public static void main(String[] args) {
    		Scanner input = new Scanner(System.in);
    		String deptName;
    		int numberEmployees;
    		float avgSalary;
    		float totalDept;
    		boolean end = false;
    		System.out.println("Welcome to the Payroll Program");
     
    		while (end == false) {
    			System.out.println("Please input the name of the department type stop to end.");
    			deptName = input.next();
    			if (deptName.equalsIgnoreCase("stop")) {
    				end = true;
    			} else {
    				System.out.println("Please input the number of employees");
    				numberEmployees = input.nextInt();
    				while (numberEmployees < 0) {
    					System.out.println("Please input a positive number");
    					numberEmployees = input.nextInt();
    				}
    				System.out.println("Please input the average employee salary");
    				avgSalary = input.nextFloat();
    				while (avgSalary < 0) {
    					System.out.println(" Please input a positive number");
    					avgSalary = input.nextFloat();
    				}
    				totalDept = numberEmployees * avgSalary;
    				System.out.printf("The name of the department is %s\n ", deptName);
    				System.out.printf("The total payroll is $%sUSD ", totalDept);
    			}
    		}
    		System.out.println("Thank you for using the Payroll Program!");
    	}
    }
    Last edited by elamre; July 31st, 2012 at 10:43 PM.

  29. #24
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Absolute Beginner Here, should be an easy question for anyone really.

    So two other quick notes.
    1. Your formatting is not completely consistent, and really does not follow java convention.
    1a. Sure you dont have to follow java convention, but if you ever work as part of a team, it sure is nice if everyone speaks the same language, right? No need to be different. When I write in java, I try to stick to java convention, and when I write c++, I stick to that convention. However...
    1b. Whether or not you stick to convention, you should at least be consistent. You should always indent the same, put brackets on the line above or on their own line etc. Granted some of the formatting may have been lost from your pc to the forum...

    2. Comments. Not a single comment in the code. It is nice to see a comment at the top that describes the purpose and use of the class (program). Then once you get into the business of the code, each little body (ok not EVERY ONE) could use a little one-liner. Just a note to remember what you meant for that line to do. Sure you can read the code and figure out what it does. But what happens when it is not working, and it is not a syntax error. It is an ugly logic error. If you look at a line that says:
    total = a + b;
    It is easy to assume the total is the sum of a and b. What if in fact total was not supposed to be the sum, instead you meant to multiply. Seeing a+b may not catch your eyes as an error, so you get to share some blood with the desk for an hour while you try to get it. Throwing in a comment like //product of this and that would make a+b stand out as not what you meant to do. Ok so that very low level example is not a suggestion to comment every time you add or multiply something, but just an example of how something can look right, and be so wrong.

  30. #25
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Absolute Beginner Here, should be an easy question for anyone really.

    Quote Originally Posted by Harrald View Post
    I got 16.6665USD. I just read in my readings a way to format an answer to two decimals. I think it was .2?
    Try it and see. If ever you are not sure what it will do. Back up your hard drive, and try it. (haha)

Page 1 of 2 12 LastLast

Similar Threads

  1. How do I get this to work? Easy question, I'm just hopeless at it!
    By akmi5 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: May 1st, 2012, 02:31 PM
  2. Exception in JUnit test (easy question)
    By opium in forum Exceptions
    Replies: 1
    Last Post: February 14th, 2012, 09:09 AM
  3. Easy array question
    By surfbumb in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 3rd, 2011, 09:44 PM
  4. Quick easy Java question.
    By DHG in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 25th, 2011, 03:59 PM
  5. [SOLVED] Begginer Question please help ASAP! (Easy to Answer)
    By Bagzli in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 11th, 2010, 10:00 PM