Help with calling .length on an array
-
import java.util.*;
import java.io.*;public class Main {
public static void main(String[] args) {
Scanner keyb = new Scanner(System.in);
System.out.println("Input:");
String in = keyb.nextLine();
System.out.println("Output:");
String out = keyb.nextLine();int sum = 0; long product = 1; double count = 0; ArrayList med = new ArrayList(); File file = new File(in); Scanner f = new Scanner(file); while ( f.hasNextInt() ) { int num = f.nextInt(); sum += num; product \*= num; med.add(num); ++count; } PrintWriter pw = new PrintWriter(out); pw.println("Sum: " + sum); pw.println("Product: " + product); pw.println("Average: " + (sum/count)); Collections.sort(med); int middle = med.length /2; if (med.length % 2 == 1) { pw.println("Median: " + med.get(middle)); } else { pw.println("Median: " + (med.get(middle) + med.get(middle-1)) / 2); } }
}
-
import java.util.*;
import java.io.*;public class Main {
public static void main(String[] args) {
Scanner keyb = new Scanner(System.in);
System.out.println("Input:");
String in = keyb.nextLine();
System.out.println("Output:");
String out = keyb.nextLine();int sum = 0; long product = 1; double count = 0; ArrayList med = new ArrayList(); File file = new File(in); Scanner f = new Scanner(file); while ( f.hasNextInt() ) { int num = f.nextInt(); sum += num; product \*= num; med.add(num); ++count; } PrintWriter pw = new PrintWriter(out); pw.println("Sum: " + sum); pw.println("Product: " + product); pw.println("Average: " + (sum/count)); Collections.sort(med); int middle = med.length /2; if (med.length % 2 == 1) { pw.println("Median: " + med.get(middle)); } else { pw.println("Median: " + (med.get(middle) + med.get(middle-1)) / 2); } }
}
Member 12877930 wrote:
med.length
l think you are trying to get the size of the ArrayList, you can use
med.size
which will give you the size of the list. However, remember that Collection is an interface, thus not supposed to be instantiated. So you can use any of the implementation classes to sort.
-
import java.util.*;
import java.io.*;public class Main {
public static void main(String[] args) {
Scanner keyb = new Scanner(System.in);
System.out.println("Input:");
String in = keyb.nextLine();
System.out.println("Output:");
String out = keyb.nextLine();int sum = 0; long product = 1; double count = 0; ArrayList med = new ArrayList(); File file = new File(in); Scanner f = new Scanner(file); while ( f.hasNextInt() ) { int num = f.nextInt(); sum += num; product \*= num; med.add(num); ++count; } PrintWriter pw = new PrintWriter(out); pw.println("Sum: " + sum); pw.println("Product: " + product); pw.println("Average: " + (sum/count)); Collections.sort(med); int middle = med.length /2; if (med.length % 2 == 1) { pw.println("Median: " + med.get(middle)); } else { pw.println("Median: " + (med.get(middle) + med.get(middle-1)) / 2); } }
}
-
import java.util.*;
import java.io.*;public class Main {
public static void main(String[] args) {
Scanner keyb = new Scanner(System.in);
System.out.println("Input:");
String in = keyb.nextLine();
System.out.println("Output:");
String out = keyb.nextLine();int sum = 0; long product = 1; double count = 0; ArrayList med = new ArrayList(); File file = new File(in); Scanner f = new Scanner(file); while ( f.hasNextInt() ) { int num = f.nextInt(); sum += num; product \*= num; med.add(num); ++count; } PrintWriter pw = new PrintWriter(out); pw.println("Sum: " + sum); pw.println("Product: " + product); pw.println("Average: " + (sum/count)); Collections.sort(med); int middle = med.length /2; if (med.length % 2 == 1) { pw.println("Median: " + med.get(middle)); } else { pw.println("Median: " + (med.get(middle) + med.get(middle-1)) / 2); } }
}