• 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

Need help with structure...

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok here is my story. this is my first java course... I am not looking for someone to do my homework for me. I would like someone to tell me what I am doing wrong and what to do to correct it. so I can learn from my mistakes. I am writing code that takes employees name, pay, hours and overtime snd outputs it and then loops for multiple employees until a name of "ZZZZ" is entered. Here is what I have so far.



{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);


String fullName;
double rate = 0;
double pay;
double hours;
double regpay;
double ovtpay;

do
{
System.out.print( "Enter full name ");
fullName = keyboard.next();

}
while(fullName != ZZZZ);


{
do
{
System.out.print( "Enter total hours worked. ");
hours = keyboard.nextInt();
if(hours <=0)
System.out.print( "Must enter a quantity:");
} while(hours <=0);

while(hours <=0)

do
{
System.out.print( "Enter pay rate per hour. ");
pay = keyboard.nextDouble();
if(pay <=0)
System.out.print( "Must enter a pay rate:");
} while(pay <=0);

if (hours < 40);

pay = hours * rate;
ovtpay = 0;
regpay = pay;

if (hours > 40)

pay = regpay + ovtpay;
ovtpay = (hours - 40)* 1.5 * rate;
regpay = 40 * rate;
}
{
System.out.print("The employees name is:"+ fullName);
System.out.printf("The hourly rate is:%n$%6.2f%n"+ rate);
System.out.print("The hours are"+ hours);
System.out.printf("The regular pay is:%n$%6.2f%n"+ regpay);
System.out.printf("The overtime pay is:%n$%6.2f%n"+ ovtpay);
System.out.printf("The employee's total pay is:%n$%6.2f%n"+ pay);
}




}

}


Thanks to anyone that takes the time to help me. When I ask the instructor he corrects the issues himself without explaining what I did wrong...which doesnt really help me.
 
Bartender
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch Bob.

About your program, I assume it works as is. Then couple of questions for you to think about (given you do know the basics of Java):
1) Is do/while loops the best approach?
2) Can the program structure into more OO perspective? (eg a separate class for employee)
3) There is not exception handling. Scanner methods surely throws exceptions depending on your inputs
4) How are the inputted employees stored? There is no kind of array or list or collection for storage (if you have a collection, then you most likely have a employee class)
 
Bob Spaulding
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
its not working at the moment getting one error:

E:\ECA223\payRateCalc.java:29: cannot find symbol
symbol : variable ZZZZ
location: class payRateCalc
while(fullName != ZZZZ);


as for knowledge of java not so much. I think I am trying to hard im all over the book and still cant figure it out. lol
 
K. Tsang
Bartender
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need to assign your input to some variable using the Scanner class eg
 
Bob Spaulding
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well I tried it still cant get it to work. We are only up to chapter 3 in the book so we are still in basic loops and structuring. it says to use sentinel logic to solve the problem.
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The == operator doesn't do what you think it does on reference types. That includes Strings. Somebody has already told you to use the equals method, which is correct. But the use of System.exit is a bad idea, too.

Actually I would agree with your use of a "do" loop. It does mean you will end up working out a pay rate for somebody called "ZZZZ" however. Remember "ZZZZ" not ZZZZ.

Sentinel-controlled looping means you keep your loop going as long as something particular happens, in your case the name not being ZZZZ.
Forget about exception handling for the time being. Just resign yourself to the application crashing if you enter "Bob" when you want 123.45.
It is not appropriate to use do-loops around Scanner calls however. At this stage I suggest you simply use the nextDouble method, leaving out any type-checking. Ask about the type-checking later. Make your application simple and short, and you will better see what is happening.

You will see other people have difficulty with the same thing and if you have a week to spare, have a look at this thread and look for the strange-looking syntax involving while and equalsIgnoreCase on the same line. That is a very strange-looking bit of syntax, but is probably the best way to run such a loop.

The advice to create an Employee class is good; hasn't your book told you that?
 
Bob Spaulding
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for your advice and the suggestion to check out the other post! Ok after I have worked on this all day I have studied her way of writing this similar program.
I have rearranged my code using her structure as an example. We are only in chapter 3 of Absolute Java by Savitch so I wasnt familiar with some of her input but structuring is what I am having problems with. I got my code to compile and run through the first time and then when you get to enter the second name and you press enter there is an exception.
Here is my new code:

[b][/b]

The exception is at line 30 hourlyRate input statement im kind of stumped because it went through the loop once but gets an error when it goes through the second time.
I marked line 30 and it is an inputmismatch error code.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bob Spaulding wrote:
The exception is at line 30 hourlyRate input statement im kind of stumped because it went through the loop once but gets an error when it goes through the second time.



A couple of points... First, the exception error message has a lot of detail which can be used to find the error. If you don't tell us what the exception is, we can't really help you. Second, we can't tell what line 30 is -- as all we get is snippets of your code -- you need to flag that line for us.

