【问题标题】:BufferedReader reads older inputBufferedReader 读取较旧的输入
【发布时间】:2013-05-13 14:17:02
【问题描述】:

Java 初学者在这里。为了测试,我创建了自己的使用 BufferedReader 的输入类。代码如下所示:

import java.io.BufferedReader;

import java.io.IOException;
import java.io.InputStreamReader;

public class inputter {

    private static BufferedReader read = new BufferedReader(new InputStreamReader(System.in));
    /**
     * @param
     * A reader for chars
     * @throws IOException 
     */
    public static char getChar() throws IOException{
        int buf= read.read();
        char chr = (char) buf;
        while(!Character.isLetter(chr)){
            buf= read.read();
            chr = (char) buf;
        }
        return chr;
    }

    /**
     * @param currencies, names
     * A reader for Ints
     * @throws IOException 
     * 
     */public static int getInt()throws IOException{
        String buf = read.readLine();
        while(!buf.matches("[-]?(([1-9][0-9]*)|0)")){
            buf = read.readLine();
            System.out.print("No valid input. Please try again.");
        }
        return Integer.parseInt(buf);
    }

     /**
         * @param currencies, names
         * A reader for Floats
         * @throws IOException 
         * 
         */
    public static float getFloat()throws IOException{
        String buf = read.readLine();
        while(!buf.matches("[-]?(([1-9][0-9]*)|0)(\\.[0-9]+)?")){
            System.out.print("No valid input. Please try again.\n");
            buf = read.readLine();
        }
        return java.lang.Float.parseFloat(buf);
    }

}

它的问题是,每当我读取一个字符,然后尝试读取一个整数,它就会跳转到 else 条件并输出No valid input. Please try again。我认为这是因为有旧的输入(例如换行符)到处乱飞。我该如何清理它?

【问题讨论】:

  • 你试过调试你的代码吗?我认为问题在于,您不能将字符串 (-> char[]) 转换为 char。也许读取缓冲区包含不可打印的字符?
  • 我认为这不是问题,因为它可以正常工作多次,并且当我只使用 float 和 int 时也会打印错误消息。
  • 你是如何输入的?你在输入类似“a”[输入键]“1”[输入键]的东西吗?
  • 等等!类型转换和 Class Float 或 Integer 的 Parsing-method 之间有很大很大的区别!
  • 我知道。也许我不明白你在暗示什么?! @Farlan,是的,我就是这样做的。

标签: java newline inputstream bufferedreader


【解决方案1】:

看来问题出在您的输入序列上:

尝试输入以下序列:“a1[enter]”。您的代码应该适用于这种输入。但是,如果您输入“a[enter]1[enter]”,您的代码应该会失败。 原因是 [enter] 键只有在你执行下一个 readline() 时才会处理,它不会匹配数字格式,因此进入你的 else 条件。

【讨论】:

  • 感谢您的努力,但这不是问题所在。我找到了错误的序列。问题是换行符保留在缓冲区中。 while 序列后面的一个简单 read.readLine() 完成了这项工作。
  • public static char getChar() throws IOException{ String buf= read.readLine(); char chr = (char) buf.charAt(0); while(buf.length()
【解决方案2】:

一段时间后,我最终自己发现并重写了代码。它现在看起来如下并且完美运行(至少对我而言):

public static char getChar() throws IOException{ 
    String buf= read.readLine(); 
    char chr = (char) buf.charAt(0); 
    while(buf.length() <= 1 && !Character.isLetter(chr)){ 
        System.out.println("This is not valid input. Please try again."); 
        buf= read.readLine(); 
        chr = (char) buf.charAt(0); 
    } 
    read.readLine(); 
    return chr; 
}

我实际上又重写了一遍,以便更容易,但结果略有不同:

public static char getChar() throws IOException{
        int buf= read.read();
        char chr = (char) buf;
        while(!Character.isLetter(chr)){
            buf= read.read();
            chr = (char) buf;
        }
        return chr;
    }

我现在经常使用它们,但我仍然只是一个初学者,所以我可能会再次重写它们。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-17
    • 2018-05-01
    • 2013-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多