aspose file tools*
The moose likes Beginning Java and the fly likes Problem with Loop Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Problem with Loop" Watch "Problem with Loop" New topic
Author

Problem with Loop

Andreas Beccerino
Greenhorn

Joined: Nov 16, 2009
Posts: 7
To solve a task I need to loop a method.



I intended to count how often einmaligerTest returns 3. Afterwards the programm was supposed to divide benuterEingabe (userInput) by i to get the average.
However, it doesnt't work the way i wanted it to. In the end i is always 0 and therefore the division returns an error. What did I do wrong?
W. Joe Smith
Ranch Hand

Joined: Feb 10, 2009
Posts: 710
You are never reaching into your if statement, if i is always coming back 0. Can you show the code for einmaligerTest()?


SCJA
When I die, I want people to look at me and say "Yeah, he might have been crazy, but that was one zarkin frood that knew where his towel was."
Andreas Beccerino
Greenhorn

Joined: Nov 16, 2009
Posts: 7
Here it is:




So I need to call the einmaligerTest method before the if statement?

The general task is to create a devise which can test how save cash machines are. It creates 4 random numbers and compares it to 4 other numbers. After 3 failed it attempts it will create 4 new numbers to check. This should continue multiple times depending on user input.
W. Joe Smith
Ranch Hand

Joined: Feb 10, 2009
Posts: 710
I don't know if this is your problem (not going to lie, I'm having a small language problem), but your if statement looks like this:



Shouldn't it be like



with a { after the else? Not sure on that, but I think it is needed.
Andreas Beccerino
Greenhorn

Joined: Nov 16, 2009
Posts: 7
I tried to replace german variable names with englisch ones:




Hope that helps.
The general task is to create a devise which can test how save cash machines are. It creates 4 random numbers and compares it to 4 other numbers. After 3 failed attempts (the smartcards gets eaten by the cash machine) it will create 4 new numbers to check. This should continue multiple times depending on user input.
meinRandom creates random numbers and works fine.
input gets the user input and works fine too.

edit: Adding the brackets to the else-statements does not work either
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11417
    
  16

can you try this:


In other words, verify that your einmaligerTest is returning what you think it should.

System.out.println() is your friend when debugging!!!

also... you are using ints. an int divided by an int gives you an int. if you are expecting a fraction between 0 and 1, it will always be rounded down to 0. try something like

return (float)userInput / i;

to cast one of the variable to a float, which will then force the result to be a float as well. Of course, you may have to change your return type...


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Andreas Beccerino
Greenhorn

Joined: Nov 16, 2009
Posts: 7
I think nonrecurringTest works the way it's supposed to. It either returns a 3, which means the number was not guessed, or a 0,1,2, which means the number was guessed. So in multipleTest he should count the fail attempts to use for the devision. However, i stays zero no matter how many failed attempts there where.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11417
    
  16

I would still suggest you liberally sprinkle System.out.println()s into your code. Verify what the method is returning. verify you step into your if condition. Print out what the value of i is before your "i = i + 1;" and again after.

You can't really debug your code until you know what it is doing.

if you have a debugger and know how to use it, step through the code line by line. If not, the println()s are the way to go.
Andreas Beccerino
Greenhorn

Joined: Nov 16, 2009
Posts: 7
I think somehow I really don't spring into the if-clause but I can't find the reason.
Here is another method of the class which works just fine



It counts how often 3 is returned form nonrecurringTest (number of failed attempts) and returns that number.
I think the structure is quite close to multipleTest.
Why is only one of them working?

edit:
I think I figures it out. My for-loop was wrong. I get results by modifying it this way:


Thanks for helping though
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Problem with Loop