*
The moose likes Java in General and the fly likes factor program Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "factor program" Watch "factor program" New topic
Author

factor program

Ashley Griffin
Greenhorn

Joined: Jun 24, 2012
Posts: 3
Hi,
My instructions for my program is:
Create an application that will repeatedly read in positive numbers from the user, until the user enters a 0. For each number, the program will list out the proper factors of the number.
I can't figure out how to work all the kinks out of my program. For example when I enter a number like 9, it return 9, 1.
Here is my program:
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18855
    
  40

Ashley Griffin wrote:Hi,
My instructions for my program is:
Create an application that will repeatedly read in positive numbers from the user, until the user enters a 0. For each number, the program will list out the proper factors of the number.
I can't figure out how to work all the kinks out of my program. For example when I enter a number like 9, it return 9, 1.


Hint: is there a difference when you enter 9 the first time? And when you enter 9 after you enter 1 to continue?

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Ashley Griffin
Greenhorn

Joined: Jun 24, 2012
Posts: 3
I not quite sure that I understand your hint.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18855
    
  40

Ashley Griffin wrote:I not quite sure that I understand your hint.


How about this? When you first run your program, your divisor starts off as 2, and get incremented. When you continue -- meaning the same program is processing another number, what is the value of divisor starting at? Or what should the value of divisor start at?

Henry
Ashley Griffin
Greenhorn

Joined: Jun 24, 2012
Posts: 3
The divisor should increase by one during each run starting from 2 to 3 and so on.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11328
    
  16

I think you are trying to do too much at once here. The most important thing one needs to learn to be a programmer is how to separate out the various pieces of the project into their component parts. For example, I would (at minimum) break this down into at least three parts:

1) Prompt and get input from a user exactly once.
2) loop indefinitely until some flag value is set (in your case, it will be a "0")
3) assuming you have an integer, find and print the factors.

notice that each of these can be written without a concern for what or how the other two work. #3 doesn't care if you get the data form a user, read it from a file, query a database, or pick a number at random - all it cares is that you pass it an integer, and it will find/print the factors. Personally, I would probably even split it into 3a) find the factors and return them as a list, and 3b) given a list of integers, print them out.

So what I would do is start over. use what you've written as a crib, but begin fresh. The I'd start by having a main method that only calls another method - something like "private static int getUserInput()". This method doesn't need any parameters passed to it, but will return an input. Then have your main print the value it gets back.

On your first draft of getUserInput(), have it do nothing but return value of "5". Once you know you can call it, get back a value, then you can refine it to prompt and get input from the user, and return that. Eventually, you'll have it working perfectly.

ONLY WHEN IT DOES WORK PERFECTLY should you consider adding the code for #2. It would be as simple a writing a loop around the calling of the method, and checking the returned value.

ONLY WHEN THE LOOPING is working perfectly, add in a call that gets/prints the factors. Again, my first draft of getFactors() would probably do nothing more than print "i'm finding the factors of X" - where you print out the value you are trying to factor.


The point here is that each piece needs to be developed separately. It is an iterative and refine process - never try to write you code in one fell swoop.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
 
 
subject: factor program