• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

New to Java and I cant get this to work

 
Ranch Hand
Posts: 46
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey guys, I am new to this site and Java as well. I have been wracking my brain trying to figure out this assignment for school on my own but I am stumped. There don't actually seem to be any errors I can see; it just doesn't execute like it should. Can you guys help me out? Any help is appreciated.

 
Rancher
Posts: 43081
77
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch.

"it just doesn't execute like it should" is not a particularly useful problem description. What should it, and what does it do instead? At which point does one diverge from the other?
 
Greenhorn
Posts: 3
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
your If is not correct, use brackets where possible

not correct:



correct:

 
Jorge Gutierrez
Ranch Hand
Posts: 46
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm really sorry, I didn't mean to be so vague. There are two parts to this assignment; I got the first part working as it should. The first part ends before the boolean I declared and it is based on this:

"Create a non-GUI-based Java application that calculates weekly pay for an employee. The application should display text that requests the user input the name of the employee, the hourly rate, and the number of hours worked for that week. The application should then print out the name of the employee and the weekly pay amount. In the printout, display the dollar symbol ($) to the left of the weekly pay amount and format the weekly pay amount to display currency."

The second section that I am working on if after the boolean and is the section Im having trouble with, and this is what its supposed to do:

"Modify the Payroll Program application so it continues to request employee information until the user enters stop as the employee name. In addition, program the application to check that the hourly rate and number of hours worked are positive numbers. If either the hourly rate or the number of hours worked is not a positive value, the application should prompt the user to enter a positive amount."

When I run the code, it acts as though the second section doesn't exist; when I enter "stop" as the name, it just continues, and if I enter a negative number it doesn't print the text I set it to.
 
Marshal
Posts: 8857
637
Mac OS X VI Editor BSD Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Jorge,

1. Suggestion in order do not loose mark - lines 13, 14. Get a habit to name a variables which makes sense. As in your comments for instance: hourlyWage, hoursWorked and avoid number1, number2, which doesn't say a lot (will be easier to understand for everyone and for yourself). Comment itself is a good, but each time to understand what variable means, you have to look for a comment to remind that to yourself.
2. Lines 17, 19, 21, 26, 32 "System.out.println("Enter Employee Name");". In order to read user input, leave an empty space at the end of string after the colon System.out.println("Enter Employee Name: ");

Other suggestions probably will be given by more experienced guys here ;)
 
Jorge Gutierrez
Ranch Hand
Posts: 46
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Kyle Mills wrote:your If is not correct, use brackets where possible

not correct:



correct:



When I changed this it starts to go into the second section, but it still doesn't recognize "stop", it just acts as though stop is a name, and it asks me for the employee wage forever.
 
Kyle Mills
Greenhorn
Posts: 3
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

When I changed this it starts to go into the second section, but it still doesn't recognize "stop", it just acts as though stop is a name, and it asks me for the employee wage forever.



your vars are set as int, you can't assign stop to an int, it will throw an InputMismatchException. my guess is, use Integer.parseInt() method, change the declarations to a String and if the user types stop, do your close and System.exit();
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Welcome to the Ranch!

You have to be very careful with your semicolons. A stray one can result in an empty statement, which does nothing and is probably not what you intended. In the code above, line 6 is an if-statement that does nothing. The statement that follows it is actually executed regardless of the condition. This is not what you intended. Proper formatting and use of braces, even when there is only one statement to execute, usually helps you avoid or at least quickly find these kinds of problems. The code you have written is actually formatted properly like this:

Go through your code again and see where you have done similar things. And try to format your code properly. Also, take the advice previously given to use meaningful names and save people, especially yourself, the effort of trying to figure out what the intent of the code is.
 
Kyle Mills
Greenhorn
Posts: 3
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A safer way that avoids the risk of NullPointerException is:
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jorge Gutierrez wrote:When I changed this it starts to go into the second section, but it still doesn't recognize "stop", it just acts as though stop is a name, and it asks me for the employee wage forever.


And that's because of your loops. Have a look at this one (corrected, and indented as Junilu suggested):and tell me exactly what it does - step by step.

Winston
 
Jorge Gutierrez
Ranch Hand
Posts: 46
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I made all the edits suggested, ran the code, and this is the exact output from the IDE:

run:
Enter Employee Name
stop
Enter Hourly Wage
15
Enter Hours Worked
40
Employee Name: stop
Pay is $600USD
Enter Employee Name
Enter Hourly Wage

It actually wont allow me to enter the employee name again, and when I enter a wage, it just repeats "Enter Hourly Wage" over and over. I want to apologize to everyone for my ignorance; i am only three weeks into the course but it has been nothing but "read the text and figure it out" so Ive doing just that to the best of my ability haha.
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jorge Gutierrez wrote:I made all the edits suggested, ran the code, and this is the exact output from the IDE:


