• Post Reply Bookmark Topic Watch Topic
  • New Topic

problem with breaking a for loop  RSS feed

 
Adam Kieft
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have been stuck on this for awhile now. It is supposed to be a factor generator.

Here is the code:



tester class:



output:



I apologize for the length of code. I cannot see what I am doing wrong.

Thanks in advance,

Adam
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch!

What is the problem you have? What error messages do you get? What does it do that you don't expect or what doesn't happen that you do expect? Please TellTheDetails (<-- link) so we can help.
 
Adam Kieft
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I apologize for that. In the output, you would see the line that says obtained. The number obtained must match the number expected. As you can see in the first case the obtained was 30 and the expected was 2. I am not sure why this happened. I assume its a problem with the loop or the return statement for the method hasMoreFactors.
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

I have removed some of the longer comments from your code because they make the lines too long, and they do not help us.
Where is this code from? Is it a partial attempt at a college assignment?
You appear to be using an inefficient method for calculating factors; you only need to iterate up to √n, and if you haven't found any factors, assume the argument is prime.
Please explain what the algorithm ought to be. You can never sort this sort of thing out by writing code; you have to work out the algorithm, which you do on paper. Write down exactly what the algorithm should be.
Your method for whether the number has more factors will return true if your value is a prime number e.g. 3, 529. I presume this is what you intended?

There is controversy about break;
Some people (myself included) don't like it, thinking it is a substitute for goto and breaches the conventions of structured programming, and other people think it is all right. You can terminate a loop by having composite condition. If you have not yet found a factor, continue the loop. You can put that test as part of the middle part of the for loop heading.
 
Adam Kieft
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is part of a college program yes. The problem is I did C++ about 7 years ago and have no programming experience since and can not for the life of me grasp this program.

The loop is now:



but it is still not working as expected and I don't know why.
 
Raymond Tong
Ranch Hand
Posts: 255
2
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem is on your nextFactor() method.
It didn't return the value as you expected.
So check this method on what it has returned against what it is expected.

Just forget about programming in Java and write down the logic on papers.
Then check against on what you have written in code.
You may then able to spot the problem easily.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!