# Positive Divisor of int

• 07-07-2008, 09:10 PM
starchildren3317
Positive Divisor of int
Hello Community,

I am a new java student. I recently began taking a java course a few weeks ago just to let you all know about where my knowledge with java is.

I am trying to create a program that will prompt the user (I am using JOptionPane) to enter in a number and then I need to get all the positive divisors of that number, seperate them between the even numbers and the odd numbers and output the result to the user. I am learning how to do loops at the moment.

So for example if the user typed in 100
I need the program to figure out that the positive divisors of 100 are: 1, 2, 4, 5, 10, 20, 25, 50, and 100.

This is where I am stuck.

Also, I have a fairly good idea of how to seperate numbers into even and odd by telling the program to % and if the remainder is 0 than the number is even and if it is not zero than the number is odd. However, I can't figure out how I would seperate the positive divisors that were calculated by the program.

For example, if the user entered in the numbers 1, 2, 4, 5, 10, 20, 25, 50, and 100 than I could use: switch (number % 2) and then use cases to seperate which were odd and even but not sure how I could put it into the switch after the program figured out the positive divisors.

Any tips, help, suggestions, examples would be really really appreciated. I have been working on this program for days and this is where I am really stuck.

Thanks again,
Newbie Java Student
• 07-07-2008, 10:12 PM
Nicholas Jordan
keep going
Quote:

Originally Posted by starchildren3317
I am a new java student. I recently began taking a java course a few weeks ago just to let you all know about where my knowledge with java is.

At a junior college?

Quote:

Originally Posted by starchildren3317
I am trying to create a program that will prompt the user (I am using JOptionPane) to enter in a number and then I need to get all the positive divisors of that number, seperate them between the even numbers and the odd numbers and output the result to the user. I am learning how to do loops at the moment.

Loops are a powerful construct. Getting divisiors is not unlike what you have already done to determine even/odd

Quote:

Originally Posted by starchildren3317
So for example if the user typed in 100 I need the program to figure out that the positive divisors of 100 are: 1, 2, 4, 5, 10, 20, 25, 50, and 100.

First divisor other than provided number cannot be greater than one-half of sample number. I suggest a brute-force approach to start off with:

Code:

`While number % --candidate divisor != 0 continue.`
The double minus mark is shorthand for number = number miinus one.

Quote:

Originally Posted by starchildren3317
This is where I am stuck.

Do you have any ideas no matter how crude? Also, check for division by zero before attempting modulo operator. Also, eliminate negative numbers. Also,....

This is how you get unstuck, program away conditions that cannot exist. The solution naturally falls out.

Quote:

Originally Posted by starchildren3317
Also, I have a fairly good idea of how to seperate numbers into even and odd by telling the program to % and if the remainder is 0 than the number is even and if it is not zero than the number is odd. However, I can't figure out how I would seperate the positive divisors that were calculated by the program.

Code:

`if ( number < 0 )`
Quote:

Originally Posted by starchildren3317
For example, if the user entered in the numbers 1, 2, 4, 5, 10, 20, 25, 50, and 100 than I could use: switch (number % 2) and then use cases to seperate which were odd and even but not sure how I could put it into the switch after the program figured out the positive divisors.

Switch approach for this is clumsy in hands of beginner. Use a loop, test for a condition.
• 07-07-2008, 11:44 PM
Norm
"have been working on this program for days"
If you post your code with coments on where you are having problems, it would help us help you.
• 07-08-2008, 04:13 AM
starchildren3317
I sort of follow you but I am still having a hard time figuring out how to do it.

To answer your question first: It is not a junior college but I have no IT background. After teaching myself HTML I decided that I wanted to learn Java so I signed up for a Java course.

Here is where I am at:

Code:

```import javax.swing.JOptionPane; import java.util.*; public class CH05EX16 {         public static void main(String[] args)         {                 int locker; //The locker number user inputs.                 int evens; //The number of closed lockers.                 int odds; //The number of open lockers.                 int number; //The positive divisor of locker.                 String inputMessage;                 String outputMessage;                 String inputStr;                                 inputMessage = "Enter the number of lockers "                                         + "in your school. \n"                                         + "To exit the program enter 0.";                 inputStr = JOptionPane.showInputDialog(inputMessage);                 locker = Integer.parseInt(inputStr);                                 while (locker != 0 )                 {                        }         } }```
So what the program is doing is that the user is entering in the number of lockers that are at their school. There is a game that is played and the result is that all the even number lockers end up being closed and all the odd number lockers end up being open.

