This program lets the user input a number and checks if the number is prime. However, if want the program to exit when the user inputs the "q". I have tried several things (do while, if) but none of the methods seem to work. How is this caused and how can I solve it?
Below is the source code:
// Test for primes 2
import java.io.*;
class FindPrime2 {
public static void main(String args[])
throws IOException {
// int num;
boolean isPrime;
String str;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("\nFINDING PRIME NUMBERS v0.1");
System.out.print("\nPlease enter a number: ");
str = br.readLine();
if(str == "q") System.exit(1);
int num = Integer.valueOf(str);
System.out.println("You have picked: " + num);
if(num < 2) isPrime = false;
else isPrime = true;
for(int i=2; i <= num/i; i++) {
if((num % i) == 0) {
isPrime = false;
break;
}
}
if(isPrime) System.out.println(num +" is Prime.");
else System.out.println(num + " is not Prime.");
}
}