3 Replies Latest reply: Jul 5, 2008 6:58 PM by 3004 RSS

    Console — multiple integers input

    843785
      I have a problem with input, in partucular with multiple integers input. For example I want to read 4 integers from the console, i.e.:

      1 2 3 1

      Ideally, I want to get 4 variables from this input (variables to be automatically created). As I am quite new to Java, I couldn't find the solution for creating new variables, so I decided to get all these integers into an int[] array, i.e. the result should be int[] input {1, 2, 3, 1}.
      I found two ways of doing it, although they are not as neat as they might be (I thinnk there is a way of simplifying them).

      The first method uses two Scanners — quite a bulky approach, in my opinion:
      import java.io.*;
      import java.util.*;
      
      public class InputTest1 {
          
        public static void main (String[] args) {
      
              int[] inputArray = new int[4];
              String result;
              Scanner scanner = null;
              Scanner reader = null;
      
              try {
               scanner = new Scanner(new InputStreamReader(System.in));
               result = scanner.nextLine();
               reader = new Scanner(result);
      
               for (int i=0; x<inputArray.length; i++) {
                 inputArray[i] = reader.nextInt();
               }
      
               /*
               Actions
               */
      
               } finally {
                 scanner.close();
                 reader.close();
               }
        }
      }
      The second method uses two arrays, which also works:
      import java.io.*;
      import java.util.*;
      
      public class Ladia {
          
         public static void main (String[] args) {
      
          int[] inputArray = new int[4];
          String[] result;
          BufferedReader in = null;
          Scanner scanner = null; 
      
          try {
               scanner = new Scanner(new InputStreamReader(System.in));
               result = scanner.nextLine().split("\\s");
               for (int x=0; x<result.length; x++){
                   inputArray[x] = Integer.parseInt(result[x]);
               }
      
               /*
               Actions
               */
      
               } finally {
               scanner.close();
      
           }
        }
      }
      So my question is &#8212; what is the best way to get multiple variables from the console?
        • 1. Re: Console — multiple integers input
          3004
          Sp3ct3r wrote:
          Ideally, I want to get 4 variables from this input (variables to be automatically created). As I am quite new to Java, I couldn't find the solution for creating new variables,
          You can't create variables at runtime.
          so I decided to get all these integers into an int[] array, i.e. the result should be int[] input {1, 2, 3, 1}.
          Yup, that's the way to do it, if you know how many there'll be. Otherwise, you can use a List.
          I found two ways of doing it, although they are not as neat as they might be (I thinnk there is a way of simplifying them).
          Both ways look fine to me, though I'd probably prefer the first one. Also, I'd close the scanner immediately after reading, rather than after processing.
          • 2. Re: Console ? multiple integers input
            843785
            Also, I'd close the scanner immediately after reading, rather than after processing.
            Personally, I never close System.in.
            • 3. Re: Console — multiple integers input
              3004
              Navy_Coder wrote:
              Also, I'd close the scanner immediately after reading, rather than after processing.
              Personally, I never close System.in.
              Me neither. But if I were going to, I'd do it after reading and before processing.