Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Java
  4. Help with calling .length on an array

Help with calling .length on an array

Scheduled Pinned Locked Moved Java
javadata-structureshelp
4 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    m009le
    wrote on last edited by
    #1

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

    }

    U L J 3 Replies Last reply
    0
    • M m009le

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

      }

      U Offline
      U Offline
      User 12105981
      wrote on last edited by
      #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.

      1 Reply Last reply
      0
      • M m009le

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

        }

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        See ArrayList (Java Platform SE 7 )[^].

        1 Reply Last reply
        0
        • M m009le

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

          }

          J Offline
          J Offline
          jdferrell
          wrote on last edited by
          #4

          There are key differences between an ArrayList and an Array. You are trying to find the length of an ArrayList, not an array. You are looking for med.size() not med.length

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups