java - issues with a for loop in a user entered array -


this homework having issues part of assignment here part

"start asking user how many double values want read in. allows them input them 1 @ time. exception should thrown if given faulty values. store values read in array."

the issue having loop asks user input numbers want throws 5 errors can't figure out or there easier way user input?. here code

//import statements import java.util.*;     //for scanner class   // class beginning class  standarddev { public static void main(string[] args ) {     //declare variables area     scanner input = new scanner (system.in);       //collect inputs user or read in data here     system.out.println("welcome standard deviation , average program!");     system.out.println("how many numbers want enter? ");     int number =input.nextint();     double [] value = new double [number];       //echo input values user here     system.out.println("you want enter in "+number+" numbers, lets start");       //main code , calculations      int i;     ( = 0;, <number;, i++){         value[i] = input.nextdouble();         system.prinln("here numbers have entered "+value+"!");     }     //output results here 

and if can tell me how check if number entered isn't valid input (is different if wasn't array?) , correctly entering in doubles program?

you have fix statement removing commas , using system.out.println rather system.prinln

in way:

for ( = 0; <number; i++){     value[i] = input.nextdouble(); } system.out.println("here numbers have entered "     + arrays.tostring(value) + " !"); 

the result be:

welcome standard deviation , average program! how many numbers want enter?

3

you want enter in 3 numbers, lets start

1 2 3

here numbers have entered [1.0, 2.0, 3.0]!

about validation:

you can modify statement in way in order catch wrong input , allow user re-enter value:

int = 0; while(i < number) {     try {         system.out.println("enter value " + (i+1) + "/" + number + ":");         value[i] = input.nextint();         i++;     } catch(inputmismatchexception e) {         system.out.println("this not valid input! please enter again!");         input.next();   // consume invalid token     } } 

this final result:

welcome standard deviation , average program!

how many numbers want enter?

3

you want enter in 3 numbers, lets start

enter value 1/3: 1

enter value 2/3: 2

enter value 3/3: a

this not valid input! please enter again!

enter value 3/3: 3

here numbers have entered [1.0, 2.0, 3.0] !


Comments

Popular posts from this blog

jquery - How do you format the date used in the popover widget title of FullCalendar? -

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -