简体   繁体   中英

Reading an Array List (Java)

I am trying to take input from a user and then sort that input into an array. The input must either be an int, double, or string. All I have right now is a loop asking for the input of the user.

How do I read/identify the input and store it into its proper ArrayList ?

What I have so far:

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

public class userInput {

public static void main(String [] args){
     ArrayList<String> randomTextlist = new ArrayList<String>();
     ArrayList<Double> doublelist = new ArrayList<Double>();
     ArrayList<Integer> intlist = new ArrayList<Integer>();
     boolean input = true;

 Scanner scan = new Scanner(System.in);

 while (input){
      System.out.print("Input string, int, or double.");
      intlist.add(scan.next());
 }

Am I heading in the right direction? If so how do you suggest making sure that the input is put into their proper ArrayLists?

This will keep collecting until you type "q" and then print all the results in each ArrayList . You can modify it from there to suit your needs.

while (input) {
    System.out.println("Input string, int, or double:");
    if(scan.hasNextInt()) {
        intlist.add(scan.nextInt());
    } else if(scan.hasNextDouble()) {
        doublelist.add(scan.nextDouble());
    } else {
        String str = scan.next();
        if(str.equals("q")) 
            break;

        randomTextlist.add(str);
    }
}

// sort the lists
Collections.sort(intlist);
Collections.sort(doublelist);
Collections.sort(randomTextlist);

// print the lists
System.out.println(intlist);
System.out.println(doublelist);
System.out.println(randomTextlist);

Try this:

public static void main(String [] args){
    ArrayList<String> randomTextlist = new ArrayList<String>();
    ArrayList<Double> doublelist = new ArrayList<Double>();
    ArrayList<Integer> intlist = new ArrayList<Integer>();
    boolean input = true;

    Scanner scan = new Scanner(System.in);

    while (input){
        System.out.print("Input string, int, or double.");
        //exit loop case: input false, or how would you exit the loop?
        if (scan.hasNextBoolean()){
            break;
        }

        if (scan.hasNextDouble()){
            doublelist.add(scan.nextDouble());
        }
        else if (scan.hasNextInt()){
            intlist.add(scan.nextInt());
        }
        else{
            randomTextlist.add(scan.next());
        }
    }
    Collections.sort(doublelist);
    System.out.println(doublelist);
    Collections.sort(intlist);
    System.out.println(intlist);
    Collections.sort(randomTextlist);
    System.out.println(randomTextlist);
    }

a number like 245 is integer and also double so you must add it to intList and doubleList
but a number like 235.35 is only double so you must add it only to doubleList
other inputs (not fully numeric inputs ) are not int or double so you must add them to randomTextlist

now how we can detect an input is which one of them ?
it's simple using try/catch statements.how ?

first check if input is Integer or not ? if it does'nt be Integer , when you try to parse it, it throws NumberFormatException so you can detect that input is not Integer .

in catch statement you must check if input is Double or not, if it does'nt be ; it throws NumberFormatException as above
so input isn't Double , now we are sure that it belongs to randomTextlist

but if it was Integer you must add it to int and double list, and so on
now watch this code ,so that you understand what i'm trying to tell you !

   while (input) {
        String next = scan.next();
        try {
            /*
             check if input is integer or not,
             if it not ,  it throws NumberFormatException
             */
            int n = Integer.parseInt(next);

            // if input was integer it does'nt throw any exception 
            // so add input to your lists
            intlist.add(n);
            doublelist.add(new Double(n));

        } catch (NumberFormatException e) {
            try {
                 /*
                 check if input is double or not,
                 if it not , it throws NumberFormatException
                 */
                double n = Double.parseDouble(next);

                // like above there is no exception so input is double 
                // and we add it to doublelist
                doublelist.add(n);
            } catch (NumberFormatException ex) {
                // after two NumberFormatExceptions we are sure that  
                // input isn't integer or double so add it to randomTextlist
                randomTextlist.add(next);
            }
        }
    }

First, you need to figure out what type of data you have.

Try to parse as an Integer first, then as a Double and default to a String if all else fails. Untested example:

String scannedValue = scan.next();
try {
    intlist.add(Integer.parseInt(scannedValue));
} catch (NumberFormatException e1) {
    try {
        doublelist.add(Double.parseDouble(scannedValue));
    } catch (NumberFormatException e2) {
        randomTextlist.add(scannedValue);
    }
}

Now for the sorted list stuff. Currently, you are just adding the value to the list.

Since Integer , Double and String all implement Comparable , you can use the sort method on Collections to sort your lists:

Collections.sort(randomTextlist);
Collections.sort(doublelist);
Collections.sort(intlist);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM