Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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] );
share|improve this question
1  
Show us some code –  Darkhogg Nov 18 '13 at 23:07
    
added some code, although I doubt its very helpful –  James Loper Nov 18 '13 at 23:19

3 Answers 3

up vote 1 down vote accepted

This is difficult without any code, but basically, use the scanner to get the input into string selection, get the integer value into int i with int i = Integer.parseInt(selection);, then myArray[i].

share|improve this answer
    
i've added some code –  James Loper Nov 18 '13 at 23:20
    
Whats the reason I have to store as a string and parse it to an int, rather than just take it as an int in the first place? –  James Loper Nov 18 '13 at 23:21
    
im getting errors. Ill post errors and updated code above –  James Loper Nov 18 '13 at 23:37
    
Because I forgot about Scanner.nextInt() :) Integer.parseInt() is how I would typically get an int value out of command line arguments. –  vangelion Nov 18 '13 at 23:41
Scanner input = new Scanner(System.in);

then output:

urArray[input.nextInt()];
share|improve this answer

You have to read an int, and use this when getting the value stored at the specified index.

A way to do this is the following:

yourArray[scanner.nextInt()];

Where scanner is the Scanner object.

To catch the excpetions you may receive when reading things you assume are numbers, you could do this:

Scanner scanner = new Scanner(System.in);
try {
    yourArray[scanner.nextInt()];

} catch(IllegalStateException | NoSuchElementException e) { // <--- Java 1.7 syntax, in case you wonder
    e.printStackTrace();
}
share|improve this answer
    
how come I have to store the value as a string. Are the index numbers of an array not classed as int? –  James Loper Nov 18 '13 at 23:13
    
I was thinking of two different solutions at the same time. I've updated the answer to better explain what should be done. –  Izmaki Nov 18 '13 at 23:15
    
I received errors with yourarray[scanner.nextInt()]; posted updated code above –  James Loper Nov 18 '13 at 23:40

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.