That's fine, but it's not what I asked for.

It actually wont allow me to enter the employee name again, and when I enter a wage, it just repeats "Enter Hourly Wage" over and over.


So, look at the loop you've written to enter the wage and tell me (or us) exactly what it does. In detail.
What does 'System.out.println("Enter Hourly Wage");' do?
What does 'number1 = input.nextInt();' do?
Like that. And keep going until you've described the entire loop.

I want to apologize to everyone for my ignorance...


Not a problem. We've all been there. But if we just hand you the answer you won't learn.

So, once again, look at your "enter wage" loop and do as I suggest.

The simple act of describing to us what it does will probably tell YOU what's wrong.

Winston
 
Marshal
Posts: 79177
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome again

There is something not right about your having a boolean (should read enterAnotherEmployee) and never changing it in the body of the loop. Try
enterAnotherEmployee = !name.equals("stop");
You may need to wrap the remainer of the loop in
if (enterAnotherEmployee)...
but that allows you to dispense with the break statement (and the close()).
Get rid of the close() call: never never never close a Scanner or similar pointing at System.in. Read this thread and you will find out why.

Yes, using "stop".equals(...) obviates NullPointerExceptions, but I do not believe your nextLine call can ever return null, so you probably won't have that problem here. If somebody pushes enter twice, however, you will get an empty String returned and no name. This post discusses the possibility of getting the empty String from nextLine.
 
Jorge Gutierrez
Ranch Hand
Posts: 46
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Winston Gutkowski wrote:

Jorge Gutierrez wrote:I made all the edits suggested, ran the code, and this is the exact output from the IDE:


That's fine, but it's not what I asked for.

It actually wont allow me to enter the employee name again, and when I enter a wage, it just repeats "Enter Hourly Wage" over and over.


So, look at the loop you've written to enter the wage and tell me (or us) exactly what it does. In detail.
What does 'System.out.println("Enter Hourly Wage");' do?
What does 'number1 = input.nextInt();' do?
Like that. And keep going until you've described the entire loop.

I want to apologize to everyone for my ignorance...


Not a problem. We've all been there. But if we just hand you the answer you won't learn.

So, once again, look at your "enter wage" loop and do as I suggest.

The simple act of describing to us what it does will probably tell YOU what's wrong.

Winston



while(number1 >0);


Winston, once I did this I face-palmed. Of course the loop is going to continue forever; I'm asking it to continue as long as the number entered is greater than zero. My issue is even after reading the text we are provided and looking online, I cant figure out a way to make this loop continuously until I enter "stop" as the name. I think once I figure out that portion, the rest will be simple. I changed the loops to if-else statements to test it out and it will run through and display the message if I enter a negative number, but obviously it wont loop. There is clearly something about loops I'm not understanding. I don't want the answer handed to me, but I would really appreciate if anyone could point me in the right direction. If anything, at least you guys have gotten me to see my mistakes, so Im going to spend the rest of the night trying to figure this out.
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The advice you need has already been given but I will reiterate:
1. Always use braces, even when there is only one statement in the body.
2. Format your code properly; auto format your code so that you can see how the computer interprets what you've written. Your formatting is misleading you in some places; the compiler does not actually follow the flow you think you have specified based on your indentation. This problem is compounded by not using braces and having stray semicolons.

The problem you have is that you have a brace that is matched up differently by the compiler from what you think it is. If you just follow the advice above, you will save yourself some grief and aggravation.

The other problem you have is that you have everything in main. This makes it difficult for you to isolate problems because everything you have is piled into one method. But I don't know if you've learned about methods and breaking up a big, complex task into many smaller and simpler ones.
 
Jorge Gutierrez
Ranch Hand
Posts: 46
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for reiterating. I will carefully go through what has been said here and figure it out. I don't have any other resources besides the internet and my online texts so I'm struggling a little but it will come to me sooner or later. Thank you very much to everyone who took the time to help me.
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jorge Gutierrez wrote:while(number1 >0);

Winston, once I did this I face-palmed. Of course the loop is going to continue forever; I'm asking it to continue as long as the number entered is greater than zero...


Well done! You've just completed your first lesson in proper debugging.

And BTW, it's a very good habit to get into: When you run into problems like this, try to find someone else to explain the problem to and go through your logic line by line with them. You'd be amazed how often that simple act will show YOU what you've done wrong because, instead of being concerned with how to write it, you're actually analysing what it DOES.

And it's just one of the reasons that we (or I) keep banging on about WhatNotHow (←click). You cannot write Java code to solve a problem until you understand WHAT it needs to do; and that usually involves lots of paper, pencils, and your brain ... Oh, and NO computer.

Winston
 
Jorge Gutierrez
Ranch Hand
Posts: 46
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Junilu Lacar wrote:
The other problem you have is that you have everything in main. This makes it difficult for you to isolate problems because everything you have is piled into one method. But I don't know if you've learned about methods and breaking up a big, complex task into many smaller and simpler ones.




