ArrayList in Java and inputting

arraylist, arrays, java

Solution

Answers:

>1. I can't seem to get the input loop to work out, what's the best practice for doing so.

I would rather have a simple while loop instead of a do{}while... and place the condition in the while... In my example it read:

while the read number is not end signal and count is lower than limit: do.

>2. What is the object-type going to be for the reading method? A double[], or an ArrayList?

An ArrayList, however I would strongly recommend you to use List ( java.util.List ) interface instead. It is a good OO practice to program to the interface rather to the implementation.

>2.1How do I declare method-type to be an arraylist?

See code below.

>2.2. How do I prevent the array from having more than 1000 values stored within it?

By adding this restriction in the while condition.

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

public class InputTest{
    
    private int INPUT_LIMIT = 10000;

    public static void main( String [] args ) {
        InputTest test = new InputTest();
        System.out.println("Start typing numbers...");
        List list = test.readRange( 2.0 );
        System.out.println("The input was " +  list );
    }

    /**
     * Read from the standar input until endSignal number is typed.
     * Also limits the amount of entered numbers to 10000;
     * @return a list with the numbers.
     */
    public List readRange( double endSignal ) {
        List<Double> input = new ArrayList<Double>();
        Scanner kdb = new Scanner( System.in );
        int count = 0;
        double number = 0;
        while( ( number = kdb.nextDouble() ) != endSignal && count < INPUT_LIMIT ){
            System.out.println( number );
            input.add( number );
        }
        return input;
    }
}

Final remarks:

It is preferred to have "instance methods" than class methods. This way if needed the "readRange" could be handled by a subclass without having to change the signature, thus In the sample I've removed the "static" keyword an create an instance of "InputTest" class

In java code style the variable names should go in cammel case like in "endSignal" rather than "end_signal"

Problem

I'm used to python, so this is a bit confusing to me. I'm trying to take in input, line-by-line, until a user inputs a certain number. The numbers will be stored in an array to apply some statistical maths to them. Currently, I have a main class, the stats classes, and an "reading" class. Two Questions: I can't seem to get the input loop to work out, what's the best practice for doing so. What is the object-type going to be for the reading method? A double[], or an ArrayList? How do I declare method-type to be an arraylist? How do I prevent the array from having more than 1000 values stored within it? Let me show what I have so far: ``` public static java.util.ArrayList readRange(double end_signal){ //read in the range and stop at end_signal ArrayList input = new ArrayList(); Scanner kbd = new Scanner( System.in ); int count = 0; do{ input.add(kbd.nextDouble()); System.out.println(input); //debugging ++count; } while(input(--count) != end_signal); return input; } ``` Any help would be appreciated, pardon my newbieness...

Original source