0

I use a buffered reader to read in the input and then it adds it to an array. But for some reason it only adds the last input to the array. I also want to check if the first input is zero... so thats what I am doing with the check variable. But the main problem is that it doesn't add it to the array.

public static void main (String[] Args) throws IOException
{
    int[] numbers = new int[100];

    Scanner scan = new Scanner(System.in);
    InputStreamReader isReader = new InputStreamReader(System.in);
    BufferedReader bReader;
    bReader = new BufferedReader(isReader);
    int intNumber = Integer.parseInt(bReader.readLine());
    int check = scan.nextInt();
    while (check != 0)
    {
        int i = 0;
        numbers[i] = Integer.parseInt(bReader.readLine());
        check = intNumber;
        i++;
    }
    bReader.close();
}

1 Answer 1

3

move int i = 0 outside of while loop. In each iteration i is getting initialized to 0 so your array is having only one value and that is in 0th index

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.