简体   繁体   中英

Converting object method with InputStream as input to array to print

I have a method to read an IntegerArray which is a class and the input is InputStream. I tried to use scanner but I'm stuck on filling out the array and creating the return statement to output it the correct way. I really want to know how the conversion should work It's my first time facing this kind of problem since I'm really new to Java so please go easy on me. What i got so far:

public class ArrayReader {

    public static IntegerArray readIntArray(InputStream input) {
        Scanner scanner = new Scanner(input);
        scanner.nextInt();
        int size = 0;
        int[] iarr;
        while(scanner.hasNextInt()) {
            size++;
            scanner.nextLine();
        }
    }
}


import java.util.Arrays;

public final class IntegerArray {
    private int[] a;

    public IntegerArray(int[] a) {
        this.a = a;
    }

    public int length() {
        return a.length;
    }

    public int getElementAt(int i) {
        return a[i];
    }

    public int sum() {
        int sum = 0;
        for(int i: a) {
            sum += i;
        }
        return sum;
    }

    public double average() {
        int i, sum = 0, armean;
        for(i = 0; i < a.length; i++) {
            sum = sum + a[i];
        }
        armean = sum / i;
        return armean;
    }

    public IntegerArray getSorted() {
        int[] b = a.clone();
        Arrays.sort(b);
        return new IntegerArray(b);
    }

    public IntegerArray contact(IntegerArray ia) {
        int[] newA = new int[a.length + ia.length()];
        for(int i = 0; i < ia.a.length; i++) {
            newA[i] = a[i];
        }

        for(int j = 0; j < ia.a.length; j++) {
            newA[j + a.length] = ia.a[j];
        }

        return new IntegerArray(newA);
    }

    @Override
    public String toString() {
        return a.toString();
    }


}

Using Java 8 you can write your method like this,

public class ArrayReader {

    public static IntegerArray readIntArray(InputStream input) {
        Scanner scanner = new Scanner(input);
        List<Integer> intList = new ArrayList<Integer>();
        while (scanner.hasNextInt()) {
            intList.add(scanner.nextInt());
        }
        scanner.close();

        return new IntegerArray(intList.stream().mapToInt(Integer::intValue).toArray());
    }

    public static void main(String args[]) throws Exception {
        FileInputStream fin = new FileInputStream("num.txt");
        IntegerArray integerArray = readIntArray(fin);
        System.out.println(integerArray.sum());
        System.out.println(integerArray.average());
        fin.close();
    }
}

Create a text file with some filename like num.txt in your current directory, put some numbers in it, run the code and enjoy the output.

Edit:

Explanation of this line.

IntegerArray(intList.stream().mapToInt(Integer::intValue).toArray());

Your IntegerArray class declares a constructor which accepts native int array like this,

public IntegerArray(int[] a) {
    this.a = a;
}

But the read numbers were stored in List<Integer> object hence that list needed to be converted into int[] which is where, this

intList.stream().mapToInt(Integer::intValue).toArray()

is needed.

It basically creates a stream from intList , then mapToInt(Integer::intValue) method converts the stream of Integer objects to stream of native int numbers which finally gets collected as int[] using toArray() method, which is what we aimed to get as per your constructor needs.

Had you declared your constructor using wrapper Integer class like this,

public IntegerArray(Integer[] a) {
    this.a = a;
}

Then we could have achieved it by just writing this,

new IntegerArray(intList.toArray(new Integer[intList.size()]))

Hope it helps.

not the best, but it works

public class ArrayReader {

    public static IntegerArray readIntArray(InputStream input) {
        try(Scanner scanner = new Scanner(input)){
            List<Number> inputList = new LinkedList<>();
            while(scanner.hasNextInt()) {
                inputList.add(scanner.nextInt());
            }
            int[] iarr = new int[inputList.size()];
            for (int i = 0; i < inputList.size(); i++) {
                iarr[i] = inputList.get(i).intValue();   
            }
            return new IntegerArray(iarr);
       } 
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM