one last tiny little detail

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Sep 2004
Posts: 10
Reputation: Jason Marceau is an unknown quantity at this point 
Solved Threads: 0
Jason Marceau Jason Marceau is offline Offline
Newbie Poster

one last tiny little detail

 
0
  #1
Sep 28th, 2004
Hi,

I have written a program that accepts a positive integer as input and determines whether or not the number is prime, it all works accept when a number is prime it should display " x is a prime number " and continue to prompt for user input ( loop ) but it will only display " please enter a number"

Could you have a look at my code and see what I am missing or what I have done wrong please?

Thank you!

Here is the code:


import java.io.*;

public class PrimalityTest
{

public static void main(String[] args)throws IOException{


// create a keyboard input stream
BufferedReader stdin = new BufferedReader (new
InputStreamReader(System.in));

// variables to store input
int Input;



do
{

// prompt for user input
System.out.print("Please enter a number: ");
Input = Integer.parseInt(stdin.readLine());

// test for prime number

for (int i = 2; i < Input; i++)
if ((Input % i) == 0)
{
System.out.println(Input + " is not a prime number");
return;
}
else
System.out.println(Input + " is a prime number");

// if number is 0 then exit
if (Input == 0)
{
System.out.println("Exiting program...");
}
else
// if number is negative
if (Input <= -1)
{
System.out.println("Please enter a positive integer.");
}

}

while (Input !=0);

}
}
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 84
Reputation: jerbo is an unknown quantity at this point 
Solved Threads: 1
jerbo jerbo is offline Offline
Junior Poster in Training

Re: one last tiny little detail

 
0
  #2
Sep 28th, 2004
First clue to you is to use 'break' and not 'return'
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 84
Reputation: jerbo is an unknown quantity at this point 
Solved Threads: 1
jerbo jerbo is offline Offline
Junior Poster in Training

Re: one last tiny little detail

 
0
  #3
Sep 28th, 2004
Here is the completed code I worked out. Note - I expanded to a long to enable you to enter large numbers.

  1. import java.io.*;
  2.  
  3. public class PrimalityTest {
  4.  
  5. public static void main(String[] args) throws IOException {
  6.  
  7. // create a keyboard input stream
  8. BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
  9.  
  10. // variables to store input
  11. // int Input;
  12. long Input;
  13. // Added boolean flag
  14. boolean isprime;
  15.  
  16. do {
  17. // prompt for user input
  18. System.out.print("Please enter a number: ");
  19. // Input = Integer.parseInt(stdin.readLine());
  20. Input = Long.parseLong(stdin.readLine());
  21.  
  22. isprime = true; // Set/reset flag
  23.  
  24. // test for prime number
  25. for (long i = 2; i < Input; i++) {
  26. if ((Input % i) == 0) {
  27. System.out.println(Input + " is not a prime number");
  28. System.out.println("It is divisable by: " + i);
  29. isprime = false;
  30. break; // Break out of loop
  31. }
  32. }
  33.  
  34. // See if we found a divisor
  35. if(isprime){
  36. System.out.println(Input + " is a prime number");
  37. }
  38. // if number is 0 then exit
  39. if (Input == 0) {
  40. System.out.println("Exiting program...");
  41. } else
  42. // if number is negative
  43. if (Input <= -1) {
  44. System.out.println("Please enter a positive integer.");
  45. }
  46.  
  47. } while (Input != 0);
  48.  
  49. }
  50. }
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum


Views: 1820 | Replies: 2
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC