By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,805 Members | 1,241 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,805 IT Pros & Developers. It's quick & easy.

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

P: 3
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..


Expand|Select|Wrap|Line Numbers
  1. package primeNo;
  2. import java.util.Scanner;
  3. import javax.swing.JOptionPane;
  4. public class primeNo {
  5. public static void main(String[] args) {
  6. int num;
  7. JOptionPane.showInputDialog("Please enter a number : ");
  8. Scanner user_num1=new Scanner(System.in);
  9. int i;
  10. for (i=2; i<num; i++){
  11.  
  12.  
  13. int n=num%i; //this is where it all went wrong
  14.  
  15.  
  16. if (n==0) {
  17. JOptionPane.showMessageDialog(null,"This is not a prime no.");
  18. break;
  19. }
  20. if (i==num) {
  21. JOptionPane.showMessageDialog(null,"This is a prime no.");
  22. }
  23. }
  24. }
Sep 14 '12 #1
Share this Question
Share on Google+
1 Reply


100+
P: 126
i have altered it, it works fine now:)
Expand|Select|Wrap|Line Numbers
  1. package prime;
  2. import java.util.Scanner;
  3. import javax.swing.JOptionPane;
  4. public class Prime {
  5. public static void main(String[] args) {
  6. String str = JOptionPane.showInputDialog("Please enter a number : ");
  7. int num1 = Integer.parseInt(str);
  8. Scanner user_num1=new Scanner(System.in);
  9. int i;
  10. for (i=2; i<num1; i++){
  11.  
  12.  
  13. int n=num1%i; //this is where it all went wrong
  14.  
  15.  
  16. if (n==0) {
  17. JOptionPane.showMessageDialog(null,"This is not a prime no.");
  18. break;
  19. }
  20. if (i==num1) {
  21. JOptionPane.showMessageDialog(null,"This is a prime no.");
  22. }
  23. }
  24. }
Nov 6 '12 #2

Post your reply

Sign in to post your reply or Sign up for a free account.