如何从缓冲读取器输入字符串?

发布于 2024-08-13 08:37:34 字数 801 浏览 4 评论 0原文

我也主要使用扫描仪,并且也想尝试使用缓冲阅读器: 这是我到目前为止所拥有的,

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

韶华倾负 2024-08-20 08:37:34

keyboardInput.readLine() 已经返回一个字符串,因此您只需执行以下操作:

return keyboardInput.readLine();

(update)

readLine 方法会抛出 IOException.您要么抛出异常:

public String getFlavour() throws IOException {
   ...
}

要么在您的方法中处理它。

public static String getFlavour() {
    BufferedReader keyboardInput = null;
    try {
        keyboardInput = new BufferedReader(new InputStreamReader(System.in));
        System.out.println(" please enter your flavour ice cream");
        // in this case, you don't need to declare this extra variable
        // String flavour = keyboardInput.readLine();
        // return flavour;
        return keyboardInput.readLine();
    } catch (IOException e) {
        // handle this
        e.printStackTrace();
    }
    return null;
}

keyboardInput.readLine() already returns a string so you should simply do:

return keyboardInput.readLine();

(update)

The readLine method throws an IOException. You either throw the exception:

public String getFlavour() throws IOException {
   ...
}

or you handle it in your method.

public static String getFlavour() {
    BufferedReader keyboardInput = null;
    try {
        keyboardInput = new BufferedReader(new InputStreamReader(System.in));
        System.out.println(" please enter your flavour ice cream");
        // in this case, you don't need to declare this extra variable
        // String flavour = keyboardInput.readLine();
        // return flavour;
        return keyboardInput.readLine();
    } catch (IOException e) {
        // handle this
        e.printStackTrace();
    }
    return null;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文