# Program To Take Two Numbers and see if one is a factor of the other

Printable View

• 11-08-2011, 08:03 PM
Interista
Program To Take Two Numbers and see if one is a factor of the other
I think this program compiles ok up to the point of my final if (where I say that lower is a factor of higher... Then it gives a lot of errors. I'm not sure why.

Once I've cracked this I have to add an extra line (using else) which says lower goes into higher X times, but with Y left over. I've been shown how to work out a remainder statement, but where does the remainder go or how do I store it? Is it enough to include % in that message?

Thanks in advance of your kindness!

Code:

```import javax.swing.JOptionPane; public class Factors {         public static void main (String[] args)                 {                         String number1, number2;                         number1=JOptionPane.showInputDialog(null, "Enter first number:");                         int num1 = Integer.parseInt(number1);                         number2=JOptionPane.showInputDialog(null, "Enter second number:");                         int num2 = Integer.parseInt(number2);                         if (num1 < num2)                         {                         int higher = num2;                         int lower = num1;                         }                         else                         {                         int higher = num1;                         int lower = num2;                         }                         if (higher % lower == 0)                         {                         int division = higher / lower;                         JOptionPane.showMessageDialog(null, lower + " is a factor of " + higher + "( " + lower + " goes into " + higher + " exactly " + division + " times.)");                         }                         System.exit(0);                 }```
• 11-08-2011, 08:12 PM
Norm
Re: Program To Take Two Numbers and see if one is a factor of the other
Quote:

it gives a lot of errors.
Please post the full text of the errors if you need help correcting them.

One problem I see is that you have defined some variables inside of {} where other parts of the code can NOT see them.
Move the definitions out to be at the same level as where you want to use them in other pieces of code.
This is a problem in the scope of the definition. Variables inside of inner {} pairs are not known outside of those {}s
• 11-09-2011, 12:01 AM
Interista
Re: Program To Take Two Numbers and see if one is a factor of the other
I changed it as per your instructions but I still have problems.

It says

Factors.java:12: error: ' .class' expected
int higher=num2; (with an arrowhead under the h of higher)

Factors.java:12: error: not a statement
int higher=num2; (with an arrowhead under the i of int)

Factors.java:12: error: illegal start of expression
int higher=num2; (with an arrowhead under the = sign)

Factors.java:14: 'else' without 'if'
else (with an arrowhead under the e)

I have two questions - what am I doing wrong?

And I'm going to have to add a final statement which prints a message if the remainder is not 0 and says x goes into y so many times with this left over. I know % works out the remainder but how do I store this remainder?

Code:

```import javax.swing.JOptionPane; public class Factors {         public static void main (String[] args)                 {                         String number1, number2;                         number1=JOptionPane.showInputDialog(null, "Enter first number:");                         int num1 = Integer.parseInt(number1);                         number2=JOptionPane.showInputDialog(null, "Enter second number:");                         int num2 = Integer.parseInt(number2);                         if (num1 < num2)                         int higher=num2;                         int lower=num1;                         else                         int higher=num1;                         int lower=num2;                         if (higher % lower == 0)                         {                         int division = higher / lower;                         JOptionPane.showMessageDialog(null, lower + " is a factor of " + higher + "( " + lower + " goes into " + higher + " exactly " + division + " times.)");                         }                         System.exit(0);                 } }```
• 11-09-2011, 12:06 AM
Norm
Re: Program To Take Two Numbers and see if one is a factor of the other
You should use {} with your if/else statements and indent nested code. When you do add the {}s you will see where the problem is.
Your code being all in a vertical column makes it hard to understand the nesting of statements with the if / else statements