简体   繁体   中英

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

day 2: 8h 43min

... )

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.

        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. So to eliminate that enter presses add dummy readLine statement between each line read.

            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. Take a look to this url to see how to read a txt with bufferedReader.

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

Hope this help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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