Converting String Array to an Integer Array

后端 未结 6 1186
我寻月下人不归
我寻月下人不归 2020-11-29 05:24

so basically user enters a sequence from an scanner input. 12, 3, 4, etc.
It can be of any length long and it has to be integers.
I want to convert the

6条回答
  •  情歌与酒
    2020-11-29 05:59

    import java.util.ArrayList;
    import java.util.List;
    import java.util.Scanner;
    
    class MultiArg {
    
        Scanner sc;
        int n;
        String as;
        List numList = new ArrayList();
    
        public void fun() {
            sc = new Scanner(System.in);
            System.out.println("enter value");
            while (sc.hasNextInt())
                as = sc.nextLine();
        }
    
        public void diplay() {
            System.out.println("x");
            Integer[] num = numList.toArray(new Integer[numList.size()]);
            System.out.println("show value " + as);
            for (Integer m : num) {
                System.out.println("\t" + m);
            }
        }
    }
    

    but to terminate the while loop you have to put any charecter at the end of input.

    ex. input:

    12 34 56 78 45 67 .
    

    output:

    12 34 56 78 45 67
    

提交回复
热议问题