Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ArrayList in Java and inputting

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:

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

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

    1. How do I declare method-type to be an arraylist?

    2. 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...

like image 740
Octaflop Avatar asked Sep 22 '08 17:09

Octaflop


People also ask

How do you add an element to an ArrayList using user input?

Element can be added in Java ArrayList using add() method of java. util. ArrayList class.

How do you take multiple inputs from an ArrayList?

ArrayList<Integer> numbers = new ArrayList<Integer>(); //insert into array if > 0 int x = sc. nextInt(); if(x > 0){ numbers. add(x); } //square numbers array for (int i = 0; i < numbers.

What is ArrayList in Java with example?

In this tutorial, we will learn about the ArrayList class in Java. We will learn about different operations and methods of the arraylist with the help of examples. In Java, we use the ArrayList class to implement the functionality of resizable-arrays. It implements the List interface of the collections framework.


2 Answers

What you need in your loop condition is:

while ( input.get( input.size()-1 ) != end_signal );

What you're doing is decrementing the counter variable.

Also you should declare the ArrayList like so:

ArrayList<Double> list = new ArrayList<Double>();

This makes the list type-specific and allows the condition as given. Otherwise there's extra casting.

like image 192
Jason Cohen Avatar answered Sep 27 '22 04:09

Jason Cohen


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"

like image 40
OscarRyz Avatar answered Sep 28 '22 04:09

OscarRyz