2013-04-08 166 views
0

我有一个用户可以运行的hacky Ant命令,它可以执行某些操作。该命令提示用户输入。在IntelliJ中,测试时,它可以正常工作。但是当我从终端运行蚂蚁目标时,我会感到奇怪的行为。下面是代码:扫描仪在Mac OSX终端中无法正常工作

Scanner userIn = new Scanner(System.in); 
    PrintWriter writer = new PrintWriter(System.out); 
    writer.println(error + " If you continue with the load, some data may be in a corrupted state. Would you like to continue? (y/n): "); 
    writer.flush(); 
    String userResponse = userIn.next(); 
    while (!(userResponse.equalsIgnoreCase("y") || userResponse.equalsIgnoreCase("n") 
     || userResponse.equalsIgnoreCase("yes") || userResponse.equalsIgnoreCase("no"))) { 
     writer.println("Invalid input. Please specify if you would like to continue with the load. (y/n): "); 
     writer.flush(); 
     userResponse = userIn.next(); 
    } 
    return userResponse.equalsIgnoreCase("y") || userResponse.equalsIgnoreCase("yes"); 

当运行来自终端的蚂蚁命令,则正常显示的错误消息,但然后当用户进入输入,或者没有任何反应,或我必须按进入一堆次任何处理。它也拒绝正确读入输入。如果我输入yes,它仍会永久循环,提示输入,就像用户输入无效。

这是一种错误吗?我无法使用扫描仪吗?

编辑:我已经尝试使用nextLine(),而不是已经。有时候,但不一致我得到一个java.util.NoSuchElementException:没有行发现异常。其他时候,如果我按下输入10次以上,它就会起作用。

回答

1

我一直有同样的问题,我的程序(通过蚂蚁运行)会创建一个扫描仪,但当询问输入,程序似乎只是循环,并不接受任何输入,直到我终止与CTRL + C。原来问题是我的build.xml文件中的“fork”属性。我有叉=“是”,我把它改为“不”,或只是把它拿出来,扫描仪工作得很好。希望这可以帮助!