我正在做测试驱动开发,这需要我为一个接受用户输入的类编写一个测试函数。由于控制台输入函数在测试期间停止输入,因此我使用一个使用字符串的InputStream编写测试。
String str="3\n2\n1\n4\n";
InputStream is = new ByteArrayInputStream(str.getBytes());
assertTrue(app.RunApp(is));这导致调用函数getChoice(InputStream i),该函数涉及来自扫描仪的控制台输入。
public int getChoice(InputStream i) throws IOException {
Scanner s=new Scanner(i);
s.useDelimiter("\n");
String y= s.next();
int x=Integer.parseInt(y);
return x;
}我想让上面的代码一个接一个地获取字符串中的数字。但是,正在发生的是,它正确地获取了第一个数字,然后,流的位置直接到达流的末尾,这导致了NoSuchElementException。请帮帮我!
发布于 2012-07-25 12:39:09
使用...
String y = s.nextLine(); // That will take the entire line instead of only 1st wordhttps://stackoverflow.com/questions/11642808
复制相似问题