Results 1 to 13 of 13
- 09-30-2011, 09:22 PM #1
Member
- Join Date
- Sep 2011
- Posts
- 8
- Rep Power
- 0
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 09:32 PM.
-
Re: Help!
- 09-30-2011, 09:41 PM #3
Member
- Join Date
- Sep 2011
- Posts
- 8
- Rep Power
- 0
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
- 09-30-2011, 09:42 PM #4
Member
- Join Date
- Sep 2011
- Posts
- 8
- Rep Power
- 0
Re: Help!
Thats the errors I am getting
- 09-30-2011, 09:42 PM #5
Senior Member
- Join Date
- Nov 2010
- Posts
- 210
- Rep Power
- 3
Re: Help!
I see what you're trying to do here, but it doesn't work that way:Java Code:if ( n1/3 != double) { ... }
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.
-
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.
- 09-30-2011, 09:47 PM #7
Member
- Join Date
- Sep 2011
- Posts
- 8
- Rep Power
- 0
Re: Help!
so....
Java Code:if (n1/3 != %)
... im not sure if i understand?
- 09-30-2011, 09:48 PM #8
Senior Member
- Join Date
- Nov 2010
- Posts
- 210
- Rep Power
- 3
- 09-30-2011, 09:48 PM #9
Member
- Join Date
- Sep 2011
- Posts
- 8
- Rep Power
- 0
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....
- 09-30-2011, 09:51 PM #10
Member
- Join Date
- Sep 2011
- Posts
- 8
- Rep Power
- 0
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?
- 09-30-2011, 09:54 PM #11
Senior Member
- Join Date
- Nov 2010
- Posts
- 210
- Rep Power
- 3
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 09:56 PM.
- 09-30-2011, 09:55 PM #12
Member
- Join Date
- Sep 2011
- Posts
- 8
- Rep Power
- 0
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
- 09-30-2011, 09:56 PM #13
Member
- Join Date
- Sep 2011
- Posts
- 8
- Rep Power
- 0


4Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks