0

Can anyone please help me with the code as how to read multiple lines from console and store it in array list? Example, my input from the console is:

12     abc      place1
13     xyz      place2

and I need this data in ArrayList.

So far I tried this code:

Scanner scanner = new Scanner(System.in);
ArrayList informationList = new ArrayList<ArrayList>();
String information = "";
int blockSize = 0, count = 1;
System.out.println("Enter block size");
blockSize = scanner.nextInt();
System.out.println("Enter the Information ");
while (scanner.hasNext() && blockSize >= count) {
    scanner.useDelimiter("\t");
    information = scanner.nextLine();
    informationList.add(information);
    count++;
}

Any help is greatly appreciated.

Input line from console is mix of string and integer

user1547054
  • 7
  • 1
  • 1
  • 3
  • Scanner scanner = new Scanner(System.in); ArrayList informationList = new ArrayList(); String information=""; nt blockSize = 0, count=1; System.out.println("Enter block size"); blockSize = scanner.nextInt(); System.out.println("Enter the Information "); while(scanner.hasNext() && blockSize >= count){ scanner.useDelimiter("\t"); information = scanner.nextLine(); informationList.add(information); count++; } – user1547054 Jul 23 '12 at 22:20
  • What type should the elements of the ArrayList be? Are you just storing the raw strings or parsing each line into a strongly typed data structure? If the latter, please provide the code for the data object you're using. – Mike Deck Jul 23 '12 at 22:21
  • Input line from console is mix of string and integer – user1547054 Jul 23 '12 at 22:25
  • Are you aware that by executing this code you are creating an arraylist of arraylists? Is that really what you want? you probably would want to set your type to String since you will have a variety of inputs and that is the most simple. ArrayList informationList = new ArrayList(); – Joel Jul 23 '12 at 22:25
  • @user1547054 Instead of providing code in comments try editing question. This way it is easier to read and access. – Pshemo Jul 23 '12 at 22:25
  • @Pshemo: I hv done it already. Sorry I m new to tthis forum – user1547054 Jul 23 '12 at 22:28
  • @user1547054 Good for you. Also try not to see SO as forum. We do not try to discuss about solutions, but just provide them :) – Pshemo Jul 23 '12 at 22:32
  • @Pshemo: Agreed and Thank you. So, now can I have some inputs about the real problem please? – user1547054 Jul 23 '12 at 22:33
  • @user1547054 I still don't understand exactly what the expected behavior is, should you have a list of strings e.g. ["12 abc place1", "13 xyz place2"] or should it be a list of Object with Strings and Integers e.g. [12, "abc", "place1", 13, "xyz", "place2"], or should it be a list of strongly typed objects? Or perhaps a list of lists which is what your current code somewhat implies? – Mike Deck Jul 23 '12 at 22:37
  • @Mike Deck: Thanks for the questions. I need a list of object with Strings and Integers – user1547054 Jul 23 '12 at 22:41

1 Answers1

2

You've got a few problems.

First of all, the initialization line for your ArrayList is wrong. If you want a list of Object so you can hold both Integers and Strings, you need to put Object inside the angle braces. Also, you're best off adding the generic type argument to the variable definition instead of just on the object instantiation.

Next, your count is getting messed up because you're initializing it to 1 instead of 0. I'm assuming "block size" really means the number of rows here. If that's wrong leave a comment.

Next, you don't want to reset the delimiter your Scanner is using, and you certainly don't want to do it inside your loop. By default a Scanner will break up tokens based on any whitespace which I think is what you want since your data is delimited both by tabs and newlines.

Also, you don't need to check hasNext() in your while condition. All of the next*() methods will block waiting for input so the call to hasNext() is unnecessary.

Finally, you're not really leveraging the Scanner to do what it does best which is parse tokens into whatever type you want. I'm assuming here that every data line is going to start with a single integer and the be followed by two strings. If that's the case, just make a call to nextInt() followed by two calls to next() inside your loop and you'll get all the data parsed out into the data types you need automatically.

To summarize, here is your code updated with all my suggestions as well as some other bits to get it to run:

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

public class Example {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        ArrayList<Object> list = new ArrayList<>();
        System.out.println("Enter block size");
        int blockSize = scanner.nextInt();
        System.out.println("Enter data rows:");
        int count = 0;
        while (count < blockSize) {
            list.add(scanner.nextInt());
            list.add(scanner.next());
            list.add(scanner.next());
            count++;
        }
        System.out.println("\nThe data you entered is:");
        System.out.println(list);
    }
}
Mike Deck
  • 18,045
  • 16
  • 68
  • 92