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.
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:
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(); } }