I must use JOptionPane and leave it open so that the user can continue to put in any number of locker without having to restart the program.

So I have to figure out how to take the locker number and get its positive divisor. Then I must take all the positive divisors of the number and seperate them between the ones that are even and the ones that are odd numbers.
For each positive divisor I figured that I would use:

Code:

`(number % 2)`
This way I could tell that any number that == 0 was an even number and any number that != 0 was an odd number.

I then need to output:

Code:

```outputMessage = "The lockers that are closed are: " + evens + \n                       + "The lockers that are open are: " + odds; JOptionPane.showMessageDialog(null, outputMessage,                                             "The locker game",                                               JOptionPane.PLAIN_MESSAGE); inputMessage = "Enter a different number of lockers"                     + "To exit the program enter 0."; inputStr = JOptionPane.showInputDialog(inputMessage);                 locker = Integer.parseInt(inputStr); } //end while System.exit(0);```
• 07-08-2008, 03:11 PM
Nicholas Jordan
tedious work here, be patient
Okay, first thing is to write a separate class to do the data handling. This may not sound correct at first but that keeps us within standard practice. Just do NumericsClass nc = new NumericsClass(); in CH05EX16 and put get and set methods in, along with any method that helps you keep track of where you are. In other words, it goes where you have while( condition )

Do you have an editor?

Quote:

I must use JOptionPane and leave it open so that the user can continue to put in any number of locker without having to restart the program.
That is a JFrame or AWT program, that is what they do.

Quote:

So I have to figure out how to take the locker number and get its positive divisor. Then I must take all the positive divisors of the number and seperate them between the ones that are even and the ones that are odd numbers.
There is an absolute value method somewhere. Probabaly in java Math class. That, along with not starting off with a zero value should be your first few lines in ( Numerics class ) Using % to determine odd even is effective. What this program centers on is a widely studied problem of factoring numbers.

Quote:

I then need to output:
We can pass information in several ways. Do you know what a command line is? As well, we can have a data file, calcs can be initiated with a button in a JFrame program, a Dialog is a JFrame that does not stay on screen after the button is pushed. The way Dialog.getValue() works is the Dialog is reachable after the window on the screen is vanished.

Quote:

So I have to figure out how to take the locker number and get its positive divisor. Then I must take all the positive divisors of the number and seperate them between the ones that are even and the ones that are odd numbers.
That all goes in the Numerics Class
• 02-19-2009, 12:03 AM
Moniboniz
Hello www.java-forums.org visitors...
Hi people... :)
• 02-19-2009, 12:21 AM
MK12
Moniboniz: Only post if what you're saying is relevant. There is a different sub-forum for introductions, this isn't it.
OP(Original Poster): I have just recently wrote a program for many factor calculations ("divisors"), here is my method that gets the factors. Note there is a few things called there that are internal to my program that I'm not showing, but look at mine and see what I used to accomplish this and then try doing it yourself.
Code:

```private int[] getFactors(int number) {         ArrayList<Integer> factorList = new ArrayList<Integer>();         int numOfFactors = 0;         Integer[] factors;         for(int i = 1; i <= number; i++) {             if(i % number == 0) {                 factorList.add(new Integer(i));                 numOfFactors++;             }         }         factorList.trimToSize();         if(numOfFactors == 2) isPrime = true;         else isPrime = false;         factors = new Integer[factorList.size()];         return integerArrayToIntArray(factorList.toArray(factors)); }```
• 02-19-2009, 12:43 AM
emceenugget
there needs to be some kind of zombie post indicator here...
• 02-19-2009, 12:54 AM
MK12
What has he already figured out how to get the factors? Or did I not understand you?
-MK12
• 02-19-2009, 12:55 AM
CJSLMAN
Danger, Will Robinson, Danger ...!!!

huh... maybe a little too dramatic for a zombie alert?

