Garrett
Garrett

Reputation: 21

creating array that stops when the user enters "0"

==============

I have a bit of a issue connecting the dots on a assignment I was given. This is in Java.

The Goal / Requirements:

This is what I have so far, and it doesn't work because it isnt finished yet, and im dumb.

import java.util.Scanner;

public class IntegerArrayTester
{

    public static void main(String[] args)
    {
        Scanner console = new Scanner(System.in);

        int total = 0;
        int[] array = new int[total];

        // Asks for user input
        System.out.println("Please enter integers. To stop, type a negative number.");
        int input = console.nextInt();
        int num = input;

        while(num >= 0) {
            for(int i = 0; i < total; i++)
                array[i] = console.nextInt();
        }

        // displays the array
        for(int k = 0; k < array.length; k++) {
            System.out.println(array[k] + " ");
        }

        console.close();
    }
}

Upvotes: 2

Views: 3578

Answers (4)

user5500105
user5500105

Reputation: 297

Arrays are not resizable in Java, you need to use List.

import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.List;
import java.util.Scanner;

public class IntegerArrayTester {

    public static void main(String[] args) {
        try (Scanner console = new Scanner(System.in)) {

            int nEven = 0;
            int nOdd = 0;

            List<Integer> cummulativeTotal = new ArrayList<Integer>();

            // Asks for user input
            System.out.println("Please enter integers. To stop, type a negative number.");

            int num;
            int total = 0;

            while (true) {
                try {
                    num = console.nextInt();
                } catch (InputMismatchException ex) {
                    System.err.println("The entered value is not an integer");
                    return;
                }
                if (num < 0) {
                    break;
                }
                total += num;
                cummulativeTotal.add(total);
                if (num % 2 == 0) {
                    nEven++;
                } else {
                    nOdd++;
                }
            }

            System.out.println("Number of even inputs: " + nEven);
            System.out.println("Number of odd  inputs: " + nOdd);
            System.out.println("Cummulative totals: " + cummulativeTotal.toString());
        }
    }
}

Upvotes: 2

AbtPst
AbtPst

Reputation: 8008

try this

    ArrayList<Integer> ins = new ArrayList<Integer>();
    Scanner scan = new Scanner(System.in);

    System.out.println("Please enter numbers. Enter negative value to stop.");

    int evens = 0, odds = 0;

    while (scan.hasNextLine()) {
        int num = Integer.parseInt(scan.next());

        if(num<0) break;

        ins.add(num);

        if (num % 2 == 0)
            evens++;

        else
            odds++;
    }

    System.out.println("Even numbers = "+evens+"\nOdd numbers = "+odds);
    int[] totals = new int[ins.size()];
    totals[0] = ins.get(0);
    System.out.print("Cumulative totals \n" + totals[0] + " ");
    for (int i = 1; i < ins.size(); i++) {
        totals[i] = totals[i - 1] + ins.get(i);
        System.out.print(totals[i] + " ");
    }

Upvotes: 0

tom
tom

Reputation: 1503

First, don't use an array because you don't know the amount of numbers the user will submit.

Here you initialize the array with a fixed size of 0

int total = 0;
int[] array = new int[total];

so i guess your program crashes here when the first number ist supplied?

array[i] = console.nextInt();

Use a dynamically growing Collection like ArrayList or the like ...

Also, num should be updated...

Upvotes: 0

Ryan
Ryan

Reputation: 2084

You never update num. Add it to the loop that gets the user input.

    while(num >= 0) {
        for(int i = 0; i < total; i++) {
            array[i] = num;
            num = console.nextInt();
        }
    }

You also need to set total to something greater than 0 if you want the array to fill up.

Upvotes: 2

Related Questions