I am unsure what you mean by this. Are you talking about calling multiple classes?
 
Jorge Gutierrez
Ranch Hand
Posts: 46
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Alright, so I finally got my loops for the hourly wage and hours worked to function as they should, but for the life of me, I cant figure out why my employee name section wont stop. The instructor in my class just posted today that we are supposed to use while (!(employeeName.equals("stop")) which doesnt make any sense because its missing parentheses. This little piece here is what is making my life miserable:




I switched around the variable as suggested by Junilu, and Im not seeing anything right off the bat that looks wrong. When I execute the code, I get this:

Enter Employee Name
Jorge
Enter Hourly Wage
12
Enter Hours Worked
12
Employee Name: Jorge
Pay is $144USD
Enter Employee Name
Enter Employee Name
stop

Enter Hourly Wage

I also dont understand why it shows "Enter Employee Name" twice.
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jorge Gutierrez wrote:I also dont understand why it shows "Enter Employee Name" twice.


Read the JavaDocs for Scanner carefully and figure out the difference between how nextInt() and nextLine() process the input. Carefully read through the examples given in the JavaDocs as well.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also search this forum for “Beatles” in one of my posts from about 2 years ago. That gives a bit more explanation about nextLine. I have yet to see a book with a good explanation of nextLine.
 
Jorge Gutierrez
Ranch Hand
Posts: 46
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Alright guys, thanks to your help I have finally gotten almost everything to work normally. The program stops when I put in stop where it should and everything is dandy. My final question is this; how do I get the whole program to continue to request information until stop is entered? I have tried a few methods but none seem to work. My update code looks like this:

 
Jorge Gutierrez
Ranch Hand
Posts: 46
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried to do it like:



Since this is what the instructor of my course says its supposed to look like, but as I learned from Winston, reading this code line by line tells me that "Enter Employee Name" will just repeat forever if I dont enter stop.
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jorge Gutierrez wrote:Since this is what the instructor of my course says its supposed to look like, but as I learned from Winston, reading this code line by line tells me that "Enter Employee Name" will just repeat forever if I dont enter stop.


In which case either
(a) Your instructor is wrong.
or
(b) You misinterpreted his/her instructions about what it should "look like".

And I hope you'll forgive me, but I suspect the latter.

First off: those two checks basically do the same thing, so one of them must be redundant.

Second: I fear that you may run into the same problems with next() as with nextInt() (but I could be wrong). It's just one of the reasons I dislike Scanner (as Campbell well knows ).

Did you read the "UserInput" link I gave you? The technique it explains allows you to forget all about the issues of combining next...() and nextLine(), and also how to put all those input procedures into methods.

Also: that business of entering "stop" to finish the entry process is going to be darn difficult if you want to be able to enter it for ANY field (including your numeric ones); so to start with I'd only check for it when you're entering the name (although I have to say, it's a poor way to do it).

A better alternative, IMO, is to add a "Do you want to enter another Employee?" question at the end, and control your main() loop with the answer to that.

However, ANY top-checked loop - ie, while(whatever) - can be converted into a bottom-checked one by simply repeating whatever creates the circumstances for 'whatever' at the end, eg:and what do you do when you have repeated code? Put it in a method:Get the idea?

HIH

Winston
 
Jorge Gutierrez
Ranch Hand
Posts: 46
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Winston,

I see what you are saying, and it definitely makes more sense this way; I was only struggling with the method you see me using because the assignment states that I have make the program continue to request information until "stop" is entered as the employee name, and the instructors are really picky with specifics when it comes to grading. Ill use the information you've given me to get this done, thanks again.
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jorge Gutierrez wrote:because the assignment states that I have make the program continue to request information until "stop" is entered as the employee name, and the instructors are really picky with specifics when it comes to grading.


Yeah, instructors can be like that.

Do it as you've been told to ... just remember that it's probably not the best way.

Winston
 
Jorge Gutierrez
Ranch Hand
Posts: 46
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Winston and everyone,

I just wanted to update everyone on what I did with this. I was unable to figure out how to make it work in the way that the assignment was asking, but I was able to make it work another way. Fortunately, the instructor gave me a perfect score on this. Again I want to thank everyone for your help!




 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jorge Gutierrez wrote:I just wanted to update everyone on what I did with this. I was unable to figure out how to make it work in the way that the assignment was asking, but I was able to make it work another way. Fortunately, the instructor gave me a perfect score on this.


Congratulations!

I actually rather like your solution, though it's not the way I would have done it - but that probably says more about me than you; and it suggests to me that you're going to be a good programmer.

Very well done ... and thanks for posting your solution (which merits a cow).

Winston
reply
    Bookmark Topic Watch Topic
  • New Topic