CJSL
• 02-19-2009, 01:00 AM
emceenugget
it's a relatively old topic. and when a topic hasn't been touched for half a year, i think it means that the discussion is over. not sure why (or how) newcomers manage to often dig up old posts to introduce themselves.
• 02-19-2009, 01:11 AM
MK12
OOh didn't see date.. Its so annoying how people introduce themselves like this I've seen it quite a few times, not digging up old ones but just posting it in a random thread its like they don't have common sense...
• 02-19-2009, 01:23 AM
angryboy
haha its even scarier when you think about them becomming future programmers.
Why doesn't my door unlock? Why is my freezer spitting out ice every night at 12am?
Does anyone know why the zune got locked up on newyears eve?
• 02-19-2009, 03:16 PM
Nicholas Jordan
The Master at work.
After discussing agreement with the occasional random hello posting non-existent telephone numbers for discounted converter boxes As Seen on TV, we can see the work of Masters emerging at those random posts:
Quote:

Originally Posted by angryboy
haha its even scarier when you think about them becomming future programmers.

I think they did already, note typical systems you see routinely. e.g. - nt will not reload a dll ..... and so on.
Quote:

Originally Posted by angryboy
Why doesn't my door unlock? Why is my freezer spitting out ice every night at 12am?

How come my TV set only snows on me as of yesterday?
Quote:

Originally Posted by angryboy
Does anyone know why the zune got locked up on newyears eve?

Whois zune?
• 02-19-2009, 07:31 PM
angryboy
you're right, but i was only referring to those new members who post randomly, hijacking old threads.

what is a zune?
Quote:

You're gearing up to create a killer playlist on your 30GB Zune for your annual New Year's bash. All of a sudden, your Zune locks up, reboots itself, and freezes. What the heck is going on? ... a bug in the internal clock driver related to the way the device handles a leap year.
FROM: Microsoft Says Leap Year Bug Caused Zune Failures - PC World
• 02-19-2009, 08:23 PM
CJSLMAN
I think the Zune is MickeySoft's equivalent to iPOD....

CJSL
• 02-20-2009, 04:25 AM
Nicholas Jordan
what about the crawlers at the bottom
Quote:

Originally Posted by angryboy
you're right, but i was only referring to those new members who post randomly, hijacking old threads.

well what about the crawlers at the bottom when one posts - sometiimes they are very old ( 6+ mo ) and I took a few of them recently as the poster never got a reply. I know I have waited patiently sometimes for months hoping for a reply, as much as a year, and go back and check the work. It is rare, but sometimes the poster comes back with copious gratuity and I just try to pick the good ones.

I don't care much about whether op had a good frame, what it is is that some of the most profound questions in cs are touched by newbies sometimes - though it may need to be moderated I took one at jr and ( ... will pm you ... ) it can get combative, maybe an angry boy can answer what to do ....

some poster came in at jr asking what to do as new team lead, ...... I almost racked a pinion on that one, got into a discussion with someone who had ran a business for years. It went on for pages, I told my Branch Manager at the bank about it and she really grasped the subtle issues. Actually, that is not unlike what we are doing here with the big diff that op is gone and we have a better skill level in the codase. ( offhand employment for coder base )

I'm gonna be a definite league above where you are at but you are showing skills already that make for productive work, given: If you were New Team Lead - what would you do about the occasional intruder who posts non-existent telephone numbers for deeply discounted digital television converter boxes. Would you assume that person is from a derivative markets firm, just got home from work and wants to make distractions? I doubt it, I met one of those persons back when automated tele-dialers were new tech ~ many of them have an aversion to work that is systemic - they will do remarkable things, if it is to avoid work. Counter intuitive is the professional teminology, but actually it is deeper than that.

When you have a New Team Lead, there are issues involved that can only be handled by adroit skills that find their base in 50,000+ hours in a field. You do not have that, efforts to work the issue of where the border line is may assist in resolution of where the authorities on this forum want to go. You have already laid against IDE's, a common idiom in the cs field. When I started coding, I blew into the bowels of STL - before I could code int someint = 0; In there, I saw something anyone who works around machinery would recognize as a potential runaway pointer. Later, I got into a discussion with a cryptographer from a major institution. The result is that I got cited in scientific literature.

Let's see if you can get cited, is there anything you are excited about?

Other than the hack you mentioned.