简体   繁体   English

麻烦从文本文件中读取单个字符

[英]Troubles reading a single character from a text file

I'm writing an "app" that takes in time input from the user and stores the hours and the minutes separately for each day in a text file (giving a result that looks like: 我正在编写一个“应用程序”,它接受用户输入的时间,并将每天的小时和分钟分别存储在文本文件中(给出的结果如下:

day 1: 8h 45min 第一天:8小时45分钟

day 2: 8h 43min 第2天:8小时43分钟

... ) ...)

the idea behind it is to use this data for multiple stuff, like calculating the average time, or just accessing the time at any day, but I haven't reached that stage yet, I'm having troubles doing the simplest stuff like reading the hour and printing it. 其背后的想法是将这些数据用于多个工作,例如计算平均时间,或仅访问每天中的时间,但我还没有达到那个阶段,我在做最简单的工作(例如阅读小时并打印。

here's the code 这是代码

import java.util.Scanner;
import java.io.*;

public class TimeInput {

  public static void main(String[] args) {

    write();
    read();

  }

  static void write() {

    int dayOfMonth = 1;
    String fileName = "time.txt";

    int[] time = new int[2];
    String timeDisplay;

    Scanner s = new Scanner(System.in);

    try {

        FileWriter fileWriter = new FileWriter(fileName);
        BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);


        while (dayOfMonth <=31) {

            System.out.println("Day " + dayOfMonth);
            System.out.print("Enter hour: " + "__" + "h\r");
            System.out.print("Enter hour: ");
            time[0] = s.nextInt();

            System.out.print("Enter minutes: " + "__" + "min\r");
            System.out.print("Enter minutes: ");
            time[1] = s.nextInt();


            timeDisplay = ("\n"+ "day " + dayOfMonth + ": " + time[0] + "h " + time[1] + "min");

            bufferedWriter.write(timeDisplay);
            bufferedWriter.newLine();

            dayOfMonth++;

            if (time[0] == 0 && time[1] == 0) {
                bufferedWriter.close();
                dayOfMonth = 32; // break
            }
        }
    }

    catch (IOException ex) {
        System.out.println(
        "Error writing to file '" + fileName + "'");
    }
}

static void read() {

    String fileName = "time.txt";
    String line = null;

    try {

        FileReader fileReader = new FileReader(fileName);

        BufferedReader bufferedReader = new BufferedReader(fileReader);

        while ((line = bufferedReader.readLine()) != null) {
            char h = line.charAt(7);
            System.out.println(h);
        }

        bufferedReader.close();
    }
    catch (FileNotFoundException ex) {
        System.out.println(
        "Unable to open file '" + fileName + "'" );
    }
    catch (IOException ex) {
        System.out.println(
        "Error reading file '" + fileName +"'");
    }
  }
}

I keep getting a String out of bounds exception and I don't understand why 我不断收到字符串超出范围的异常,但我不明白为什么

You need to check empty string before char operation. 在进行char操作之前,您需要检查空字符串。

        while ((line = bufferedReader.readLine()) != null) {
            if("".equals(line)){
                continue;
            }
            char h = line.charAt(7);
            System.out.println(h);
        }

Buffered Writer also saved enter key presses between your input. Buffered Writer还保存了输入之间的回车键。 So to eliminate that enter presses add dummy readLine statement between each line read. 因此,为了消除回车readLine在每行读取之间添加虚拟readLine语句。

            line=bufferedReader.readLine();
            while ((line = bufferedReader.readLine()) != null) {
                System.out.println(line);

                char h = line.charAt(7);
                System.out.println(h);
                line=bufferedReader.readLine();
            }

Take a look to the last loop. 看一下最后一个循环。

Put a breakpoint and see what happen. 放置一个断点,看看会发生什么。 Maybe the ArrayIndexOutBoundExceptions happens cuz youre trying to read more that one character and this cant be possible. 也许发生ArrayIndexOutBoundExceptions是因为您试图读取更多的一个字符,而这是不可能的。 Take a look to this url to see how to read a txt with bufferedReader. 看一下此URL,以了解如何使用bufferedReader读取txt。

http://www.mkyong.com/java/how-to-read-file-from-java-bufferedreader-example/ http://www.mkyong.com/java/how-to-read-file-from-java-bufferedreader-example/

Hope this help. 希望能有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM