I need some 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 bigger number by the smaller 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!
 

clamum

Lifer
Feb 13, 2003
26,252
403
126
Software forum

Attach code feature

"It's supposed to divide the smaller number by the bigger number (Big / small)" :confused:
 

dabuddha

Lifer
Apr 10, 2000
19,579
17
81
Originally posted by: VigilanteCS
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!

Dividing the smaller number by the bigger number means Small/Big
 

VigilanteCS

Senior member
Dec 19, 2004
415
0
0
Okay, the answer is supposed to be greater than one, so the big number is always diving the little number. okay i fixed it.
 

AgentEL

Golden Member
Jun 25, 2001
1,327
0
0
There is a software forum for this. Also a code view to look at code easier.

That said, you should probably use mod because that is how you get the remainder. Also, your while loop checks if "one" or "two" doesn't equal -1. However, you never change the value of "one" or "two" in the contents of the "while" loop.

I hope I understand your problem OK.
 

dabuddha

Lifer
Apr 10, 2000
19,579
17
81
Originally posted by: AgentEL
There is a software forum for this. Also a code view to look at code easier.

That said, you should probably use mod because that is how you get the remainder. Also, your while loop checks if "one" or "two" doesn't equal -1. However, you never change the value of "one" or "two" in the contents of the "while" loop.

I hope I understand your problem OK.

Plus he terminates after spitting out an answer which he doesn't want to.
Need to move your input code within the loop.
 

xirtam

Diamond Member
Aug 25, 2001
4,693
0
0
I used java.io instead of java.swing, but all you'd have to do is replace how the I/O is done. Doesn't catch division by zero, so if you have to do that, just check to see if smaller is 0 after testVars (or in testVars).

File: divide.java
====================
import java.io.*;

public class divide
{
public double larger=0, smaller=0;
public boolean cont=true;
public void Process()
{
int remainder=0;
while (this.cont)
{
getVars();
testVars();
remainder=(int) (this.larger % this.smaller);
System.out.println("The remainder is: " + remainder);
}
return;
}


private void getVars()
{
System.out.print("Enter the first number: ");
this.larger=readVar();
System.out.print("Enter the second number: ");
this.smaller=readVar();
return;
}

private double readVar()
{
String arg="";
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
try {
arg = br.readLine();
} catch IOException ioe) {
System.out.println("IO error reading first number");
System.exit(1);
}
if (cont)
this.cont=!(boolean)(Integer.parseInt(arg) == -1)
return Double.parseDouble(arg);
}

private void testVars(){
double dummy=0;
if (this.smaller > this.larger)
{
dummy = this.larger;
this.larger = this.smaller;
this.smaller = dummy;
}
}
}



File: execute.java (or whatever front end you want to put on this thing)
==================

import divide;
public class execute
{
public static void main(String args[])
{
divide pid = new divide();
pid.Process();
System.out.println("Exit code given... terminating normally.");
System.exit(0);
}
}


Compile both. Run the execute class. Game over.