I've created an array 8 elements long using the scanner class. I'm trying to accept user inputs via scanner and print out the value at that index location. eg if the user enters '2', it prints out the value at the second element. I've searched on google but all the resources are how to use the scanner to input data into an array, not using the scanner to retreive data back. the only way I thought of is by using a ridiculous amount of if else statements but there must be a cleaner way to do it in a loop?
This is my array, I've used the scanner to fill my array. Now, on prompt, the user must input a number from 1 to 8. If they input 1, print out solution[0]. Input 8, print out solution[7] etc. Hope its easier to understand now
String[] solution = new String[8];
Scanner scan = new Scanner( System.in );
for( int i = 0; i < solution.length; i++ ){
System.out.println( "Enter solution:" );
solution[ i ] = scan.next();
}
scan.close();
Scanner scan1 = new Scanner( System.in );
String selection;
System.out.println("Enter an int between 0 and 7 to retrieve the Selection: ");
selection = scan1.next();
int i = Integer.parseInt(selection);
System.out.println( "The Selection is: " + solution[i] );