i was 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

    i was 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.");
    }
    }
    }
    Last edited by Rabbit; Nov 6 '12, 04:14 PM. Reason: Please use code tags when posting code.
  • PreethiGowri
    New Member
    • Oct 2012
    • 126

    #2
    i have altered it, it works fine now:)
    Code:
    package prime;
    import java.util.Scanner;
    import javax.swing.JOptionPane;
    public class Prime {
    public static void main(String[] args) {
    String str = JOptionPane.showInputDialog("Please enter a number : ");
    int num1 = Integer.parseInt(str);
    Scanner user_num1=new Scanner(System.in);
    int i;
    for (i=2; i<num1; i++){
    
    
    int n=num1%i; //this is where it all went wrong
    
    
    if (n==0) {
    JOptionPane.showMessageDialog(null,"This is not a prime no.");
    break;
    } 
    }
    if (i==num1) {
    JOptionPane.showMessageDialog(null,"This is a prime no.");
    }
    }
    }

    Comment

    Working...