5

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?

1
  • 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. Commented Nov 1, 2012 at 14:07

4 Answers 4

3

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()]);
Sign up to request clarification or add additional context in comments.

10 Comments

@user1791473: You have couple of options. First: Define as big enough array if you know the max input size. Accept all the inputs and truncate the array in the end, if partially filled. Second: Use the open ended collection e.g. List and convert to array in the end (suggested in other answers). If you plan to use option one and need help, please let me know.
@user1791473: Added the answer for second approach.
@user1791473: Updated to use the nextLine(). This will stop as soon as you enter -1.
@user1791473: You need to define some termination condition. -1 is just one example. You may use any condition you like. Please share, what condition you want to use?
@user1791473 Yes. Updated the EDTI2 section in the answer. Please check. Now it will terminate on empty line only.
|
1

Use an ArrayList if you want a variable-size collection.

ArrayList<Integer> lst = new ArrayList<Integer>();
lst.add(scanner.getInt());

Comments

1

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

0

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)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.