Click to See Complete Forum and Search --> : Reading Odd and Even Integers


October 1st, 1999, 11:19 AM
I can't figure out what to put in here where the ?'s are to get it to read Odd and Even numbers...

import javax.swing.JOptionPane;

public class Even {
public static void main (String args[])
{
String num1;
int n1,odd,even;

num1 = JOptionPane.showInputDialog("Please type in an integer:");

n1 = Integer.parseInt(num1);
odd = ? ;
even = ? ;


if( ? )JOptionPane.showMessageDialog(
null,"The number is "+odd,"Results",JOptionPane.INFORMATION_MESSAGE);
else JOptionPane.showMessageDialog(
null,"The number is "+even,"Results",JOptionPane.INFORMATION_MESSAGE);

System.exit (0);
}
}

Please help!

scooby
October 1st, 1999, 12:07 PM
I am not exactly sure what you are trying to do in the code. Are odd and even accumulators to keep track of the number of even and odd numbers? Or are you just trying to determine if a number read in is even or odd? If you can answer these questions i can probably help you.

DHunter21
October 1st, 1999, 01:01 PM
If you are just trying to determine in the integer is an odd or an even then I would suggest:

n1 = Integer.parseInt(num);

boolean odd = (n1%2 == 1) ? true : false;

what this is doing is saying if "n1" Modulus 2 is equal to 1 then the answer is true (ie odd). A modulus (in case you do not know) divides by the number given and returns the remainder.

Hope it helps

Dustin