This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes beginning methods help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "beginning methods help" Watch "beginning methods help" New topic
Author

beginning methods help

Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1659
    
  11

Heather,

how about we try again to stop thinking about code. Your new code looks nicer, but you still seem a bit confused....

let's think about what would happen if we were to tell a story.
This, by the way, is what everyone else was asking you to do.

"Once upon a time, our program started.
It asked for input.
The input wasn't -1.
It decided whether the input was even or odd.
It showed a dialog with the result.
It asked for input again.
The input wasn't -1.
It decided whether the input was even or odd.
It showed a dialog with the result.
It asked for input again.
The input was -1.
The program stopped.
The end"

Do you see where the loop should be now?


When you do things right, people won't be sure you've done anything at all.
Heather Hamrick
Greenhorn

Joined: Feb 21, 2012
Posts: 13
this is what I have in my main:



so is my logic right that you enter an integer, you call the method and it tests whether its odd/even, and then the loop continues if it isnt -1
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1659
    
  11

looks better.

You'll need to find a way to get that String turned into an int. The Integer class has a method you can use.

EDIT -- once you parse the value, you'll realize where your logic has a bit of a pitfall.
Heather Hamrick
Greenhorn

Joined: Feb 21, 2012
Posts: 13
which String, my input string??

I tried to run it, and it complies but it always returns odd and when I enter another number after the first it just adds another "This number is odd" to the first message so it says "this number is odd this number is odd"
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Heather Hamrick wrote:but it always returns odd


Of course it does. Once again, you didn't follow the advice given. Did you read the "story" presented? Did you try to get your code to follow that story? Did you examine your code, try to write what it does in story form, and see how it differs from the one presented?

Here's your current code's story.



Look at step 4, and what's inside its loop. Tell me what you see in there that will ever give you a result of even. Do you see one huge thing that's missing from there?

Now look at the loop in your code. Tell me which part of that will ever give you a result of even. Do you see something that you have to do every time through the loop that you're not doing?

And here's one possible hint, in case you're not totally missing something, but maybe just have a not-uncommon misconception:

When you call a method and assign the result to a variable, using that variable later does not call the method again:


Note that in the "y" and "z" lines there, we are NOT calling foo() again. It is NOT the case that whenever I use "x" it will replace it with "foo()". Some people think that's how it works, and if you were thinking that, I can see how you might have thought your loop would work. But if you weren't thinking that, then you just missed something huge.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: beginning methods help
 
Similar Threads
Odd-Even Number print program
getting and showing a wrapped value.
input number to array --not displaying right item [code included]
program able to compile and execute but output was wrong.
Simple Program not working.