java - Differentiating String/Int User Input -
i having issues trying type code make program distinguish whether user inputs string value or int value. if int value typed in, stored array (named data) printed , tested typing string values (as seen in code below in method go()
. using .hasnextint
incorrectly? here code:
import java.util.scanner; import java.util.random; import java.text.decimalformat; public class integerstatistics { java.util.scanner scan; // declare storage integers int[] data; random random; // create constructor public integerstatistics() { scan = new scanner(system.in); data = new int[10]; random = new random(); } private void showmenu() { system.out.println("menu:"); system.out.println(" p - print list of values"); system.out.println(" s - print statistics values"); system.out.println(" f - fill list random values"); system.out.println(" c - clear list of values"); system.out.println(" h - print out menu"); system.out.println(" x - exit program"); } private void clearvalues() { system.out.print("the values: [0"); // empty (zero out) array int = 1; while(i < data.length) { system.out.print(", 0"); i++; } system.out.println("]"); } private void filllist() { data[0] = (random.nextint(26) - 10); system.out.print("the values: [" + data[0]); for(int = 1; < data.length; i++) { data[i] = (random.nextint(26) - 10); system.out.print(", " + data[i]); } system.out.println("]"); } private void printvalues() { data[0] = 1; system.out.print("the values: [" + data[0]); // print values for(int = 1; < data.length; i++) { data[i] = + 1; system.out.print(", " + data[i]); } system.out.println("]"); } private void printstats() { int sum = 0; int max = data[0]; int min = data[0]; // calculate stat values of array for(int = 0; < data.length; i++) { // calculate sum of values sum += data[i]; // find maximum value in array if(data[i] > max) { max = data[i]; // find minumum value in array } else if(data[i] < min) { min = data[i]; } } // caculate average of values decimalformat df = new decimalformat("#.000"); double avgvalue = (sum / ((double)data.length)); // print stat values of array system.out.println("sum of values: " + sum); system.out.println("maximum value: " + max); system.out.println("minimum value: " + min); system.out.printf("average value: " + df.format(avgvalue) + "\n"); } public void go() { system.out.println("welcome simple statistics program\n"); string input; int inputnum = integer.parseint(s); showmenu(); int index = 0; { system.out.print("enter command or integer: "); input = scan.next(); inputnum = scan.nextint(); if(inputnum.hasnextint()) { data[index] = input.hasnexint(); } else if(input.equals("p")) { printvalues(); } else if(input.equals("s")) { printstats(); } else if(input.equals("f")) { filllist(); } else if(input.equals("c")) { clearvalues(); } else if(input.equals("h")) { showmenu(); } else if(input.equals("x")) { // nothing } else { system.out.println("unrecognized command. try again."); showmenu(); } } while( ! input.equals("x")); system.out.println("\nthank using simple statistics program"); } public static void main(string[] args) { new integerstatistics().go(); } }
the array created through input of int can partially filled,but must not exceed length of ten integers. let me know clarification needed.
you can define string , try convert integer, if throws numberformatexception because isn't number.
Comments
Post a Comment