如何从缓冲读取器输入字符串?
我也主要使用扫描仪,并且也想尝试使用缓冲阅读器: 这是我到目前为止所拥有的,
import java.util.*;
import java.io.*;
public class IceCreamCone
{
// variables
String flavour;
int numScoops;
Scanner flavourIceCream = new Scanner(System.in);
// constructor
public IceCreamCone()
{
}
// methods
public String getFlavour() throws IOexception
{
try{
BufferedReader keyboardInput;
keyboardInput = new BufferedReader(new InputStreamReader(System.in));
System.out.println(" please enter your flavour ice cream");
flavour = keyboardInput.readLine();
return keyboardInput.readLine();
}
catch (IOexception e)
{
e.printStackTrace();
}
}
我相当确定会得到一个 int 你可以说,
Integer.parseInt(keyboardInput.readLine());
但是如果我想要一个 String 我该怎么办
Im used too using Scanner mainly and want too try using a buffered reader:
heres what i have so far
import java.util.*;
import java.io.*;
public class IceCreamCone
{
// variables
String flavour;
int numScoops;
Scanner flavourIceCream = new Scanner(System.in);
// constructor
public IceCreamCone()
{
}
// methods
public String getFlavour() throws IOexception
{
try{
BufferedReader keyboardInput;
keyboardInput = new BufferedReader(new InputStreamReader(System.in));
System.out.println(" please enter your flavour ice cream");
flavour = keyboardInput.readLine();
return keyboardInput.readLine();
}
catch (IOexception e)
{
e.printStackTrace();
}
}
im fairly sure to get an int you can say
Integer.parseInt(keyboardInput.readLine());
but what do i do if i want a String
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
keyboardInput.readLine()
已经返回一个字符串,因此您只需执行以下操作:(update)
readLine
方法会抛出IOException.您要么抛出异常:
要么在您的方法中处理它。
keyboardInput.readLine()
already returns a string so you should simply do:(update)
The
readLine
method throws anIOException
. You either throw the exception:or you handle it in your method.