String readline() in Java

Asked

Viewed 1,219 times

1

I’m having difficulty instantiating the readline() method correctly in java so that you read the file, run the while loop and write the desired data. If anyone can help me, I’d really appreciate it!

main java code.:

package com.nayana.exercicio1;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Date;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.HashSet;



public class Main {

    FileReader FileReader;
    BufferedReader BufferedReader;
    DateFormat format;
    HashSet<String> customer;
    HashSet<String> country;
    SimpleDateFormat formatter;
    Date date;
    ParseException ParseException;
    IOException IOException;
    int lines;
    String columns[];
    String line;
    String string;
    String formattedDate;
    Date datex;
    String readLine() {
        return null;

    }


}

process code.java:

package com.nayana.exercicio1;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashSet;

public class Process {

    public static void main(String[] args) throws FileNotFoundException, ParseException {
        Main pr = new Main();

        pr.BufferedReader = new BufferedReader(new FileReader("C:\\Users\\nayan\\Downloads\\orders_04_20_07.txt"));



        new String();

        pr.lines = 0;

        pr.customer = new HashSet<String>();
        pr.country = new HashSet<String>();


        System.out.println("Estas são as datas formatadas: " );


        while(pr.readLine() != null) {

            pr.columns =  ((String) pr.line).split(";");
            pr.lines++; 

            //CONTAR QUANTOS COSTUMERID DIFERENTES EXISTEM

            pr.customer.add(pr.columns[4]);
            pr.customer.size();

            //MOSTER DIFERENTES PAÍSES

            pr.country.add(pr.columns[6]);

            //CONVERTER DATA

            pr.string = pr.columns[2];
            pr.format = new SimpleDateFormat("dd-MMM-yyyy");
            pr.date = (Date) pr.format.parse(pr.string);
            pr.format.format(pr.date);
            pr.formattedDate= "";



            try {

                pr.datex = (Date) pr.format.parse(pr.columns[2]);
                pr.formatter = new SimpleDateFormat("yyyy-dd-MM");
                pr.formattedDate = pr.formatter.format(pr.datex);
            } catch (ParseException e1) {

                e1.printStackTrace();
            }

            System.out.println(pr.formattedDate);


        }
        System.out.println("\nO número total de linhas é: " + pr.lines);
        System.out.println("\nO número total de CustomerId é: " + pr.customer.size());
        System.out.println("\nDiferentes países existentes" + pr.country);
    }

}
  • The loop is infinite?

  • Yes, the goal would be to read all lines of the txt document. Is it wrong? Can you help me?

  • Readline() read the whole file or per line?

  • Read the whole file

  • Then use a comparator and not a loop. Use if (pr.readLine() != null) {//your code }

  • I have tried, but it does not return any value. Just "These are the formatted dates: The total number of lines is: 0 The total number of Customerid is: 0 Different existing countries[] ;"

  • When I use all the code with the same class, without instantiating, it works correctly. I can’t understand where my error is :/

  • Is there a method that returns the size of some text? If you can put pr.linhas the size.

  • Console says something?

  • You use the values of each line by accessing pr.line but that value is never affected. It seems to me that what you want to do is while (true){ pr.line = pr.BufferedReader.readline(); if (pr.line == null) break;}

Show 5 more comments

1 answer

0


In your class Main you have the code below, which always returns null

String readLine() {
        return null;

    }

This way the loop will never be accessed while(pr.readLine() != null) will always be null.

To work, instead of using this readLine null, use the method of BufferedReader.

Another thing, your pr.line never initialized, it needs to receive the line, modifying its loop for the example below will solve.

while((pr.line = pr.BufferedReader.readLine()) != null) {

Browser other questions tagged

You are not signed in. Login or sign up in order to post.