Results 1 to 13 of 13
Like Tree4Likes
  • 1 Post By Iron Lion
  • 2 Post By Iron Lion
  • 1 Post By Iron Lion

Thread: Help!

  1. #1
    melinko928 is offline Member
    Join Date
    Sep 2011
    Posts
    8
    Rep Power
    0

    Exclamation Help!

    Java Code:
    import javax.swing.JOptionPane;
    public class Divideby3
    {
        public static void main(String args[])
        {
    
            int n1, n2, n2;
            int divisby3 = 0;
    
            String s1 = JOptionPane.showInputDialog("Enter first number ");
            n1 = Integer.parseInt(s1);
            s1 = JOptionPane.showInputDialog("Enter second number ");
            n2 = Integer.parseInt(s1);
            s1 = JOptionPane.showInputDialog("Enter third number ");
            n3 = Integer.parseInt(s1);
    
            if ( n1/3 != double)
            {
                divisby3 += 1;
            }
            if ( n2/3 != double)
            {
                divisby3 += 1;
            }
            if ( n3/3 != double)
            {
                divisby3 += 1;
            }
    
            JOptionPane.showMessageDialog(null,"There are " + divisby3 + " numbers divisible by three.");
        }
    }



    What do i do?
    the original question is :

    5. Write an application that inputs three numbers from a user and displays how many of the numbers are divisible by 3.
    Last edited by melinko928; 09-30-2011 at 10:32 PM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Help!

    Quote Originally Posted by melinko928 View Post
    What do i do?
    First of all (and it should seem obvious): tell us exactly what's wrong.

  3. #3
    melinko928 is offline Member
    Join Date
    Sep 2011
    Posts
    8
    Rep Power
    0

    Default Re: Help!

    \CIT270\Divideby3.java:17: error: '.class' expected
    if ( n1/3 != double);
    ^
    Z:\CIT270\Divideby3.java:21: error: '.class' expected
    if ( n2/3 != double)
    ^
    Z:\CIT270\Divideby3.java:25: error: '.class' expected
    if ( n3/3 != double)
    ^
    3 errors

    Tool completed with exit code 1

  4. #4
    melinko928 is offline Member
    Join Date
    Sep 2011
    Posts
    8
    Rep Power
    0

    Default Re: Help!

    Thats the errors I am getting

  5. #5
    Iron Lion is offline Senior Member
    Join Date
    Nov 2010
    Posts
    210
    Rep Power
    5

    Default Re: Help!

    Java Code:
    if ( n1/3 != double) { ... }
    I see what you're trying to do here, but it doesn't work that way:
    1: You can't test whether a variable is a primitive type by comparing it to the name of that type. If you're working with objects you can use the instanceof operator, but that won't help you here.
    2: Since n1 has been declared as int, n1/3 will also be int. If n1 is 7, then n1/3 will evaluate to 2, with the remainder of 1 being discarded.

    Instead, use the modulus operator (%) to test for the remainder which results from integer division.
    melinko928 likes this.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Help!

    OK, what are you trying to do with this line?

    if ( n1/3 != double)

    double is the name of a type, so this makes no sense.

  7. #7
    melinko928 is offline Member
    Join Date
    Sep 2011
    Posts
    8
    Rep Power
    0

    Default Re: Help!

    so....

    Java Code:
     if (n1/3 != %)

    ... im not sure if i understand?

  8. #8
    Iron Lion is offline Senior Member
    Join Date
    Nov 2010
    Posts
    210
    Rep Power
    5

  9. #9
    melinko928 is offline Member
    Join Date
    Sep 2011
    Posts
    8
    Rep Power
    0

    Default Re: Help!

    sorry, i just am not sure of how to test if a number will be evenly divisible by another, so i thought if i tested it not being equal to a decimal it may work....

  10. #10
    melinko928 is offline Member
    Join Date
    Sep 2011
    Posts
    8
    Rep Power
    0

    Default Re: Help!

    ok i think i got it...


    Java Code:
    if ( n153 == 0)
    		{
    			divisby3 += 1;
    		}
    		if ( n2%3 == 0)
    		{
    			divisby3 += 1;
    		}
    		if ( n3%3 == 0)
    		{
    			divisby3 += 1;
    		}

    right?

  11. #11
    Iron Lion is offline Senior Member
    Join Date
    Nov 2010
    Posts
    210
    Rep Power
    5

    Default Re: Help!

    Your calculation to check for divisibility by 3 is fine now, although I don't see where n153 is declared or why it needs to be 0.

    Edit: just noticed it's a typo for n1%3. Yep, that'll work, but your code could benefit from arrays and/or loops instead of writing the same condition three times for three different variables.
    Last edited by Iron Lion; 09-30-2011 at 10:56 PM.
    melinko928 likes this.

  12. #12
    melinko928 is offline Member
    Join Date
    Sep 2011
    Posts
    8
    Rep Power
    0

    Default Re: Help!

    Java Code:
    import javax.swing.JOptionPane;
    public class Divideby3
    {
    public static void main(String args[])
    {
    
    	int n1, n2, n3;
    	int divisby3 = 0;
    
    	String s1 = JOptionPane.showInputDialog("Enter first number ");
    	n1 = Integer.parseInt(s1);
    	s1 = JOptionPane.showInputDialog("Enter second number ");
    	n2 = Integer.parseInt(s1);
    	s1 = JOptionPane.showInputDialog("Enter third number ");
    	n3 = Integer.parseInt(s1);
    
    	if ( n1%3 == 0)
    	{
    		divisby3 += 1;
    	}
    	if ( n2%3 == 0)
    			{
    		divisby3 += 1;
    	}
    	if ( n3%3 == 0)
    	{
    		divisby3 += 1;
    	}
    
    JOptionPane.showMessageDialog(null,"There are " + divisby3 + " numbers divisible by three.");


    It works... you rock iron lion!!!
    thanks so much

  13. #13
    melinko928 is offline Member
    Join Date
    Sep 2011
    Posts
    8
    Rep Power
    0

    Default Re: Help!

    Quote Originally Posted by Iron Lion View Post
    Your calculation to check for divisibility by 3 is fine now, although I don't see where n153 is declared or why it needs to be 0.
    it was a typo! :)

Posting Permissions

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