ArrayList in Java and input

I'm used to python, so this is a bit confusing to me. I try to enter data in turn until the user enters a specific number. The numbers will be stored in an array to apply statistical mathematical data to them. Currently, I have a main class, statistics classes, and a reading class.

Two questions:

  • I can't get the input loop to work, which is best for this.

  • What is the type of object for the read method? Double [] or ArrayList?

    • How to declare method type as arraylist?

    • How to prevent an array from having more than 1000 values ​​stored in it?

Let me show you what I still have:

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, sorry for my newbie ...

+6
5

:

< β†’ 1. , , .

while do {} while... ... :

, : do.

< β†’ 2. ? [] ArrayList?

ArrayList, List (java.util.List). OO , .

> 2.1 arraylist?

.

< β†’ 2,2. , 1000 , ?

while.

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;
    }
}

:

" ", . , "readRange" , "static", "InputTest"

java- , , "endSignal", "end_signal"

+2

:

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

, .

ArrayList :

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

. .

+5

**

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(Double.valueOf(kbd.next()));
        System.out.println(input); //debugging
        ++count;
    } while (input(--count) != end_signal);
    return input;
}

**

0

, , . :

;

import java.util.; import java.util.regex.;

ArrayListInput {

public ArrayListInput() {
    // as list
    List<Double> readRange = readRange(1.5);

    System.out.println(readRange);
    // converted to an array
    Double[] asArray = readRange.toArray(new Double[] {});
    System.out.println(Arrays.toString(asArray));
}

public static List<Double> readRange(double endWith) {
    String endSignal = String.valueOf(endWith);
    List<Double> result = new ArrayList<Double>();
    Scanner input = new Scanner(System.in);
    String next;
    while (!(next = input.next().trim()).equals(endSignal)) {
        if (isDouble(next)) {
            Double doubleValue = Double.valueOf(next);
            result.add(doubleValue);
            System.out.println("> Input valid: " + doubleValue);
        } else {
            System.err.println("> Input invalid! Try again");
        }
    }
    // result.add(endWith); // uncomment, if last input should be in the result
    return result;
}

public static boolean isDouble(String in) {
    return Pattern.matches(fpRegex, in);
}

public static void main(String[] args) {
    new ArrayListInput();
}

private static final String Digits = "(\\p{Digit}+)";
private static final String HexDigits = "(\\p{XDigit}+)";
// an exponent is 'e' or 'E' followed by an optionally
// signed decimal integer.
private static final String Exp = "[eE][+-]?" + Digits;
private static final String fpRegex = ("[\\x00-\\x20]*" + // Optional leading "whitespace"
        "[+-]?(" + // Optional sign character
        "NaN|" + // "NaN" string
        "Infinity|" + // "Infinity" string

        // A decimal floating-point string representing a finite positive
        // number without a leading sign has at most five basic pieces:
        // Digits . Digits ExponentPart FloatTypeSuffix
        // 
        // Since this method allows integer-only strings as input
        // in addition to strings of floating-point literals, the
        // two sub-patterns below are simplifications of the grammar
        // productions from the Java Language Specification, 2nd
        // edition, section 3.10.2.

        // Digits ._opt Digits_opt ExponentPart_opt FloatTypeSuffix_opt
        "(((" + Digits + "(\\.)?(" + Digits + "?)(" + Exp + ")?)|" +

        // . Digits ExponentPart_opt FloatTypeSuffix_opt
        "(\\.(" + Digits + ")(" + Exp + ")?)|" +

        // Hexadecimal strings
        "((" +
        // 0[xX] HexDigits ._opt BinaryExponent FloatTypeSuffix_opt
        "(0[xX]" + HexDigits + "(\\.)?)|" +

        // 0[xX] HexDigits_opt . HexDigits BinaryExponent
        // FloatTypeSuffix_opt
        "(0[xX]" + HexDigits + "?(\\.)" + HexDigits + ")" +

        ")[pP][+-]?" + Digits + "))" + "[fFdD]?))" + "[\\x00-\\x20]*");// Optional
                                                                        // trailing
                                                                        // "whitespace"

}

  • Java . , . double , List , casting/type:

    if (!readRange.isEmpty()) {
        double last = readRange.get(readRange.size() - 1);
    }
    
  • Java, (LinkedList, SynchronizedLists,...). , , , - .

  • , while while, , , next = input.next(). trim(). , . playe, whitespacing

  • nextDouble() , , -, , . String, , , .

  • , double, JavaDoc Double.valueOf(). , , .

  • , . , , readRange.size().

  • , , .

  • , , , Java 1.5 Auto-Boxing . Scanner.next() null (afaik), .

  • ,

Ok, hope you find my solution and explanations useful, use result.size () as an indicator and the break keyword to leave the while statement.

Greetz, GHad

0
source
public static ArrayList&lt;Double> readRange(double end_signal) {

    ArrayList<Double> input = new ArrayList<Double>();
    Scanner kbd = new Scanner( System.in );
    int count = 0;
    do{
        input.add(kbd.nextDouble());
        ++count;
    } while(input(--count) != end_signal);
return input;
}
0
source

All Articles