• October 20th, 2013, 08:21 AM
dulitul
Hello guys,

I have got a task to print all prime numbers in a certain range. Could you please tell me why the code does not work? The data validation works correct but the main task to print the prime numbers does not. Thank you.

Code :

```import java.io.*;   public class Prime {   BufferedReader input = new BufferedReader(new InputStreamReader(System.in));   public static int lowerBound; public static int higherBound;   public void getInput() throws IOException { String line1 = input.readLine(); String line2 = input.readLine(); int lowInput = Integer.parseInt(line1); int highInput = Integer.parseInt(line2); lowerBound = lowInput; higherBound= highInput;   }     public void validatedata() throws IOException {   do{ if (higherBound <= lowerBound) { System.out.println("The upper bound should be at least as big as the lower bound. " + "Please try again"); getInput(); } else System.out.println("the lower bount is: " + lowerBound); System.out.println("the high bound is: " + higherBound); } while (higherBound <= lowerBound); }   public void prime_calculation() throws IOException {   while (lowerBound > 2) { int k = 0; for (k = lowerBound; k <= higherBound; k++) { if ((higherBound % k) != 0) { System.out.println(k); getInput(); } } } }   }```

User class:

Code :

```import java.io.*;   public class PrimeUser extends Prime {     public static void main(String argv[]) throws IOException {   Prime isprime = new Prime(); isprime.getInput(); if (lowerBound < 2) System.out.println("You typed in number less than two. Please try again."); else if (higherBound <= lowerBound) isprime.validatedata(); else isprime.prime_calculation(); }       }```
• October 20th, 2013, 08:26 AM
Norm
Please copy the program's output and paste it here. Add some comments showing what it wrong and what the output should be.
• October 20th, 2013, 08:39 AM
dulitul
If I type in:

lower bound = 1
upper bound = 5

Output - "You typed in number less than two. Please try again." ---- This works fine

2)
lower bound = 6
upper bound = 3

Output - "The upper bound should be at least as big as the lower bound." - Works fine

3)
lower bound = 2
upper bound = 100

Output - nothing ----- doesn't work fine

The output should be - 2, 3, 5, 7 etc. In other words it should list all prime numbers
• October 20th, 2013, 08:47 AM
GregBrannon
Giving the user a blank screen and expecting him/her to know what to do with it is unfriendly and unreasonable. Improve your prompts so that the user knows what to do.

Adding prompts may also show you what the program is doing, allowing you to see where you're making logic errors.
• October 20th, 2013, 08:52 AM
dulitul
Will do. Thanks. However, this does not solve my problem as main code does not work.
• October 20th, 2013, 09:00 AM
Norm
Quote:

main code does not work.
Try debugging the code by adding some calls to the println() method to print out the values of variables as the code executes so you can see what the code is doing. Be sure to put ID Strings in the print()s so you know where the number was printed instead of just printing a number without an ID:
System.out.println("an ID for the variable "+ <the variable here>);
System.out.println("i="+i);

When you see what the code is doing, it will help you fix the problem.
• October 20th, 2013, 09:07 AM
dulitul
I am looking for someone to tell me the solution as I cannot fix it.
• October 20th, 2013, 09:12 AM
GregBrannon
At least you're honest. We're trying to give you basic debugging techniques that should help you find the source of the problem(s) and then fix them with or without our help. You can travel down that path with us, or you can move on. We're not simply going to fix it for you.
• October 20th, 2013, 10:25 AM
Norm
Break the project up into separate simple steps:
1) get user input
2)validate the user's input
3)calculate primes

If step 1) works, change the code to automate the testing of step 2 by changing the code:
Code :

```// String line1 = input.readLine(); // String line2 = input.readLine(); // int lowInput = Integer.parseInt(line1); // int highInput = Integer.parseInt(line2); lowerBound = 3; //lowInput; // Hard code values for easier testing higherBound= 20; //highInput;```
This will make the testing of steps 2 and 3 much faster & easier.

Add some println statements to the calculate method to print out the values of the variables as their values are changed.
• October 20th, 2013, 04:27 PM
dulitul
Ok guys,

I fixed it and found my error. Please see below. I used boolean true/false for the purpose. However, now I am struggling with another requirement. I want a message to be printed if there are no prime numbers in the range at all. I just want a single message " No primes found". Can you please help me?

This is my main method:

Code :

``` public void prime_calculation() throws IOException {   while ((lowerBound >= 2) && (higherBound > lowerBound)) { for (int k = lowerBound; k < higherBound; k++) { boolean primecheck = true;   for (int j=2; j < k; j++) { if (k % j == 0) { primecheck = false; break; } } if (primecheck) System.out.println(k); } validatedata(); } }```
• October 20th, 2013, 04:33 PM
GregBrannon
Code java:

```if ( noPrimesFound ) { System.out.println( "No primes found in the specified range." ); }```
• October 20th, 2013, 04:45 PM
dulitul
Yes I know that. Already tried it before. However, the issue comes from the fact that it is posted more than once. I want a single statement if there are no matches. For example in my code it would be:

Code :

```if (primecheck == false) System.out.println("No primes found");```

And for example when I input -

lower bound - 2
upper b - 10

It displays
No primes found
No primes found
No primes found