I am having troubles with java. I need to scan numbers (example: 1 3 4 2 5 6 7) and put them into array. The problem is I don't know how long it will be. Is there a command that I can use to determine the length of the numbers putted in scanner?
- 
        Most scanning devices I've worked with act as a keyboard, and just "type" whatever you scan. So you can just grab all the input and then parse it.Bobson– Bobson2012-11-01 14:07:46 +00:00Commented Nov 1, 2012 at 14:07
4 Answers
If you don't want to use ArrayList, as a workaround, I advice to read the entire line and then split to get the length and individual values:
   String line = scanner.nextLine();
   String[] values = line.split(" ");
   int[] intValues = new int[values.length];
   int indx = 0;
   for(String value:values){
     intValues[indx++] = Integer.parseInt(value);
   }
EDIT: Second approach:
   List<Integer> numList = new ArrayList<Integer>();
   int number = 0;
   while (number != -1) {
      System.out.println("Enter a positive integer value (or -1 to stop): ");
      number = Integer.parseInt(in.nextLine());
      if (number != -1){
        numList.add(number);
      }
   }
   in.close();
   Integer[] numArray = numList.toArray(new Integer[numList.size()]);
EDIT2: Taking care of multiple numbers in the same line and terminating at empty line
List<Integer> numList = new ArrayList<Integer>();
while(true) {
   System.out.println("Enter a positive integer value (or -1 to stop): ");
   String line = in.nextLine();
   if(line.length() <1){
      break;
   }
   String [] numbers = line.split(" ");
   for(String num: numbers){
          numList.add(Integer.parseInt(num));
   }
 }
 in.close();
 Integer[] numArray = numList.toArray(new Integer[numList.size()]);
10 Comments
nextLine(). This will stop as soon as you enter -1.-1 is just one example. You may use any condition you like. Please share, what condition you want to use?EDTI2 section in the answer. Please check. Now it will terminate on empty line only.Use an ArrayList if you want a variable-size collection.
ArrayList<Integer> lst = new ArrayList<Integer>();
lst.add(scanner.getInt());
Comments
You can use an ArrayList. Just fill it up and at the end, convert it to an integer array. Or even better, keep the list and work with that.
Scanner in = new Scanner(System.in);
ArrayList<Integer> intList = new ArrayList<Integer>();
while (true) {
  System.out.println("Enter a positive integer value (or -1 to stop): ");
  int number = in.nextInt();
  if (number < 0) break;
  intList.add(number);
}
// just use this last line if you really want an array. Work with the list instead if possible.
Integer[] yourFinalArray = intList.toArray(new Integer[0]);
Comments
If you use the google Guava library ( https://code.google.com/p/guava-libraries/ ) you can say:
String numbers="1 3 4 2 5 6 7";
Splitter.on(" ").split( numbers );
this gives you an iterable. If you insist on getting an array of Strings, you can say:
Iterables.toArray(Splitter.on(" ").split( numbers ), String.class)