Henry
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please tell us the details. Which Exception?

If it is an InputMismatchException you might have a similar problem to this thread. That behaviour of Scanner's takes some time getting used to. You are reading the name twice when you start your loop. One of the next() calls needs to be deleted.
Try using nextLine() for your name input; then you can enter Bob Spaulding and have it appear as one String rather than Bob on its own. In fact if you are calling next(), then it takes Bob as the name and leaves Spaulding for later; when it tries to parse that to an int . . . .
 
Bob Spaulding
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I marked line 30 and it is an inputMisMatch error code. I tried deleting one of the name next()'s and that caused an error and if I put nextLine in it skips the name on the second loop and goes straight to hours lol...
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bob Spaulding wrote:I marked line 30 and it is an inputMisMatch error code. I tried deleting one of the name next()'s and that caused an error and if I put nextLine in it skips the name on the second loop and goes straight to hours lol...



Input mismatch means that what is next is not what you wanted. In this case, the next token can't be parsed to be a double.

Henry
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bob Spaulding, your code would be easier to read, and therefore read by more people, if you would use Code tags.
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the version you had last night, you had name = next() before the loop. so I entered "Campbell Ritchie" and it took "Campbell" for name.
Then you get to the heading of the while and it took "Ritchie" as name.
Then in the loop I entered 123.45 as salary and 40 as hours and got a result.
Then if I went back to the loop and tried "Bob Spaulding" it took "Bob" for name and "Spaulding" for the salary . . .

I would suggest you try nextLine() instead of next() in a few places.
 
Bob Spaulding
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok I added the nextLine() statements and it is just skipping adding the name in the second time around and adding the first name automatically... I even tried using last name thinking with one word it wouldnt double up.

here is the code now:


 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You had two lines with name = . . . before. One was before the loop and the other was at the beginning of the loop. You have deleted that bit from the beginning of the loop and retained the call before the loop. So you will always have the same name. And all your other inputs will get out of order with one another.

Don't use \n in a printf statement. Use %n instead. And don't write such long lines; they are wider than most screens.
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you really put a semicolon after if ( . . . )?

You should have been warned against that.

And I presume the blue 30 > in an earlier version of your quoted code is a mistake: please post your code with copy-and-paste.
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Correct me if I am wrong but, it seems to me that everyone is looking at code when they should be looking at logic.
Here is how I see the problem: You need to do until right, when right then do something else and go to the next section. Using this manner of logic you then know that the inputer will not get to go anywhere till it is right and when it is right that will be recorded. This then leads to the choice of loops, the do while seems to be the only choice. Bring them in and do not let them out till they get it right. Being you are just learning do not worry about exceptions or full names. To start with just use one name and let the name be anything as long as you convert it to characters ie b2b anything but ZZZZ. Once you get that going you can test for more things even adding the full name.
To use full names you will need to do as said above, sorry who every said it for not relooking, use nextLine() this works for when there might be spaces.
 
Larry Robinson
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



next
public String next(Pattern pattern)
Returns the next token if it matches the specified pattern. This method may block while waiting for input to scan, even if a previous invocation of hasNext(Pattern) returned true. If the match is successful, the scanner advances past the input that matched the pattern.
Parameters:
pattern - the pattern to scan for
Returns:
the next token
public String nextLine()
Advances this scanner past the current line and returns the input that was skipped. This method returns the rest of the current line, excluding any line separator at the end. The position is set to the beginning of the next line.
Since this method continues to search through the input looking for a line separator, it may buffer all of the input searching for the line to skip if no line separators are present.
Returns:
the line that was skipped

Here is the program that you started with and a few changes. First I always just import the whole java.util or any of the others. That way when I am working I do not have to go back and add another sector of the class. I also set the decision at the bottom as well as the request for a decision. I also cleared the variable “fullname” after it's use, at the bottom. I changed some of the variables it is best to be descriptive so, it is easy to read. Oh! I also setup the decision from ZZZZ to Y/N so that next() would work. As you can see from above next() returns a token; one character in this case. As you can read from above the nextLine() searches till reads a line separator so there needs to be a line separator added to your variable or else you will only be able to use the name input first time around. I could not find a way around this with the limitations you have on your assignment. There are lots of ways of doing it a lot easier but not within the scope of your assignment.
Take care and remember there is no such thing as Luck just hard work and good thoughts.



I hope this helps you. Make your logic flow from one section to the other. In my other post I to had some logic mistakes.
 
Bob Spaulding
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your help guys I am still trying to figure out the whole name thing I have tried a while loop a do while loop my brain is melting and this is only one assignment lol after I figure out the name problem then I have tofigure out how to total all the wages paid but im hung up on the name thing.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic