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?
– Maury Developer
Yes, the goal would be to read all lines of the txt document. Is it wrong? Can you help me?
– Nayana Ciuro
Readline() read the whole file or per line?
– Maury Developer
Read the whole file
– Nayana Ciuro
Then use a comparator and not a loop. Use
if (pr.readLine() != null) {//your code }
– Maury Developer
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[] ;"
– Nayana Ciuro
When I use all the code with the same class, without instantiating, it works correctly. I can’t understand where my error is :/
– Nayana Ciuro
Is there a method that returns the size of some text? If you can put
pr.linhas
the size.– Maury Developer
Console says something?
– Maury Developer
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 iswhile (true){ pr.line = pr.BufferedReader.readline(); if (pr.line == null) break;}
– Isac