This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Beginning Java and the fly likes sorry ... i know you guys have seen this one many times ... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "sorry ... i know you guys have seen this one many times ..." Watch "sorry ... i know you guys have seen this one many times ..." New topic
Author

sorry ... i know you guys have seen this one many times ...

Tammy Kline
Greenhorn

Joined: Apr 07, 2007
Posts: 20
I did a search so I know this one is already out there, but this is the way I'm doing it and it's not working. Instead of copying/pasting someone else's and trying to work with it, I'd rather you helped me with mine. I'm not trying to waste anyone's time ... just want to know why the heck I can't get this thing to work!! (I'm definitely a java dummy, so please speak in simple terms!! REALLY!!) So you don't have to search on the others (and don't remember) the whole point of this code is to be able to put in dollars, euros (convert them to dollars), and yen (convert them to dollars) and keep looping and saving the answers until 4 is chosen at which time the total amount is spit out and the program exits.

pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
what happens when you try to run and compile this? What errors do you get? The more you tell us, the more we can help you.
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Also, your main() method is static (as it must be). You must make all of the variables that main references static. Either that or declare an instance of the class Gifts in your main method, but I don't think that that's what you have in mind. That's probably your main error.
[ April 07, 2007: Message edited by: pete stein ]
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Next you have a bunch of methods declared w/in methods. AFAIK this is a java no-no. Best to pull them out of the main method, declare them as static and within the class Gifts, but not within any other methods.
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
There are several other problems that I see.... I think that you need to start over w/ this, but first get a decent book on beginning Java (something like Head First Java will do nicely) and start working through the examples. And also find examples online as you have already discussed. It's ok to copy others work especially on your first few programs. See how its done right before banging away at the keys. If you learn bad habits, you perpetuate bad habits. Hang in there! Persevere and you'll succeed.
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
pseudocode on one way to do this:

[ April 07, 2007: Message edited by: pete stein ]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38472
    
  23
Welcome to the Ranch, Teresa.

Declare an instance of class Gift? That is probably the best thing to do.
Move everything out of the main method, and create a new method, let's call it go.That means you are actually writing object-oriented programming; you are creating an object.

Now that won't work, because as Pete Stein has correctly told you, you can't have methods inside methods.
I trust you have come across the switch block? You go as far as "input = stdin.nextInt();" Then put a switch block in for the four cases, and change your "wrongChoice" to the "default" part at the end.

Then pull out the dollar, yen and euro methods, which now become separate methods. So as your first option in the switch block you simply write "enterDollars();", etc, etc.

BTW: the arithmetic "dollars = myDollars + dollars;" won't work as you expect.

Good luck
[ April 07, 2007: Message edited by: Campbell Ritchie ]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38472
    
  23
I see Pete Stein and I are disagreeing with each other.
You will find that in computing there are always two ways (at least) to do anything. There are often lots of ways to do something.
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
My next bit of advice,... go w/ what Campbell Ritchie says. He knows a heck of a lot more than I do!
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Campbell Ritchie:
... You will find that in computing there are always two ways (at least) to do anything...

This is why it's binary.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Tammy Kline
Greenhorn

Joined: Apr 07, 2007
Posts: 20
Wow. I'm even more confused! I took a short break to see if I got a response yet, but I gotta get back to the family and won't have a chance to really look at this for another couple of hours. Thanks for you help ... I imagine you'll hear from me soon (once I get a chance to really read this over and think it through).

Thanks!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38472
    
  23
Only too pleased to help. We are not trying to confuse you, but remember that programming is a difficult activity. I shan't be around at least until tomorrow, maybe not until Monday.

Since you are familiar with the do . . . while block, I am presuming you are also familiar with the switch block. If not, have a look at this part of the Java Tutorial. The Java Tutorial is very useful, and may get you out of having to buy books at all, but it puts too much in the main method. Putting lots in the main method is convenient for writing examples because it takes less space, but it doesn't show you creating objects nor invoking their methods.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Campbell Ritchie:
...Putting lots in the main method is convenient for writing examples because it takes less space, but it doesn't show you creating objects nor invoking their methods.

