Results 1 to 4 of 4
  1. #1
    Interista is offline Member
    Join Date
    Oct 2011
    Posts
    24
    Rep Power
    0

    Default 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!

    Java 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);
    		}

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,570
    Rep Power
    25

    Default Re: Program To Take Two Numbers and see if one is a factor of the other

    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
    Last edited by Norm; 11-08-2011 at 07:15 PM.

  3. #3
    Interista is offline Member
    Join Date
    Oct 2011
    Posts
    24
    Rep Power
    0

    Default 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?

    Java 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);
    		}
    }

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,570
    Rep Power
    25

    Default 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

Similar Threads

  1. Factor listing issues
    By skaterboy987 in forum New To Java
    Replies: 10
    Last Post: 10-30-2011, 12:38 AM
  2. Replies: 10
    Last Post: 12-20-2010, 12:47 PM
  3. need help with factor quadratic...
    By frostkarrotor in forum New To Java
    Replies: 3
    Last Post: 11-30-2010, 12:28 AM
  4. greatest prime factor
    By java_prgr in forum New To Java
    Replies: 2
    Last Post: 07-23-2010, 08:28 PM
  5. prime numbers program
    By i contra i in forum New To Java
    Replies: 9
    Last Post: 01-15-2009, 07:22 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •