trying to use JOptionPane as the input mode to find out if the input number is

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dynamiser
    New Member
    • Sep 2012
    • 3

    #1

    trying to use JOptionPane as the input mode to find out if the input number is

    i was trying to use JOptionPane as the input mode to find out if the input number is prime or not.. but i am stuck with this code..

    Code:
    package primeNo;
    import java.util.Scanner;
    import javax.swing.JOptionPane;
    public class primeNo {
    public static void main(String[] args) {
    int num;
    JOptionPane.showInputDialog("Please enter a number : ");
    Scanner user_num1=new Scanner(System.in);
    int i;
    for (i=2; i<num; i++){
    
    
    int n=num%i; //this is where it all went wrong
    
    
    if (n==0) {
    JOptionPane.showMessageDialog(null,"This is not a prime no.");
    break;
    }
    }
    if (i==num) {
    JOptionPane.showMessageDialog(null,"This is a prime no.");
    }
    }
    }
  • zmbd
    Recognized Expert Moderator Expert
    • Mar 2012
    • 5501

    #2
    Exactly what is your problem with the code.
    Errors
    Input is this and it gives that...
    And what have you already tried to troubleshoot?

    Determination of prime numbers is a normal homework problem.

    Comment

    • dynamiser
      New Member
      • Sep 2012
      • 3

      #3
      thanks ZMBD.. i am new to programming so i might not be very clear at defining the problem.. i was trying to make a program where i can enter a number using JOptionPane, the scanner takes in the input, system checks if the number is prime or not and shows the result in another messagebox.. so far, i have been able to enter a number but i have not been able to make the scanner take the input from the messagebox and return a value.. am i being clear here?? how do i do it??

      simple logic:

      1. run the program
      2. system asks user to input a number
      3. system takes the number and checks if it is a prime no
      4. returns a message saying if the number is prime or not..

      Comment

      • zmbd
        Recognized Expert Moderator Expert
        • Mar 2012
        • 5501

        #4
        hint... line 7... return is not assigned.

        This Swing Java Tutorial describes developing graphical user interfaces (GUIs) for applications and applets using Swing components

        should point you in the correct direction

        Comment

        Working...