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.

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(;
  9. int i;
  10. for (i=2; i<num; i++){
  13. int n=num%i; //this is where it all went wrong
  16. if (n==0) {
  17. JOptionPane.showMessageDialog(null,"This is not a prime no.");
  18. break;
  19. }
  20. }
  21. if (i==num) {
  22. JOptionPane.showMessageDialog(null,"This is a prime no.");
  23. }
  24. }
Sep 17 '12 #1
Share this Question
Share on Google+
3 Replies

Expert Mod 5K+
P: 5,397
Exactly what is your problem with the code.
Input is this and it gives that...
And what have you already tried to troubleshoot?

Determination of prime numbers is a normal homework problem.
Sep 17 '12 #2

P: 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..
Sep 17 '12 #3

Expert Mod 5K+
P: 5,397
hint... line 7... return is not assigned.
should point you in the correct direction
Sep 17 '12 #4

Post your reply

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