Need help on a java program

VigilanteCS

Senior member
Dec 19, 2004
415
0
0
Hi
I'm in AP Java programming now, and we're doing a review of basic stuff. I havent taken java in over a year and i'm stumped on this program.

It's supposed to divide the smaller number by the bigger number (Big / small) and spit out the remainder, and terminate when I enter -1. I'm having trouble getting the remainder. Can anyone help me? Here's the code so far...
package divide;

import javax.swing.*;

public class divide
{
public static void main(String[] args)
{
String input, input2;
double one, two, answer;
int remainder;
input = JOptionPane.showInputDialog("Enter the first number");
input2 = JOptionPane.showInputDialog("Enter the second number");

one = Double.parseDouble(input);
two = Double.parseDouble(input2);

while(one != -1 || two != -1)
{
if(one > two)
{
answer = one / two;
JOptionPane.showMessageDialog(null, "The answer is " + answer);
System.exit(0);
}
else if(one < two)
{
answer = two / one;
JOptionPane.showMessageDialog(null, "The answer is " + answer);
System.exit(0);
}
}

}
}

I'm afraid of using mod(%) because I suck with it, but I think it's probably what I'm going to have to do. Thanks for any help. Also, it does not drop out when I enter -1, anybody know why? My bigger worry is the remainder now. All i can think of doing with mod is finding out if it's even or odd.
Thanks again!
 

cyclistca

Platinum Member
Dec 5, 2000
2,885
11
81
Not dropping out problem.

When you enter -1 for one of the values do you leave the other one black? If so that mean it contains a NULL value. The NULL value is probably causing you while statement to not be evaluated correctly.

Intialize varaibles one and two to 0 before you use them. This might fix your problem.
 

lozina

Lifer
Sep 10, 2001
11,711
8
81
Is the program supposed to keep prompting you for new numbers to divide until you enter -1?

If not, I don't know why you are using a while loop... If so, why do you call System.exit() after displaying the answer?
 

lozina

Lifer
Sep 10, 2001
11,711
8
81
Also, one more thing.. you said it needs to divide big by small and spit out remainder... well if you're using double you'll never have a remainder because there's always fractions. So instead of type casting into double you should type cast into int. (i.o.w. do int one = Integer.parseInt(...) instead)
 

Reel

Diamond Member
Jul 14, 2001
4,484
0
76
Use mod as said above to do the remainder. You also have some bad logic in your while evaluation. You are essentially telling it to continue if one is not equal to -1 OR two is not equal to -1. That means you would need to put both to -1 to trigger the exit condition of that loop. If you made it AND rather than OR, I believe it would have the desired effect. It would translate out to continue if one is not equal to -1 AND two is not equal to -1. It is difficult to explain well so I apologize if it is confusing.

Maybe someone else can explain this better without slipping into laws of logic? Regardless, give it a try and work it out for yourself to understand it.
 

VigilanteCS

Senior member
Dec 19, 2004
415
0
0
this I did realize, I changed it afterward. thanks for the help.
while(one != -1 && two != -1), is this correct, reelcool?, because it doesnt work =/.

I got the remainder working, I can't believe how simple that was.
 

VigilanteCS

Senior member
Dec 19, 2004
415
0
0
Okay my only problem left is terminating the loop, right now it terminates when I enter -1 for both, but sicne i have no if statement dealing with if the equal, it will do that when you enter to of the same numbers.
 

duragezic

Lifer
Oct 11, 1999
11,234
4
81
Why not just add a statement that will break; if they are equal? All you need is else { break; }
 

kamper

Diamond Member
Mar 18, 2003
5,513
0
0
If you want to have a loop then you have to prompt for more intput inside the loop. Otherwise it'll never stop when you finally do get it to continue.
 

kamper

Diamond Member
Mar 18, 2003
5,513
0
0
Well, what's the point of the loop? Do you want the user to put in more numbers after you show the result? If so you have to pop up two more input dialogs roughly as shown below. Also, don't forget to catch the NumberFormatException and check to make sure that the smaller number isn't 0 ;)

One other thing, your else clause should either read (one <= two) or simply not have a condition at all. You don't mean for it to fall through if the numbers are the same do you?