Read integers separated with whitespace into int[] array

前端 未结 10 1889
陌清茗
陌清茗 2020-12-13 19:13

I read line with

BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
reader.readLine();

Example input is

相关标签:
10条回答
  • 2020-12-13 19:19

    Since you don't know the size of input, you can read input as string and then parse the string array to integer array

    String[] arr=reader.readLine().split(" ");
    int[] intarr=new int[arr.length];
    
    for(int i=0;i<arr.length;i++)
     intarr[i]=Integer.parseInt(arr[i]);
    
    0 讨论(0)
  • 2020-12-13 19:24

    Simple answer would be read integer inside the for loop using nextInt() method of scanner class.

    Here is the implementation :

    import java.util.Scanner;
    
    public class DriverMain {
        public static void main(String args[]){
            Scanner scanner = new Scanner(System.in);
            
            System.out.println("Enter the size of an array: ");
            int size = scanner.nextInt();
    
            System.out.println("Enter array elements separated by single space.");
            int[] numbers = new int[size];
            for(int i=0;i<size;i++){
                numbers[i] =  scanner.nextInt();
            }
    
            for(int i=0;i<size;i++){
                System.out.println(numbers[i]);
            }
    
        }
    }
    
    0 讨论(0)
  • 2020-12-13 19:28

    You could pre-compiled regex Pattern also to split String.

        Pattern pattern = Pattern.compile("(\\d+)\\s+"); //$NON-NLS-1$
        Matcher matcher = pattern.matcher(line);
        List<Integer> numbers = new LinkedList<Integer>();
    
        while (matcher.find()) {
            numbers.add(Integer.valueOf(matcher.group(1)));
        }
        Integer[] output = numbers.toArray(new Integer[numbers.size()]);
    

    Or you could also use pattern.split directly

        Pattern pattern = Pattern.compile("(\\d+)"); //$NON-NLS-1$
        String[] numberAsString = pattern.split(line);
        int[] numbers = new int[numberAsString.length];
        for (int i = 0; i < numberAsString.length; i++) {
            numbers[i] = Integer.valueOf(numberAsString[i]).intValue();
        }
    

    Gotta love regex :D

    0 讨论(0)
  • 2020-12-13 19:28
     Scanner sc = new Scanner(System.in);
        String n=sc.nextLine();
        String[] no=n.split(" ");
        int sum=0;
        for(String i:no){
            sum+=Integer.parseInt(i);}
        System.out.println(sum);
    

    The Above code is how can you take space separate integer inputs using java programming and perform any task of your choice in my case I have added the numbers.

    0 讨论(0)
  • 2020-12-13 19:30

    try

     line = reader.readLine();
     String[] s = line.split(" ");
     ...
    

    you can look to StringTokenizer also, but one of the fastest will be to read bytes and iterate them and convert the ascii (utf8?) coded numbers yourself ;)

    0 讨论(0)
  • Well, with Java 8 Streams this can actually be done in one line:

    int[] array = Arrays.stream(reader.readLine().split("\\s")).mapToInt(Integer::parseInt).toArray();
    

    where reader is your BufferedReader object.

    Explanation:

    • reader.readLine() reads the input as String
    • reader.readLine().split("\\s") splits that String on white-spaces returning a String[]
    • Arrays.stream(reader.readLine().split("\\s")) creates a Stream from that String[]
    • Arrays.stream(reader.readLine().split("\\s")).mapToInt(Integer::parseInt) applies Integer::parseInt to each element of that Stream and returns an IntStream consisting of the results of the applied function
    • Arrays.stream(reader.readLine().split("\\s")).mapToInt(Integer::parseInt).toArray() returns the int[] containing the elements of that IntStream
    0 讨论(0)
提交回复
热议问题