Program konsolowy - kłopot z warunkiem (StreamTokenizer)

0

Mam kłopot, nie potrafię zbudować poprawnej pętli, którą dałoby się zakończyć z poziomu konsoli komendą "exit". Program w założeniu, ma pytać o zakres do wyświetlenia ciągu liczb. To działa, liczby generują się poprawnie ale program po jednym wykonaniu kończy swoje działanie.

Klasa PrimeNumberConsole

private int number;
private Reader reader = new BufferedReader(new InputStreamReader(System.in));
private StreamTokenizer streamToken = new StreamTokenizer(reader);
...
public void primeToConsole() {
		System.out.println("Określ zakres liczb pierwszych");
		try {
			a = streamToken.nextToken();
			System.out.println(a);
			
			if (a == StreamTokenizer.TT_NUMBER) {
					number = (int) streamToken.nval;
					PrimeNumber pn = new PrimeNumber(number);
					pn.PrintSequence();
				}
			} else {
				throw new IOException();
			}
		} catch(IOException e) {
			System.out.println("Błąd odczytu, niepoprawne dane wejściowe.");
		}

Klasa PrimeNumber

protected int a;
private int b = 0;
...
public int PrintSequence() {
		if(a > 1) System.out.print("Liczby pierwsze mniejsze od " + a + " - [");
		if (a > 7 && a % 1 == 0) {
			System.out.print(" 1 2 3 5 7 ");
				while (b++ < a) {
					if (b != 1 && b % 2 != 0 && b % 3 != 0 && b % 5 != 0 && b % 7 != 0) {
						System.out.print(b + " ");
				}
			}
			System.out.println("]");
		} 
		if (a <= 7) {
			switch (a) {
				case 7: case 6:
					System.out.print(" 1 2 3 5 ");
					break;
				case 5: case 4:
					System.out.print(" 1 2 3 ");
					break;
				case 3:
					System.out.print(" 1 2 ");
					break;
				case 2:
					System.out.print(" 1 ");
					break;
				default:
					throw new ArithmeticException("Nieporawna wartość. Zakres wartości określa liczba naturalna większa od 1.");
			}
			System.out.println("]");
		}
		return 0;
	}
1
import java.util.Scanner;

public class Primes {

	private boolean cancelled;
	protected int a;
	private int b = 0;

	public static void main(String[] args) {
		Primes ps = new Primes();
		try (Scanner in = new Scanner(System.in)) {
			while (!ps.cancelled) {
				String input = in.next();
				if (input.equals("exit")) {
					ps.cancelled = true;
					continue;
				}
				try {
					ps.a = Integer.parseInt(input);
					ps.b = 0;
					ps.PrintSequence();
				} catch (NumberFormatException e) {
					System.out.println("podaj tylko wartosci liczbowe");
				}
			}
		}
	}

	public int PrintSequence() {
		if (a > 1) {
			System.out.print("Liczby pierwsze mniejsze od " + a + " - [");
		}
		if (a > 7 && a % 1 == 0) {
			System.out.print(" 1 2 3 5 7 ");
			while (b++ < a) {
				if (b != 1 && b % 2 != 0 && b % 3 != 0 && b % 5 != 0 && b % 7 != 0) {
					System.out.print(b + " ");
				}
			}
			System.out.println("]");
		}
		if (a <= 7) {
			switch (a) {
			case 7:
			case 6:
				System.out.print(" 1 2 3 5 ");
				break;
			case 5:
			case 4:
				System.out.print(" 1 2 3 ");
				break;
			case 3:
				System.out.print(" 1 2 ");
				break;
			case 2:
				System.out.print(" 1 ");
				break;
			default:
				throw new ArithmeticException(
						"Nieporawna wartość. Zakres wartości określa liczba naturalna większa od 1.");
			}
			System.out.println("]");
		}
		return 0;
	}
}

w telegraficznym skrócie - w pętli musisz pobierać input od użytkownika, wykonać twoją metodę i wykonać kolejną iterację tak długo aż twój warunek zostanie spełniony - w tym wypadku, gdy ktoś napisze 'exit'. Wtedy w jakiś sposób musisz przerwać działanie pętli i pozwolić wykonać się reszcie programu.

1 użytkowników online, w tym zalogowanych: 0, gości: 1