Two Laptop Bag
The moose likes Beginning Java and the fly likes While,if-else statements Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "While,if-else statements" Watch "While,if-else statements" New topic

While,if-else statements

jihwan kim

Joined: Aug 01, 2012
Posts: 14
Hello all!

This is my first post, so please excuse me if my formatting for submission is wrong

I've been working on an assignment that deals with inputting data for an employee time card, then printing out a paycheck. This is suppose to be a loop program where the data input and printing of paychecks doesnt stop until the firstName of DONE is used as a sentinel for stopping the program.

The data input has to be exactly:
first name
last name
Soc sec officeType (city||suburb) unionType (union||non-union) #regularHours payrate #dependents overtime
123456789 c n 80 9.55 3 5.5

My question is if I am setting up everything right, and what I could improve on.
Another problem that I am having is that when i compile this program using BlueJ (what I use in school), but working on Notepad++, it doesnt let me input anything, and nothing shows up on my screeen. I tried testing the "DONE" sentinel in the early stages of my program, and it didnt seem to work at all.
I have a lot more questions, but ive asked enough for this post.
Any help is much appreciated!

Thank you
Jihwan Kim
Greg Brannon

Joined: Oct 24, 2010
Posts: 563
Here's the input and resulting output from running your program:

So your program is doing something, or at least waiting for the user to do something, but it's hard to tell without prompts. Adding prompts is the first change I would suggest, if they're allowed. Until you can get the program running in your environment and make some of your own improvements, I don't see much point in offering more suggestions. Let us know when you've gotten it running and ask any additional questions that come from that.

Always learning Java, currently using Eclipse on Fedora.
Linux user#: 501795
jihwan kim

Joined: Aug 01, 2012
Posts: 14
Thank you for the reply Greg

I was surprised that the program ran at all for you considering it didnt work for me.

I've been working on this all day, and although it isnt perfect I think its pretty good so far.

Its pretty messy, and I used the for loops because I didn't know how to insert those *. Thinking back now, I should have just used left/right justifications with printf, but I am not too familiar.

The problem I am encountering now is that when I input a last name with a possible suffix (i.e., Oconnor the 3rd, or, davis III) the program jumps/prints 3 lines, and goes to the unionType prompt.

Employee first Name: John
Employee last name: David the Third
Employee social security numer: The character 'c' for city office or 's' for suburban office: The character 'u' for union or 'n' for non union member:

Is this occurring because of my first printf statement, the fact that its correlating my input space/white space with (%s %s %s)?

Thank you
Jihwan Kim
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46362
Welcome to the Ranch

I dislike BlueJ myself, but Notepad++ is an excellent editor.
I have removed some blank lines from your code, and changed all the text to black, because some people find coloured text difficult to read.
I agree about prompts:\u21a9 looks like this: ↩. If you get strange results with the nextLine method, read this.

But I have the problem with your code that it isn’t object‑oriented. You ought to have a PayCheck class, and probably PaySystem, too. Your main method should be one line long:-I think you should start by creating those classes, and working out how to pass information to them.

I would suggest your union field should be boolean, and you can get a boolean directly from the keyboard with a Scanner and one of its methods, but you would have to write true or false.
fred rosenberger
lowercase baba

Joined: Oct 02, 2003
Posts: 11955

In my not so humble opinion, you have too much code here for the state you are in.

When I write code, I NEVER write more than 2-3 lines at a time before I compile/debug/test. And I test it thoroughly. And when I think I"m done testing, i test it more. I make sure what I just wrote works before I do anything else. I try and write lots of methods that each to one thing, and one thing only. Any time you write a program with more than about 10 lines in your main() method, you are doing something wrong.

You really should try and break down your requirements into discrete components. For example, I would write a method that prompts for and gets the input ONE time (and probably exits if the firstname is "done"). I would have a separate method that calls the getInput() method over and over, until the getInput method returns some sentinel value saying "don't call me any more". Another method would print the data out.

The idea is that getting the data once is completely separate from how many times you need to get the data. If I write them as separate methods, if my specs change later (i.e. I need to get a dog's name, age, and breed), I could write a new getInput() method. I don't need to worry about anything else, since the code is compartmentalized.

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
I agree. Here's the link:
subject: While,if-else statements
It's not a secret anymore!