java - How to use Ctrl+Z to stop asking for input? -


i writing program takes integers user , stores them in array, calculates arrays average.

the array can hold @ maximum 100 integers. if user wants less 100, hit ctrl+z (or command+d) stop prompting numbers.

here main method:

 public static void main(string[] args) {         scanner input = new scanner (system.in);      int [] array = new int[100];         system.out.printf("enter stream of numbers: ");          readintoarray(input, array);         (int = 0; i<=array.length;i++) {             array[i] = input.nextint();         }     } 

and here method reads array.

public static int readintoarray(scanner input, int[] nums) {     int count = 0; //number of elements entered array     while (count <= nums.length && input.hasnextint()) {         nums[count]=input.nextint();         count++;             }      return count; }  

and here average method.

public static void printaboveaverage(int[] nums, int size) {     double average;     int sum = 0;      (int = 0; < nums.length; i++) {         sum =+ nums[i];     }      average = sum/size;          system.out.print(average); 

what doing wrong?

i keep getting nosuchelementexception after hitting ctrl+z.

here:

while (count <= nums.length && input.hasnextint()) { 

this loop stop when hit ctrl-z , there no more int. next statement is:

array[i] = input.nextint(); 

in other words: read method seems correctly check if enough numbers in, or if scanner stopped having input.

but main method ignores that, , asks another number scanner.

so simple as: drop loop within main method wants more numbers.


Comments

Popular posts from this blog

html - How to set bootstrap input responsive width? -

javascript - Highchart x and y axes data from json -

javascript - Get js console.log as python variable in QWebView pyqt -