• 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

Payroll Beginner Simple Java

 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello.
I'm needing some help. I finished school about a year and a half ago and I'm trying to get freshened up on some of the stuff I learned in Java. I'm cutting and pasting a piece of code that I am trying to make work and I can't get it to work. Can someone help me and show me what the code is suppose to look like?

 
Bartender
Posts: 1845
10
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think the most obvious thing is the use of "quotes" in places you don't need them.
Quotes around something make it that literal text value (a String)
If you want to use the variable you have declared, get rid of the quotes.

For instance


 
Os Stewart
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stefan Evans wrote:I think the most obvious thing is the use of "quotes" in places you don't need them.
Quotes around something make it that literal text value (a String)
If you want to use the variable you have declared, get rid of the quotes.

For instance




Thank you! I forgot this! So quotes only need to be used for literal text?
 
Marshal
Posts: 8856
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

Ofelia Stewart wrote:So quotes only need to be used for literal text?

You're right
 
Liutauras Vilda
Marshal
Posts: 8856
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
I merged your stuff with the following thread. I hope that is okay by you.
 
Os Stewart
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I need some help with this, I can not get this to compile correctly after inputting name, salary and dependents. Can someone help me?

 
Ranch Hand
Posts: 624
9
BSD Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you getting any error on console? Post the complete error message if any.
 
Ranch Hand
Posts: 2412
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Notice this code



When you declare a local variable, you cannot use it in an expression that assumes its value until you have assigned the variable a value.
 
Liutauras Vilda
Marshal
Posts: 8856
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
Ofelia Stewart,

Please do not duplicate your threads, as very similar to this was create few hours earlier by yourself.
Let me see what I can do by merging them together for you, so you could get better responses and information would be better organized.
 
Os Stewart
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks you guys! everything seems to be okay, other that these errors.

Variable Salary might not have been initiated.
Variable numDependents migh not have been initiated.

when I try to fix it it turns this:



to this:



I'm trying to make it to where I can input salary and dependents, and then it calculaes.
 
Greenhorn
Posts: 18
Netbeans IDE C++ Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, you have some variables that need something in there besides zero right? So ask the user for it. use JOptionPane and a little parsing and you should be good to go. JOptionPane will ask the user what the program needs to know in order to run. This then makes the variable have something in it. Easy peasy.
 
Os Stewart
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Enos Winder wrote:Ok, you have some variables that need something in there besides zero right? So ask the user for it. use JOptionPane and a little parsing and you should be good to go. JOptionPane will ask the user what the program needs to know in order to run. This then makes the variable have something in it. Easy peasy.



Thank you for your fast reply! Would this be a public class? I'm not familiar with this. I use to have a bunch of stuff I used from when I was in school, as a reference, but unfortunately, I lost it all somehow.
 
Os Stewart
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Like so?

 
Os Stewart
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ofelia Stewart wrote:Like so?




When I do this, it say's "Unused Import"
 
Marshal
Posts: 79151
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ofelia Stewart wrote:. . .
When I do this, it say's "Unused Import"

That is because you are using a Scanner for your keyboard input and you don't actually need the option pane.
 
Os Stewart
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Ofelia Stewart wrote:. . .
When I do this, it say's "Unused Import"

That is because you are using a Scanner for your keyboard input and you don't actually need the option pane.



When I edited my scanner, my "System.out.prints" no longer work.
 
Enos Winder
Greenhorn
Posts: 18
Netbeans IDE C++ Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok, JOptionPane replaces system.out.println or scanner
 
Liutauras Vilda
Marshal
Posts: 8856
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

Enos Winder wrote:ok, JOptionPane replaces system.out.println or scanner

What do you mean?
 
Enos Winder
Greenhorn
Posts: 18
Netbeans IDE C++ Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, If I have a piece of code that looks like this:


I can make this same code look like this:


Or, I can use JOptionPane to do this:


This is what I mean when I say replace
 
Os Stewart
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm lost. When I rid the System output in, or scanner, (I'm fittling with it at this point) I remain with the same errors for salary and numDependents. If I get rid of Scanner, my input and my output, no longer work.
 
Os Stewart
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Enos Winder wrote:Ok, If I have a piece of code that looks like this:


I can make this same code look like this:


Or, I can use JOptionPane to do this:


This is what I mean when I say replace



Okay, let me try this...
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But why do you particularly want to use option pane for a console application? It is worth trying it out, but it adds a layer of complication. Particularly if the dialogue window appears behind something else. I have known it to do that, and I had to minimise everything else on screen to find it. I have learnt about that and know to look for the coffee cup icon on the taskbar/panel.
 
Os Stewart
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok this is what I did:



I can not get it to work. My salary and numDependents is still not working. Same errors as before. I've never used this JOptionPane before, so I don't know what to do. I'm just trying to get it to calculate my input from salary and dependents.
 
Enos Winder
Greenhorn
Posts: 18
Netbeans IDE C++ Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, In looking at your code. You have a lot of errors. First you ask for an employee name.... But you don't store that employee name in any variable. Second you have redundant variables. There is no need for redundant variables. If anything that will confuse the crap out of your program and it won't compile. Next you have some of your variables above your main and some below. Also you have some private and some just double. You really just need everything below your main and you don't have to identify it as a private or public. If you really want to then by all means make it public. by making it private you can run into trouble down the road. Remember, private only means that some parts of your program has access to it and others don't. by just declaring it a double variable with out any public or private then you get exactly what you want. Finally what I am trying to show you is to go in and do this to your code.


I tried to rewrite your code to actually work but here is what I have:


Now on the last line you will need a parse. That is not hard. Google "How do I parse in java?" It should come right up. I do this all the time it has never failed me yet.
 
Os Stewart
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Line 18 says that my salary and numDependents might not have been initialized.
 
Enos Winder
Greenhorn
Posts: 18
Netbeans IDE C++ Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
has your program asked for them?
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are trying too much all at once. You should get the salary working and add dependants later.
If you declare a local variable, the space on the stack for it might contain an old value, so it would be a corrupt value. If you assign something to your local variable, that will overwrite and delete any old values. If you say
numDependents = someNumberOrOther;

all will be well because it has been definitely assigned. If there are any if‑elses, every path must definitely assign a “fresh” local variable. Don't read this Java® Language Specification section because it is probably very difficult to understand.
 
Os Stewart
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm sorry you guys... I'm lost, I'm going to start from the last code with Scanner, and try to work from there. I'm not trying to make a pop up box right now, I'm just trying to understand the "Simple stuff" first. I'm sorry you guys, I'm trying to learn, but unfortunately it's a lot to take in for someone who hasn't done this much before. :-)
 
