-1

So I'm trying to take an input from a user using Scanner(System.in) but when i try to type something into the console it won't let me.

Can anyone help?

Function.show();

Scanner input = new Scanner(System.in);

if (input.equals("a"))
{
    Function.outputFile(1, list);
}

input.close();
skaffman
  • 398,947
  • 96
  • 818
  • 769
bananamana
  • 485
  • 1
  • 9
  • 19
  • What do you mean by "it won't let me"? Does it throw an exception? Does it just hang? Does the program complete but not display anything or change the output file? – Pops Oct 31 '10 at 21:29

2 Answers2

1

You're forgetting to call next on the Scanner. Your if line should be if (input.next().equals("a")), instead.

Yuliy
  • 17,381
  • 6
  • 41
  • 47
1

I would recommend using input.next.charAt(0) in a switch...

Function.show(); 

Scanner input = new Scanner(System.in); 

switch (input.next().charAt(0)) {
    case 'a': { 
        Function.outputFile(1, list); 
        break;
    }
    case 'b': {
        etc
    }

If you separate it, (I.E. char letter) you can use switch (letter.toUpperCase()) [in theory... I've never tried it] and then you don't have to worry about case.

Unihedron
  • 10,902
  • 13
  • 62
  • 72
Joe's Morgue
  • 173
  • 2
  • 8