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();
}
}
IntegerArrayclass, please. and what do you have asInputStream?