[Solved] How to put positive numbers to Scanner and then stop it by entering negative one?


Hope this helps. Code can be improved with exceptions checks. This one is just to get started with.

package com.samples;

import java.util.Scanner;

public class ScannerSample {

    public static void main(String [] args) {
        Scanner scanner = new Scanner(System.in);
        int [] mas = new int[50];

        int inputInt = scanner.nextInt();
        mas[0] = inputInt;

        int count = 1;
        while(inputInt > 0) {
            inputInt = scanner.nextInt();
            if(inputInt > 0)
                mas[count++] = inputInt;
        }
        System.out.println("Exiting now.");
        scanner.close();

        for (int i = 0; i < count; i++) {
            System.out.println(mas[i]);
        }
    }
}

0

solved How to put positive numbers to Scanner and then stop it by entering negative one?