开发者

I'm trying to take an input using Scanner(System.in) but it's not letting me. Help! [duplicate]

This question already has an answer here: 开发者_如何学C How to use java.util.Scanner to correctly read user input from System.in and act on it? (1 answer) Closed 5 years ago.

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();


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