Results 1 to 10 of 10

Thread: Java Application

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2008
    Posts
    6

    Java Application

    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

  2. #2
    PowerPoster kfcSmitty's Avatar
    Join Date
    May 2005
    Posts
    2,248

    Re: Java Application

    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.

  3. #3

    Thread Starter
    New Member
    Join Date
    Nov 2008
    Posts
    6

    Re: Java Application

    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.
    Last edited by tausby; Nov 11th, 2008 at 04:34 PM.

  4. #4
    PowerPoster kfcSmitty's Avatar
    Join Date
    May 2005
    Posts
    2,248

    Re: Java Application

    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.

    Code:
    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

  5. #5
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: Java Application

    Same idea as kfcSmitty's but this one is a command line example
    Code:
    import 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");
    	}
    
    }
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  6. #6

    Thread Starter
    New Member
    Join Date
    Nov 2008
    Posts
    6

    Re: Java Application

    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.
    Last edited by tausby; Nov 11th, 2008 at 09:09 PM.

  7. #7
    PowerPoster kfcSmitty's Avatar
    Join Date
    May 2005
    Posts
    2,248

    Re: Java Application

    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:

    Code:
    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.

  8. #8

    Thread Starter
    New Member
    Join Date
    Nov 2008
    Posts
    6

    Re: Java Application

    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

  9. #9
    PowerPoster kfcSmitty's Avatar
    Join Date
    May 2005
    Posts
    2,248

    Re: Java Application

    Code:
    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.

  10. #10

    Thread Starter
    New Member
    Join Date
    Nov 2008
    Posts
    6

    Re: Java Application

    Thanks a bunch you have been a big help

Posting Permissions

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



Click Here to Expand Forum to Full Width