Exactly. Procedural code in main serves as an easy way to demonstrate or test ideas, so you might see this in a lot of posts (or even books). But these are usually just sketches -- not blueprints. The ideas still need to be refined into object-oriented code.

I'm really tempted to dig into this one, but...
  • You've already received good advice to start with, and
  • I don't want to confuse things by adding anything now, and
  • I need to stop avoiding my tax return.
  • Okay, I really don't want to do my taxes, so I'll just comment on one thing that's standing out.

    When you define a method, the argument list goes inside the parentheses. This defines any parameters that you need to pass into the method. For example, if I wanted to pass an int value into a method, I might say...

    Here, I am passing an int value into the method. Inside the method, this value is referred to using the variable name "myInt."

    This is only the method definition. When I want to execute this method, I need to call it, and this is where I provide the value I want to pass in. For example...

    Now, look what you're doing...

    In your method definition, input == 2 is an expression that evaluates to a boolean. This is not a value that you want to pass into the method -- it is a condition that determines whether the method should be called. I don't see that you really need to pass anything into these methods (as they are written now), so you could define these with no arguments (an empty set of parentheses). For example, the method definition might look like...

    But then you need to call the method from somewhere else in your code. In this case, the method call should be inside some structure that determines whether it's appropriate to call the method. For example...

    That is, if input equals 2, then call enterEuros().
    [ April 07, 2007: Message edited by: marc weber ]
    Tammy Kline
    Greenhorn

    Joined: Apr 07, 2007
    Posts: 20
    Ok, so I changed it to the below and am now getting the following errors:

    ----jGRASP exec: javac -g C:\J24work\Gifts.java

    Gifts.java:45: illegal start of expression
    public void enterUSdollars() { //enter US dollars amount
    ^

    Gifts.java:66: ';' expected
    ^

    2 errors

    ----jGRASP wedge2: exit code for process is 1.
    ----jGRASP: operation complete.





    I had switches in there before, and it did the same thing. I'm obviously still not getting something right. grrr...

    I'm definitely not a programming kind of person ... sorry if this offends you programming types ... but I only took this class because I had to! I've got 2 more programs after this one to write ... and they are getting progressively harder! Yeah I know ... whine whine whine!! Sorry ...

    But I do appreciate all of the wonderful help!


    Keith Lynn
    Ranch Hand

    Joined: Feb 07, 2005
    Posts: 2367
    You can't put method declarations inside of methods.
    Tammy Kline
    Greenhorn

    Joined: Apr 07, 2007
    Posts: 20
    BTW: the arithmetic "dollars = myDollars + dollars;" won't work as you expect.


    Campbell ... what do you mean by that? Can you give me a hint as to what it's supposed to look like? I was told that by doing that it would save my "dollars" into another spot and add the new dollars together with it. Obviously not?

    All of this is like ... french ... to me.
    pete stein
    Bartender

    Joined: Feb 23, 2007
    Posts: 1561
    The problem is with these two lines:


    the first line wipes out any value in dollars and replaces it w/ the data entered from the stdin. The second line stores myDollars, an uninitialized variable + dollars back into dollars. The next time you run this, the dollars again will be wiped out and started afresh. Nowhere do you store the summed dollars. I think you would rather do this:


    Good luck with the French! Like any foreign language, the more you use it, the more fluent you will become.
    Tammy Kline
    Greenhorn

    Joined: Apr 07, 2007
    Posts: 20
    Thanks Pete!

    Now for Keith ... what do you mean I can't put methods inside methods? I'm seriously not very ... smart? ... when it comes to this stuff. I did not realize that I had methods inside methods. Can you give me an example as to what you are talking about? I know that someone else had referenced that ... but I'm at a loss as to what you mean.
    Keith Lynn
    Ranch Hand

    Joined: Feb 07, 2005
    Posts: 2367
    If you look at one of your methods

    ,

    you have this method declaration inside the do while loop.
    Tammy Kline
    Greenhorn

    Joined: Apr 07, 2007
    Posts: 20
    ok, but how do i pull them out of the do/while loop successfully? because i tried that already ... but still got the same errors that i'm currently receiving.
    Gregg Bolinger
    GenRocket Founder
    Ranch Hand

    Joined: Jul 11, 2001
    Posts: 15299
        
        6

    Typically, posting solutions is not what we do here. That is why everyone has been trying to help you and nudge you in the right direction. However, you are so close to your solution and are missing a few basic concepts. So here is your code. It compiles, but I didn't run it to see if it does what you except. It would be easier to explain some of the concepts you are missing with an easier example and by showing you the following, I hope it helps you understand a bit better what everyone has been explaining.

    What I did is moved all the code from the main method into the Gifts constructor. The main method now simply creates a new Gifts object which will automatically execute all the code in the constructor. This gets rid of your non-static variable errors.

    The next thing was to take all your methods (enterUSdollars(), etc) and move their declarations out of the do/while loop.



    GenRocket - Experts at Building Test Data
    Gregg Bolinger
    GenRocket Founder
    Ranch Hand

    Joined: Jul 11, 2001
    Posts: 15299
        
        6

    Sorry for the bad formatting on the last post. I didn't take the time to clean up from the copy and paste.
    Tammy Kline
    Greenhorn

    Joined: Apr 07, 2007
    Posts: 20
    Thanks! I finally got it to compile! WOOHOO!! I did run it ... and the only thing that's not doing right is that when I exit, instead of giving me the amount it all adds up to, it only gives me 0.0 ... but I'm determined to figure that one out on my own!!

    Thank you EVERYONE for you help! I definitely would not have figured this out on my own! I did learn some stuff, so that is good ... and I know what to look for next time (the method inside method problem).

    I can't say thank you enough!!
    pete stein
    Bartender

    Joined: Feb 23, 2007
    Posts: 1561
    Just to confuse you more, and to show that there's more than one way to skin a cat, here's another way to do your program, (it needs better error proofing though).
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 38472
        
      23
    Posted by Marc Weber:-
    You've already received good advice
    Thank you, Marc.

    Poasted by Pete Stein:-
    The problem is with these two lines: . . .
    That is exactly what I meant. If you haven't corrected that, you will either print out zero or the last amount you entered. Either that, or you don't add up the totalCollected until after you have printed it out.

    Posted by Teresa Dize:-
    I'm definitely not a programming kind of person ... sorry if this offends you programming types
    Those of use who are programming types remember what it was like to be beginners ourselves. I remember spending hours trying to work out what
    Gifts.java:45: illegal start of expression
    public void enterUSdollars() { //enter US dollars amount
    ^
    means, not realising I had simply put more {s than }s in the code. That is why we are willing to log on early on Sunday morning to try to help you.
    [ April 08, 2007: Message edited by: Campbell Ritchie ]
    Tammy Kline
    Greenhorn

    Joined: Apr 07, 2007
    Posts: 20
    Posted by Pete Stein:-

    quote:The problem is with these two lines: . . .


    I did finally fix this problem, but still hasn't fixed my problem of getting more than 0.0 when I exit out of the program. I'm working on it though! Well, will be later tonight ... being Easter, we are about to head up to PA to visit some family so technically I shouldn't even be on the computer right now (I should be getting ready!!).

    Those of use who are programming types remember what it was like to be beginners ourselves. I remember spending hours trying to work out what

    quote:Gifts.java:45: illegal start of expression
    public void enterUSdollars() { //enter US dollars amount
    ^

    means, not realising I had simply put more {s than }s in the code. That is why we are willing to log on early on Sunday morning to try to help you.



    And thank you Campbell! You guys are very dedicated to get up early on a Sunday morning to help me (and people like me)! And it's very appreciated by me, and I'm sure by many others!!

    I hope you all have a wonderful Sunday (and Happy Easter, too)!!

    Oh, and Pete ... thanks for the extra stuff to look at ... I'll definitely wait to add that confusion until after I've completely finished with the current (confusion) that I have

    All the help is great ... and even though slightly confusing at times, it's good to see that there is more than one way to skin a pig! (I know I was supposed to say cat...but pig sounds so much better!! ha!!)

    Thanks again guys!
    marc weber
    Sheriff

    Joined: Aug 31, 2004
    Posts: 11343

    Originally posted by Teresa Dize:
    ...still hasn't fixed my problem of getting more than 0.0 when I exit out of the program...

    This might seem so "obvious" once you see it, but you might drive yourself crazy in the meantime. Think: Flow of execution.

    //initialized to default value of 0.0...
    double totalCollected;
    ...
    //value printed out...
    System.out.println ("\nYour total amount collected in US dollars is:" + totalCollected + "Goodbye!");
    //reassigned new value...
    totalCollected = dollars + euros + yen;
    Tammy Kline
    Greenhorn

    Joined: Apr 07, 2007
    Posts: 20
    Originally posted by marc weber:

    This might seem so "obvious" once you see it, but you might drive yourself crazy in the meantime. Think: Flow of execution.

    //initialized to default value of 0.0...
    double totalCollected;
    ...
    //value printed out...
    System.out.println ("\nYour total amount collected in US dollars is:" + totalCollected + "Goodbye!");
    //reassigned new value...
    totalCollected = dollars + euros + yen;


    I've tried it both with totalCollected = dollars + euros + yen; and also with totalCollected = myDollars + myEuros + myYen; (since this last one is the actual place holders of the "collected" money) but both come out with the same at the end: 0.0 ... I'm wondering if maybe I have the reassignment sitting in the wrong place? It would seem to me that it should be under the following place:



    Am I wrong in that thought? It didn't work under the main section ... and I'm not sure where else to put it besides under the variables section, but that didn't work either. I even tried this:



    Same result. I'm about to go digging further into my book here to see if there's something that I missed. There has to be something!!
    pete stein
    Bartender

    Joined: Feb 23, 2007
    Posts: 1561


    What's going to print to the console here?
    Tammy Kline
    Greenhorn

    Joined: Apr 07, 2007
    Posts: 20
    Originally posted by pete stein:


    What's going to print to the console here?


    I feel a trick question coming on ...

    I'd say 2000.
    Tammy Kline
    Greenhorn

    Joined: Apr 07, 2007
    Posts: 20
    Ok, so I just did a little program on that, and it came back with 0. So. I was wrong and it should be 0.
    Tammy Kline
    Greenhorn

    Joined: Apr 07, 2007
    Posts: 20
    WOOHOOOOO!!! I FIGURED IT OUT!!! Thanks Pete! I'm slow, but I eventually catch on!!
    Tammy Kline
    Greenhorn

    Joined: Apr 07, 2007
    Posts: 20
    I was really confused as to why the answer was 0 ... but when I went back and actually looked I realized my mistake. I didn't even think about that (obviously) but I feel so much better now!! You guys are seriously really awesome. I have learned more from you guys in 24 hrs than I have in the past couple of weeks in this class. Thank you!!!
    pete stein
    Bartender

    Joined: Feb 23, 2007
    Posts: 1561
    Congrats! Pretty soon you'll be fluent at "parler" Java.
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 38472
        
      23
    From Pete Stein:
    Congrats! Pretty soon you'll be fluent at "parler" Java.
    Agree.

    From Teresa Dize:
    Thank you!!!
    Only too pleased to be able to help.

    Please post what you have finally produced; we would all like to see what you have achieved.
    Tammy Kline
    Greenhorn

    Joined: Apr 07, 2007
    Posts: 20
    Here's my "final product!" Thanks again!!

    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 38472
        
      23
    Thank you. It's all working now
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: sorry ... i know you guys have seen this one many times ...