944,079 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 2032
  • Java RSS
Sep 28th, 2004
0

one last tiny little detail

Expand Post »
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);

}
}
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Jason Marceau is offline Offline
10 posts
since Sep 2004
Sep 28th, 2004
0

Re: one last tiny little detail

First clue to you is to use 'break' and not 'return'
Reputation Points: 11
Solved Threads: 1
Junior Poster in Training
jerbo is offline Offline
84 posts
since Sep 2004
Sep 28th, 2004
0

Re: one last tiny little detail

Here is the completed code I worked out. Note - I expanded to a long to enable you to enter large numbers.

Java Syntax (Toggle Plain Text)
  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. }
Reputation Points: 11
Solved Threads: 1
Junior Poster in Training
jerbo is offline Offline
84 posts
since Sep 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: To all the member
Next Thread in Java Forum Timeline: Primality Test





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC