java - Arrays and input -
my assignment asks me write program let user input 10 players' name, age, position, , batting average. program should check , display statistics of players under 25 years old , have batting average of .280 or better, display them in order of age.
i've written code input section (where it'll store them in array):
static int players[] = new int [10]; static string name[] = new string [10]; static double average [] = new double [10]; static int age[] = new int [10]; static string position[] = new string [10]; //method input names of blue jays public static void inputinfo() throws ioexception{ bufferedreader br = new bufferedreader(new inputstreamreader(system.in)); for(int = 0; < players.length; i++) { system.out.println("enter player information."); system.out.println("input first , last name: "); name [i] = br.readline(); system.out.println("input position: "); position[i] = br.readline(); system.out.println("input batting average (e.g. .246): "); string averagestring = br.readline(); average [i] = double.parsedouble(averagestring); system.out.println("input age: "); age[i] = br.read(); system.out.println(" "); } }
my problem input. first player input shows me (as should):
input first , last name: john smith input position: pitcher input batting average (e.g. .246): .300 input age: 27
but second input skips name section , jumps position input. can't figure out why it's doing this! can me out? in advance!
the read
method reads single character of input; rest of line you've entered remains in stream.
when next loop starts, readline
detects can read rest of line already, no user input. thinks user input given.
for input age, use readline
instead of read
, , can use double.parsedouble
convert resulting string
input double
.
Comments
Post a Comment