Click to See Complete Forum and Search --> : Java Application
tausby
Nov 11th, 2008, 02:14 PM
Hello I'm new to Java and I was wondering how to Write a Java program that reads an integer and checks whether it is even. Do I use the Boolean Theory? I'm totally lost here
kfcSmitty
Nov 11th, 2008, 02:49 PM
You can use modulus to determine if the number is even or odd. Simply mod the number by 2. If it returns 0 it is even, otherwise it is false.
If you can show some code, we can help you tweak it.
tausby
Nov 11th, 2008, 03:24 PM
Here is what i have so far. I was trying to add the Option Pane box along with it too. Also the program I'm running the application is jgrasp
public class CountDivisors {
/* This program reads a positive integer from the user.
It checks if the number is even and true
*/
public static void main(String[] args) {
int N; // A positive integer entered by the user.
// Divisors of this number will be counted.
int testEvenNum; // A number between 1 and N that is a
// possible even of N.
int maxNum; // Number of divisors of N that have been found.
int numberTested; // Used to count how many possible numbers
// of N have been tested. When the number
// reaches 1000000, a period is output and
// the value of numberTested is reset to zero.
/* Get a positive integer from the user. */
while (true) {
JOptionpane.showInputDialog("Enter a positive integer: ");
N = TextIO.getlnInt();
if (N > 0)
break;
JOptionpane.showInputDialog("false");
}
/* Count the divisors, printing a "." after every 1000000 tests. */
divisorCount = 0;
numberTested = 0;
for (testDivisor = 1; testDivisor <= N; testDivisor++) {
if ( N % testDivisor == 0 )
divisorCount++;
numberTested++;
if (numberTested == 1000000) {
TextIO.put('.');
numberTested = 0;
}
}
/* Display the result. */
TextIO.putln();
TextIO.putln("The number of divisors of " + N
+ " is " + divisorCount);
} // end main()
} // end class CountDivisors
Here are the following errors i recieved from jgrasp
----jGRASP exec: javac -g I:\CountDivisors.java
CountDivisors.java:25: cannot find symbol
symbol : variable JOptionpane
location: class CountDivisors
JOptionpane.showInputDialog("Enter a positive integer: ");
^
CountDivisors.java:26: cannot find symbol
symbol : variable TextIO
location: class CountDivisors
N = TextIO.getlnInt();
^
CountDivisors.java:29: cannot find symbol
symbol : variable JOptionpane
location: class CountDivisors
JOptionpane.showInputDialog("false");
^
CountDivisors.java:34: cannot find symbol
symbol : variable divisorCount
location: class CountDivisors
divisorCount = 0;
^
CountDivisors.java:37: cannot find symbol
symbol : variable testDivisor
location: class CountDivisors
for (testDivisor = 1; testDivisor <= N; testDivisor++) {
^
CountDivisors.java:37: cannot find symbol
symbol : variable testDivisor
location: class CountDivisors
for (testDivisor = 1; testDivisor <= N; testDivisor++) {
^
CountDivisors.java:37: cannot find symbol
symbol : variable testDivisor
location: class CountDivisors
for (testDivisor = 1; testDivisor <= N; testDivisor++) {
^
CountDivisors.java:38: cannot find symbol
symbol : variable testDivisor
location: class CountDivisors
if ( N % testDivisor == 0 )
^
CountDivisors.java:39: cannot find symbol
symbol : variable divisorCount
location: class CountDivisors
divisorCount++;
^
CountDivisors.java:42: cannot find symbol
symbol : variable TextIO
location: class CountDivisors
TextIO.put('.');
^
CountDivisors.java:49: cannot find symbol
symbol : variable TextIO
location: class CountDivisors
TextIO.putln();
^
CountDivisors.java:51: cannot find symbol
symbol : variable divisorCount
location: class CountDivisors
+ " is " + divisorCount);
^
CountDivisors.java:50: cannot find symbol
symbol : variable TextIO
location: class CountDivisors
TextIO.putln("The number of divisors of " + N
^
13 errors
----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.
kfcSmitty
Nov 11th, 2008, 04:22 PM
I have no idea what you're trying there. You don't even have the necessary imports to accomplish what you need. And why use textIO to output the result if you've already imported swing? Give them a friendly dialog.
The code isn't indented and it seems much too long for what you're attempting. Unless I'm missing something, all you should need is.
import javax.swing.*;
public class CountDivisors {
/* This program reads a positive integer from the user.
It checks if the number is even and true
*/
public static void main(String[] args) {
String ans;
ans = JOptionPane.showInputDialog(null, "Enter a number");
int input= Integer.parseInt(ans);
input = input % 2;
String answer = "The number is not even";
if(input == 0){
answer = "The number is even";
}//end if
JOptionPane.showMessageDialog(null, answer);
} // end main()
} // end class CountDivisors
ComputerJy
Nov 11th, 2008, 07:05 PM
Same idea as kfcSmitty's but this one is a command line exampleimport java.util.Scanner;
public class Test
{
public static void main(final String arg[])
{
System.out.println("Enter a number: ");
final int value = new Scanner(System.in).nextInt();
System.out.println(value % 2 == 0 ? "Even" : "Odd");
}
}
tausby
Nov 11th, 2008, 08:06 PM
ok. I can understand what you saying. In other words "just keep it simple" But what if you want to add another program to an exsiting? say for example, In this program i want to write program that check if the number the user entered is between 1 and 1000. can i use the same code but it not coming out right. here is the code that I added with the code you sent me:
import javax.swing.*;
public class CountDivisors {
/* This program reads a positive integer from the user.
It checks if the number is even and true
*/
public static void main(String[] args) {
String ans;
ans = JOptionPane.showInputDialog(null, "Is this number even?");
ans = JOptionPane.showInputDialog(null, "Is This Number Between 1 and 1000?");
int input= Integer.parseInt(ans);
input = input % 2;
if(input < 1);
String answer = "false";
if(input == 0);
answer = "True";
if(input <=1000);{
answer = "True";
}//end if
JOptionPane.showMessageDialog(null, answer);
} // end main()
} // end class CountDivisors
When I test the application i put in an odd number or a number that is not between 1 and 1000 the dialog box said true i know i messed up some where but just don't know where.
kfcSmitty
Nov 11th, 2008, 08:26 PM
You're putting both #'s into "ans."
In that case, the variable ans will always contain the value for the question "Is This Number Between 1 and 1000?"
the following line:
ans = JOptionPane.showInputDialog(null, "Is This Number Between 1 and 1000?");
Puts the value entered in the input dialog into the variable ans.
If you want to get 2 values, you will either need to use 2 variables or you will need to finish your calculation with the first number before replacing it.
tausby
Nov 11th, 2008, 08:45 PM
Ok how can you use two varibles and which on i should i use? I want to show two different dialog boxes with two different answers
kfcSmitty
Nov 11th, 2008, 08:54 PM
String ans;
String ans2;
ans = JOptionPane.showInputDialog(null, "Is this number even?");
ans2 = JOptionPane.showInputDialog(null, "Is This Number Between 1 and 1000?");
Is using 2 different variables for the input. I would suggest you Google some tutorials on basic Java programming and get a handle on exactly what the code is doing before you continue any further.
tausby
Nov 11th, 2008, 09:30 PM
Thanks a bunch you have been a big help
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.