java - creating array with elements determined by user/input -


trying make array, , want elements large input be. should take x amount of numbers, place in array, , return average. first need figure out how placing size of elements in array done.

import java.util.scanner; import java.util.arraylist; //needs array  public class statsv2 {    public static void main (string args[])     {         int[] a= new int[5];         scanner sc=new scanner(system.in);         system.out.println("please enter numbers...");         for(int j=0;j<5;j++)         {             a[j]=sc.nextint();         }      } }  

so want '5'(which appears twice) many times user keeps entering new numbers

first of all, instead of j<5, use yourarray.length.

as array size, cannot done dynamically, have 3 options:

1) create array large enough user won't overrun it, you'll have checking in order stop @ end of user input.

2) use arraylist, can dynamically add to, call 'toarray()' when user done entering data extract array processing.

3) skip array altogether , use arraylist hold , process input.


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -