473,325 Members | 2,671 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

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


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
1 1706
PreethiGowri
126 64KB
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

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

Similar topics

0
by: TERMAN, IRA, ALABS | last post by:
Hello, I am trying to install Python-2.3. I get the following error when I run ./configure --with-csx=gcc on my Solaris machine. config.status: error: cannot find input file:...
1
by: nospam | last post by:
Hi, I have an application whose textareas rows automatically resize onFocus to the number of lines in the textArea. The cols are set to 100% (in a CSS file) in order to always take the full...
6
by: Peter Krikelis | last post by:
Hi All, I am having a problem setting up input mode for serial communications. (Sorry about the long code post). The following code is what I use to set up my comm port.
3
by: barkarlo | last post by:
how can I create input mask for invoice number for an example 07-08000-427 where 07-08000 is constant and 427 is changes. I would like write only number who is changes.
2
by: =?Utf-8?B?ZGxpbmdn?= | last post by:
I have a simple c# windows form with a textbox and button that, when clicked, displays a simple MessageBox. With the cursor in the textbox, I can select the Japanese language and desired Input...
2
by: BlackEyesBlueTears | last post by:
i've been asked to write a program to find the number of times that a given word occurs in a sentence.sentence and the word both are to be take as input from the user. for example: EnTER A...
5
debasisdas
by: debasisdas | last post by:
A ten-digit number contains every digit from 0 to 9. The digits are arranged so that the number formed by the first two digits, reading from left to right, is divisible by 2, the number formed by...
11
by: C C++ C++ | last post by:
Hi all, got this interview question please respond. How can you quickly find the number of elements stored in a a) static array b) dynamic array ? Rgrds MA
0
by: MattN | last post by:
Anyone know how to manipulate the language input mode editor that comes with windows and office? I need to change the input mode when a column gets focus.. direct - half width katakana and back...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.