0

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();
    }


}
3
  • show your IntegerArray class, please. and what do you have as InputStream? Commented Nov 8, 2018 at 20:53
  • @spirit Edited it in have a look. Commented Nov 8, 2018 at 20:55
  • create a list of Integer and add nextline from scanner to the list in the while loop. Outside the loop, you then add that list to your IntegerArray class or convert the list to an array and set in the IntegerArray class Commented Nov 8, 2018 at 20:56

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Can you please explain this line IntegerArray(intList.stream().mapToInt(Integer::intValue).toArray());
@Stefan: I've added explanation of this line in my answer. Please go through it and let me know if you have any queries further.
0

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);
       } 
    }
}

2 Comments

i suggest at least using ArrayList instead of LinkedList and adding scanner.close() after the while loop in case you want to put this code in a production env
Why ArrayList instead of LinkedList?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.