Enos Winder
Greenhorn
Posts: 18
Netbeans IDE C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the structure of your basic java program is this:

1. package name
2. imports
3. public class name
4. main
5. variables
6.methods
7.spit it all out


The only thing that you can really wiggle with in that structure is your main. You may occasionally call variables in different methods but only when you are using those variables in the method that is calling them. for example:
StringArray(String str, Array array);

In order for your program to be easy to read it needs to follow this structure. This helps if you find employment in this field. Heaven help us all if the code you write works but no one can read it. So for you to say it is doing to much at once isn't accurate. Further, the link that you sent is not hard to understand. It is just about like the mojave desert. DRY
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Enos Winder wrote:. . .
Now on the last line you will need a parse.
. . .

You don't need to parse anything in what you wrote. There are two reasons it won't work (probably more):-
  • 1: You are trying to sort out somebody else's code and only correcting part of it.
  • 2: You have salary uninitialised. When you get to the next line, the compiler thinks, “how much is salary? I don't know,” and won't compile any more.
  • If you can get input via Scanner, use Scanner.

    EW: option pane is a very old‑fashioned programming style. It is probably a bad idea to mix console input and GUI input anyway. You seem to be confusing OS by adding things she is unfamiliar with.
     
    Os Stewart
    Ranch Hand
    Posts: 71
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Campbell Ritchie wrote:

    Enos Winder wrote:. . .
    Now on the last line you will need a parse.
    . . .

    You don't need to parse anything in what you wrote. There are two reasons it won't work (probably more):-
  • 1: You are trying to sort out somebody else's code and only correcting part of it.
  • 2: You have salary uninitialised. When you get to the next line, the compiler thinks, “how much is salary? I don't know,” and won't compile any more.
  • If you can get input via Scanner, use Scanner.

    EW: option pane is a very old‑fashioned programming style. It is probably a bad idea to mix console input and GUI input anyway. You seem to be confusing OS by adding things she is unfamiliar with.





    Okay, when I started, this code was a mess. I got it from an old text book when I was in school. The lesson was to make it compile and fix the errors. I fixed the error, and the next lesson was to be able to input Salary and the number of Dependents. So I came up with this.



    My issue that I was having that was that the Salary and Number of Dependents are coming up with the errors, that they "might not have been initialized."

    I'm just trying to go on lesson by lesson to try to familiarize myself with the steps to "do the job", sort of speak. As the studies in the book go, more things will be added and so on.
     
    Enos Winder
    Greenhorn
    Posts: 18
    Netbeans IDE C++ Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    And this link as well: https://coderanch.com/t/567156/java/java/payroll
    This one is a bit more intense, but, you can easily get rid of what you don't want.

    Also this link:
    http://codereview.stackexchange.com/questions/15800/payroll-calculator

    When I am lost this is what I do. I start looking on the net for something that I can modify into an answer. This is what my teacher told me. "When you are out there in the real world it will be just you. Learn to use the internet now and your job won't be so hard later."
     
    Liutauras Vilda
    Marshal
    Posts: 8856
    637
    Mac OS X VI Editor BSD Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I have corrected your code indentation as well as formatting, also removed unnecessary curly braces.
    Also added comments, which are similar to steps you need to try to do. Compile your code often enough (after 2 - 4 lines of code). The sooner you'll notice mistake, the easier will be fix that.
    Try to use piece of paper and pencil, so you could work out on paper formulas, calculate them, so you'd have something to compare with your program output. Here is your indented code:

    You need to continue from here.
     
    Os Stewart
    Ranch Hand
    Posts: 71
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Okay, when I compiled it everything inputted fine, but it won't compile the sum of any of the math. Am I missing something here? My math looks right, but it won't total it?

     
    Os Stewart
    Ranch Hand
    Posts: 71
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    the printIn (sum) reads false?
     
    Liutauras Vilda
    Marshal
    Posts: 8856
    637
    Mac OS X VI Editor BSD Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    What are the values on lines 23, 24, 25 and 26?
     
    Os Stewart
    Ranch Hand
    Posts: 71
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I thought I had it but I don't. My values are the Salary and number of dependents. I thought?
     
    Consider Paul's rocket mass heater.
    reply
      Bookmark Topic Watch Topic
    • New Topic