简体   繁体   中英

Unable to read data from file in java

I am working in java code to read data from a text file and print the data in another text file. I didn't have any errors, but when I run it shows like there is no input and all I got was this as output and an empty file (in the printwriter file). But, I am pretty sure that there is input in the file that I read using the scanner.

 Process finished with exit code 0

This is my code:

import java.io.*;
import java.util.Scanner;
public class ASSI4 {
    public static void main(String[] args) {
        try {
            Scanner kbd = new Scanner(new File("st.txt"));
            PrintWriter nbb = new PrintWriter((new File("res.txt")));
            long[] ID = new long[30];
            double[][] CM = new double[30][6];
            while (kbd.hasNextLine()) {
                for (int i = 0; i < 30; i++) {
                    String line = kbd.nextLine();
                    String[] arr = line.split("\t");
                    ID[i] = Long.parseLong(arr[0]);

                    {
                        CM[i][0] = Double.parseDouble(arr[1]);
                        CM[i][1] = Double.parseDouble(arr[2]);
                        CM[i][2] = Double.parseDouble(arr[3]);
                        CM[i][3] = Double.parseDouble(arr[4]);
                        CM[i][4] = Double.parseDouble(arr[5]);
                        CM[i][5] = Double.parseDouble(arr[6]);
                    }
                }

                for (int i = 0; i < 30; i++) {
                    double sumA = 0;
                    for (int j = 0; j < 6; j++) {
                        sumA = sumA + CM[i][j];
                    }
                    double avg = sumA / 6;
                    avg = Math.round(avg * 100.0) / 100.0;
                    nbb.println(ID[i] + "  " + CM[i][0] + "  " + CM[i][1] + "  " + CM[i][2] + "  " + CM[i][3] + "  " + CM[i][4] + "  " + CM[i][5] + " " + avg);
                }
            }
            nbb.close();
            kbd.close();


        }
        catch (Exception e) {
            e.getMessage();
        }
    }
}

This is my st.txt file:

20186966 90.0 55.8 98.0 67.9 46.0 77.1
20185978 32.0 19.8 49.8 67.0 86.4 46.0
20189409 53.8 79.0 84.6 39.9 74.5 27.0
20183970 39.7 88.0 78.6 66.0 25.4 0.0
20190291 36.9 32.8 10.0 46.8 21.5 65.9 
20196949 99.0 47.6 36.7 85.5 82.9 77.0
20173040 46.8 40.9 30.5 95.9 94.9 99.8 
20171679 55.7 29.0 99.8 36.8 69.0 80.6 
20175747 77.0 100.0 56.7 36.9 34.8 22.9 
20190850 84.0 85.5 68.8 81.7 83.9 57.8 
20193406 20.0 44.5 50.0 18.6 30.9 77.8 
20175802 71.9 80.0 60.8 99.9 31.0 87.9 
20184693 46.0 94.9 58.7 73.0 35.7 29.5
20185769 76.9 67.8 51.8 12.6 96.0 94.0
20181274 28.9 85.9 76.6 42.9 32.0 95.9
20194284 1.0 44.8 75.6 44.8 33.9 30.6
20175501 70.9 24.5 48.8 80.5 76.8 61.9
20198125 6.0 16.0 12.9 25.0 3.0 29.6
20194027 33.0 27.7 83.9 31.0 75.0 74.9
20179253 87.0 100 22.9 68.9 14.8 98.0
20172010 94.0 93.9 31.9 100 70.8 90.0
20177254 11.0 26.5 2.6 30.8 86.0 100.0
20184917 60.9 24.3 49.6 61.9 66.0 80.9
20181322 83.0 45.3 74.5 64.5 50.0 99.7
20196820 78.0 75.0 70.9 56.9 99.0 19.9
20196734 35.4 34.9 84.8 68.0 15.9 70.0
20192018 91.0 59.5 3.4 42.9 81.0 58.8
20178007 55.9 94.8 71.5 38.0 81.7 98.0
20173948 93.0 100.0 9.8 45.7 47.0 9.9
20192749 26.0 45.0 57.4 95.9 14.4 79.0

Problem:

You have used \t instead of \\s+ . We should use \\s+ to match a whitespace character: [ \t\n\x0B\f\r] . Check this to learn more about patterns.

Side notes:

  1. Follow Java naming conventions eg ID should be named as id and CM should be named as cm .
  2. Do not mix reading with writing ie read the source file and close it and then write to the target file and close it.
  3. Replace e.getMessage(); with e.printStackTrace(); or at least System.out.println(e.getMessage()); .

Updated code:

import java.io.File;
import java.io.PrintWriter;
import java.util.Scanner;

public class ASSI4 {
    public static void main(String[] args) {
        try {
            Scanner kbd = new Scanner(new File("st.txt"));
            long[] id = new long[30];
            double[][] cm = new double[30][6];
            while (kbd.hasNextLine()) {
                for (int i = 0; i < 30; i++) {
                    String line = kbd.nextLine();
                    String[] arr = line.split("\\s+");
                    id[i] = Long.parseLong(arr[0]);
                    cm[i][0] = Double.parseDouble(arr[1]);
                    cm[i][1] = Double.parseDouble(arr[2]);
                    cm[i][2] = Double.parseDouble(arr[3]);
                    cm[i][3] = Double.parseDouble(arr[4]);
                    cm[i][4] = Double.parseDouble(arr[5]);
                    cm[i][5] = Double.parseDouble(arr[6]);
                }
            }
            kbd.close();

            PrintWriter nbb = new PrintWriter((new File("res.txt")));
            for (int i = 0; i < 30; i++) {
                double sumA = 0;
                for (int j = 0; j < 6; j++) {
                    sumA = sumA + cm[i][j];
                }
                double avg = sumA / 6;
                avg = Math.round(avg * 100.0) / 100.0;
                nbb.println(id[i] + "  " + cm[i][0] + "  " + cm[i][1] + "  " + cm[i][2] + "  " + cm[i][3] + "  "
                        + cm[i][4] + "  " + cm[i][5] + " " + avg);
            }
            nbb.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

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