Home > Back-end >  Why the ouput display the first line of the data in .txt?
Why the ouput display the first line of the data in .txt?

Time:07-04

Why the data only print for the second line only? Supposedly it will print all the data by columns right? Which line I made a mistake on this java programming?

What I've done and won't work:

  • Put for(int i=0; i < cols.length; i );
  • Put while (sc.hasNextLine())

Above information that i stated is both give me this:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 1 out of bounds for length 1
    at Forensic.main(Forensic.java:28)

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

public class Forensic 
{
    public static void main(String[] args) throws IOException
    {
        try
        {
            File fin = new File("Bill.txt");                                            
            FileReader fileReader = new FileReader(fin);
            BufferedReader bufReader = new BufferedReader(fileReader);
    
            File foutBelow = new File("BelowAmount.txt");
            FileWriter writerBelow = new FileWriter(foutBelow);
            PrintWriter printBelow = new PrintWriter(writerBelow);
    
            printBelow.println("Payment less than or equal to RM 1000");
            printBelow.println("Record\t\tUser ID\t\t\tPayment");
    
            String string = ""; 
            Scanner scan = new Scanner(bufReader);
            while((string = bufReader.readLine()) != null)
            {   
                string = scan.nextLine();
                String cols[] = string.split(","); 
    
                printBelow.println(cols[0]   "\t\t\t"   cols[1]   "\t\t\t\t"   cols[2]);
            }
            System.out.println("Data successfully transfered");
    
            bufReader.close();
            printBelow.close();
        }
        catch (FileNotFoundException fnfE)
        { 
            System.out.println("File not found");
        }
        catch (IOException ioE)                                           
        { 
            ioE.printStackTrace();
        }
    }
}

in Bill.txt file

1208,236,289.90
1209,221,299.70
1210,236,479.60
1211,236,200.00
1212,221,560.60
1213,289,4000.00
1214,289,235.60
1215,236,280.50
1216,221,100.20
1217,221,2800.30
1218,236,1400.70
1219,289,778.90
1220,289,778.90
1221,236,420.50
1222,277,235.60
1223,277,229.90
1224,236,479.60
1225,221,300.20
1226,289,1400.70
1227,236,479.60

Output of BelowAmount.txt

Payment less than or equal to RM 1000
Record      User ID         Payment
1209            221             299.70

The output is above. Display directly second row of the data.

CodePudding user response:

In Java, there are many ways to read a file, format its contents and write the formatted contents to another file but you seem to be mixing them all together.

The below code shows one way, which uses class java.util.Scanner.
(Notes after the code.)

import java.io.IOException;
import java.io.PrintWriter;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Scanner;

public class Forensic {

    public static void main(String[] args) {
        Path source = Paths.get("Bill.txt");
        try (Scanner scan = new Scanner(source);
             PrintWriter printBelow = new PrintWriter("BelowAmount.txt")) {
            printBelow.println("Payment less than or equal to RM 1000");
            printBelow.println("Record\t\tUser ID\t\t\tPayment");
            while (scan.hasNextLine()) {
                String string = scan.nextLine();
                String[] cols = string.split(",");
                if (cols.length == 3) {
                    printBelow.printf("%s\t\t\t%s\t\t\t\t%s%n", cols[0], cols[1], cols[2]);
                }
            }
        }
        catch (IOException xIo) {
            xIo.printStackTrace();
        }
    }
}

Don't assume that the contents of the file you are reading is as you expect it to be. Hence, in the above code, after calling method split, I check to make sure that the line read from the file has the expected format.

Refer to Scanning and Formatting in Oracle's Java tutorials.

Refer to the API documentation to see how to create and use classes like java.io.PrintWriter, for example. It has a constructor that takes a string argument. Hence no need to create a File and then a FileWriter in order to create a PrintWriter.

I also advise using try-with-resources to ensure that you close all the files you use in your program.

  • Related