I read fields from a .csv file separated with ";" semicolons. And I wrote exeptions to handle the possible deviations. But if there is an exception, the NetBeans read out the error message before the last line.
This is how the output look like:
I don't understand how is possible that the later line in the code can print out prevously. This is my whole code:
public static void reader(String fileName) throws IOException {
try (BufferedReader br = new BufferedReader(new FileReader(fileName))) {
String line;
List<String> lineHolder = new ArrayList<>();
StringTokenizer divider;
String formatter = "%2s|%-30s|%-30s|%10s|%n";
String separator = "----------";
System.out.format("ID|%-30s|%-30s|birth date|%n", "name", "email");
System.out.print("--+" + separator + separator + separator
+ "+" + separator + separator + separator + "+"
+ separator + "+\n");
while ((line = br.readLine()) != null){
if (line.startsWith("#", 0))
continue;
if (!line.contains(";")) {
throw new IOException("too less data or not proper delimiter");
}
divider = new StringTokenizer(line, ";");
lineHolder = arrayFiller(divider);
dataChecker(lineHolder, line);
System.out.format(formatter, lineHolder.get(0), lineHolder.get(1)
, lineHolder.get(2), lineHolder.get(3));
}
} catch (FileNotFoundException ex) {
System.err.println("The file not found.");
} catch (IOException ex){
System.err.println(ex.getMessage());
}
System.out.print("\n");
}
public static ArrayList<String> arrayFiller(StringTokenizer divider) {
ArrayList<String> lineHolder = new ArrayList<>();
while (divider.hasMoreTokens()) {
lineHolder.add(divider.nextToken());
}
return lineHolder;
}
These are the exceptions:
public static void dataChecker(List<String> lineHolder, String line) throws IOException {
if (lineHolder.size() < 4) {
throw new IOException("too less data or not proper delimiter");
} else if (lineHolder.size() > 4) {
throw new IOException("too much data");
} else if (lineHolder.get(0).length() > 2
|| !Pattern.matches("[0-9]+", lineHolder.get(0))) {
throw new IOException("Error during reading the file: "
+ "not proper ID field format");
} else if (lineHolder.get(1).length() > 30
|| !Pattern.matches("[a-zA-ZíÍöÖüÜóÓőŐúÚűŰáÁéÉ. ]+", lineHolder.get(1))) {
throw new IOException("Error during reading the file: "
+ "not proper Name field format");
} else if (lineHolder.get(2).length() > 30
|| !Pattern.matches("[a-zA-Z0-9#. ]+", lineHolder.get(2))) {
throw new IOException("Error during reading the file: "
+ "not proper Email field format");
} else if (lineHolder.get(3).length() > 10 || dateFormatter(lineHolder.get(3))) {
throw new IOException("Error during reading the file: "
+ "not proper Birth date field format");
}
}
public static boolean dateFormatter(String datum) {
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd");
try {
LocalDate changedDate = LocalDate.parse(datum, dtf);
return false;
} catch (DateTimeParseException ex) {
return true;
}
}
And the source file:
#ID;name;email;birth date
1,Jakob George,gipszjakab#gmail.com,1981-11-23
2;Susan Smith;usa#gmail.com;1982-12-01
3;Poul Drake;gameover#gmail.com;1990-01-02
4;Isaac Wheather;ruck%sack#freemail.hu;1988-01-22
5;George T. Benson;bigman#hotmail.com;1977-08-12
I tried to put the method (holder of exceptions) into the reader() method but the reasult the same. How is this possible and what I did wrong?
Error messages are printed through a different output stream. The standard output stream stdout is for normal logging/output, errors (via System.err.println) go to stderr. Your console/terminal shows both, but they won't wait for each other to finish printing stuff.
EDIT: Maybe this helps, too.
EDIT2: If you change the error out to print to a file instead, you will lose error output in the console/terminal. But maybe that's OK for you? Like this:
//set err out to print to file
PrintStream ps = new PrintStream("err.log");
System.setErr(ps);
//cause exception for testing it
String s = null;
s.length();
If you want to have both errors and standard output printed to the console/terminal, there's no way to control the timing of both streams, as each printed line is an independent operation.
String separator = "----------";
System.out.format("ID|%-30s|%-30s|birth date|%n", "name", "email");
System.out.print("--+" + separator + separator + separator
+ "+" + separator + separator + separator + "+"
+ separator + "+\n");
System.out.flush();
while ((line = br.readLine()) != null)
As System.out and System.err are from different output streams they wont wait for each other to complete . Clearing buffer memory right after the print statement would work it out.
Related
I created a list in a FlightBookingSystem Java Class, as you can see below:
public List<Flight> getFlights() {
List<Flight> out = new ArrayList<>(flights.values());
return Collections.unmodifiableList(out);
}
Which I imported from a text file show below:
1::LX2500::Birmingham::Munich::2020-11-25::
2::LX2500::Denmark::London::2021-07-01::
3::LY2380::London::France::2021-06-28::
It's a basic text file which holds the information for each flight
Here is the code I wish to adjust:
public Flight execute(FlightBookingSystem flightBookingSystem, int id)
throws FlightBookingSystemException {
List<Flight> flights = flightBookingSystem.getFlights();
for (Flight Flight : flights) {
if (Flight.getFlightNumber() == flightNumber) {
System.out.println(Flight.getFlightNumber() + " flight(s)");
return flights.get(id);
}
System.out.println(((Flight) flights).getFlightNumber() + " flight(s)");
}
return flights.get(id);
}
How do I change that code so that it allows the user to retrieve one single record from the text file?
Why not to retrieve all and get the one you want by key or id using HashMap ?
If you still want the other option, you can read the text file line by line, and check if it startsWith(...) and the to retrieve this line.
Code example:
try (BufferedReader br = new BufferedReader(new FileReader(file)))
{
String line;
while ((line = br.readLine()) != null)
{
// Add here 'if' condition and parse your line
}
}
Your question is a bit confusing. Your title states:
How do you allow a user to retrieve values from a list in Java?
and the very last line of your post states:
How do I change that code so that it allows the user to retrieve
one single record from the text file?
Which is it, from a List or from a text file?
If it's from a List because you already have the mechanism available then is could be something similar to this:
public String getFlightInfo(String flightNumber) {
List<Flight> flights = FlightBookingSystem.getFlights();
for (Flight flite : flights) {
if(flite.getFlightNumber().equalsIgnoreCase(flightNumber)){
return flite.toString();
}
}
JOptionPane.showMessageDialog(null, "<html>Flight number <font color=red><b>"
+ flightNumber + "</b></font> could not be found!</html>", "Flight Not "
+ "Found", JOptionPane.INFORMATION_MESSAGE);
return null;
}
The code above assumes you have an overriden toString() method applied to the Flight class. If you don't then create one.
If it's actually from file then it could be something like this:
public String getFlightInfo(String flightNumber) {
// 'Try With Resouces' to auto-close reader.
try (BufferedReader reader = new BufferedReader(new FileReader("Flights.txt"))) {
String fileLine = "";
while ((fileLine = reader.readLine()) != null) {
fileLine = fileLine.trim();
// If by chance the file line read in is blank then skip it.
if (fileLine.isEmpty()) {
continue;
}
// First, remove the double colons at the end of line (if any).
if (fileLine.endsWith("::")) {
fileLine = fileLine.substring(0, fileLine.lastIndexOf("::")).trim();
}
/* Split each read in file line based on a double colon delimiter.
The "\\s*" within the regex for the split method handles any
cases where the might be one or more whitespaces before or after
the double-colon delimiter. */
String[] lineParts = fileLine.split("\\s*\\:\\:\\s*");
if(lineParts[1].equalsIgnoreCase(flightNumber)){
// At this point you could just return the line, for example:
// return fileLine;
// or you can return a string with a little more structure:
return new StringBuilder("Flight ID: ").append(lineParts[0])
.append(", Flight #: ").append(lineParts[1]).append(", From: ")
.append(lineParts[2]).append(", To: ").append(lineParts[3])
.append(", Date: ").append(lineParts[4]).toString();
}
}
}
catch (FileNotFoundException ex) {
JOptionPane.showMessageDialog(null, ex.getMessage());
}
catch (IOException ex) {
JOptionPane.showMessageDialog(null, ex.getMessage());
}
JOptionPane.showMessageDialog(null, "<html>Flight number <font color=red><b>"
+ flightNumber + "</b></font> could not be found!</html>", "Flight Not "
+ "Found", JOptionPane.INFORMATION_MESSAGE);
return null;
}
I have a text file named "message.txt" which is read using Buffer Reader. Each line of the text file contains both "word" and "meaning" as given in this example:
"PS:Primary school"
where PS - word, Primary school - meaning
When the file is being read, each line is tokenized to "word" and "meaning" from ":".
If the "meaning" is equal to the given input string called "f_msg3", "f_msg3" is displayed on the text view called "txtView". Otherwise, it displays "f_msg" on the text view.
But the "if condition" is not working properly in this code. For example if "f_msg3" is equal to "Primary school", the output on the text view must be "Primary school". But it gives the output as "f_msg" but not "f_msg3". ("f_msg3" does not contain any unnecessary strings.)
Can someone explain where I have gone wrong?
try {
BufferedReader file = new BufferedReader(new InputStreamReader(getAssets().open("message.txt")));
String line = "";
while ((line = file.readLine()) != null) {
try {
/*separate the line into two strings at the ":" */
StringTokenizer tokens = new StringTokenizer(line, ":");
String word = tokens.nextToken();
String meaning = tokens.nextToken();
/*compare the given input with the meaning of the read line */
if(meaning.equalsIgnoreCase(f_msg3)) {
txtView.setText(f_msg3);
} else {
txtView.setText(f_msg);
}
} catch (Exception e) {
txtView.setText("Cannot break");
}
}
} catch (IOException e) {
txtView.setText("File not found");
}
Try this
............
meaning = meaning.replaceAll("\\s+", " ");
/*compare the given input with the meaning of the read line */
if(meaning.equalsIgnoreCase(f_msg3)) {
txtView.setText(f_msg3);
} else {
txtView.setText(f_msg);
}
............
Otherwise comment the else part, then it will work.
I don't see any obvious error in your code, maybe it is just a matter
of cleaning the string (i.e. removing heading and trailing spaces, newlines and so on) before comparing it.
Try trimming meaning, e.g. like this :
...
String meaning = tokens.nextToken();
if(meaning != null) {
meaning = meaning.trim();
}
if(f_msg3.equalsIgnoreCase(meaning)) {
txtView.setText(f_msg3);
} else {
txtView.setText(f_msg);
}
...
A StringTokenizer takes care of numbers (the cause for your error) and other "tokens" - so might be considered to invoke too much complexity.
String[] pair = line.split("\\s*\\:\\s*", 2);
if (pair.length == 2) {
String word = pair[0];
String meaning = pair[1];
...
}
This splits the line into at most 2 parts (second optional parameter) using a regular expression. \s* stands for any whitespace: tabs and spaces.
You could also load all in a Properties. In a properties file the format key=value is convention, but also key:value is allowed. However then some escaping might be needed.
ArrayList vals = new ArrayList();
String jmeno = "Adam";
vals.add("Honza");
vals.add("Petr");
vals.add("Jan");
if(!(vals.contains(jmeno))){
vals.add(jmeno);
}else{
System.out.println("Adam je už v seznamu");
}
for (String jmena : vals){
System.out.println(jmena);
}
try (BufferedReader br = new BufferedReader(new FileReader("dokument.txt")))
{
String aktualni = br.readLine();
int pocetPruchodu = 0;
while (aktualni != null)
{
String[] znak = aktualni.split(";");
System.out.println(znak[pocetPruchodu] + " " +znak[pocetPruchodu + 1]);
aktualni = br.readLine();
}
br.close();
}
catch (IOException e)
{
System.out.println("Nezdařilo se");
}
try (BufferedWriter bw = new BufferedWriter(new FileWriter("dokument2.txt")))
{
int pocetpr = 0;
while (pocetpr < vals.size())
{
bw.write(vals.get(pocetpr));
bw.append(" ");
pocetpr++;
}
bw.close();
}
catch (IOException e)
{
System.out.println("Nezdařilo se");
}
Good day! I use such part of code
File file = new File(someFilePath);
Scanner sc;
try {
sc = new Scanner(file);
} catch (FileNotFoundException e) {
return "";
}
sc.useDelimiter("\\Z");
System.out.println("file : " + file.getName() + " " + sc.hasNext() + " " + sc.delimiter());
String fileString = sc.next();
I get error Exception in thread "main" java.util.NoSuchElementException at last line of this piece of code.
And the output is file : 758279215_profile.txt false \Z, so the delimiter is correct, file exists (and it's not empty, I've checked it), but it has no next element for some reason (and as I think next element should be and it should be the whole text in the file). What's wrong and how to fix it? Thank you!
ADDED:
BufferedReader br = new BufferedReader(new FileReader(file));
String line = "";
while (line != null) {
try {
line = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(line);
}
returns content of a file (text file with content edited as JSON text) and null (the last itteration of the loop)
it could be locale issue.
try export LC_ALL=en_US.utf-8
Here is a method:
private void writeToFile() {
try {
String time = new SimpleDateFormat("yyyyMMdd_HHmmss").format(Calendar.getInstance().getTime());
String name = "dictionaryFile" + time + ".txt";
File dictionaryFile = new File(name);
BufferedWriter writer = new BufferedWriter(new FileWriter(dictionaryFile));
Iterator<String> it = dictionary.keySet().iterator();
while (it.hasNext()){
String line = it.next();
String entryLine = line + " -> " + dictionary.get(line);
writer.write(entryLine);
writer.close();
}
} catch(Exception e)
{
e.printStackTrace();
}
}
And here is error:
java.io.IOException: Stream closed
at java.io.BufferedWriter.ensureOpen(Unknown Source)
at java.io.BufferedWriter.write(Unknown Source)
at java.io.Writer.write(Unknown Source)
at WordQuizz.WordCollection.writeToFile(WordCollection.java:58)
at WordQuizz.WordCollection.actionPerformed(WordCollection.java:44)
Can anyone help me to solve this issue? if I just try to sysout print entryLine then there is no error. May be I need to specify file location or something like this??
The problem is that you have writer.close(); inside the while loop. Once it's closed on the first iteration, nothing else can be written, and the exception you saw is thrown.
Place the call to close after the while loop. If you are using Java 7+, then use the "try-with-resources" syntax to have it closed when the try ends.
Why are you closing your stream right in the middle of your while loop? Rather:
while (it.hasNext()){
String line = it.next();
String entryLine = line + " -> " + dictionary.get(line);
writer.write(entryLine);
}
writer.close();
Do not close the writer in middle of while loop. Change your code as below.
private void writeToFile() {
BufferedWriter writer;
try {
String time = new SimpleDateFormat("yyyyMMdd_HHmmss").format(Calendar.getInstance().getTime());
String name = "dictionaryFile" + time + ".txt";
File dictionaryFile = new File(name);
writer = new BufferedWriter(new FileWriter(dictionaryFile));
Iterator<String> it = dictionary.keySet().iterator();
while (it.hasNext()) {
String line = it.next();
String entryLine = line + " -> " + dictionary.get(line);
writer.write(entryLine);
//writer.close();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (writer != null) {
writer.close();
}
}
}
Always close the IO operation in finally block, and check if instance is not null.
I am creating an XML file from my Database and storing it in Internal storage. I require data from XML file into a single string. For which, i am using the following method.
BufferedReader br;
try {
br = new BufferedReader(new FileReader(new File(pathDAR)));
String line;
StringBuilder sb = new StringBuilder();
while ((line = br.readLine()) != null) {
sb.append(line.trim());
String temp = sb.toString().substring(38);
Log.v("XML TO String", "" + temp);
Log.v("Lengths : ", "" + temp.length() + " " + sb.length());
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
I have been getting string in the Log, but it seems to be stopping abruptly in the middle.
For e.g. I am supposed to get records string like this. Beginning and ending with database tag.
<database name="DAR.db"><table name="DARWorkDetails"><row><col name="id">1</col><col name="date">05-28-2013</col><col name="visited_city_ID">1264</col><col name="employee_ID">107</col><col name="work_type_ID">1</col><col name="name">null</col><col name="customer_Id">null</col><col name="customer_type_ID">null</col><col name="sub_customer_id">null</col><col name="reason_ID">14</col><col name="reason">ABM SM MEETING</col><col name="remarks">gfhii</col><col name="work_with">211,162</col><col name="IsCustomer">N</col><col name="created_by">107</col><col name="position_id">72</col><col name="designation_Id">3</col><col name="submit_date">05-28-2013</col><col name="IsFinal">null</col></row></table></database>
Instead i have been getting string like this :
<database name="DAR.db"><table name="DARWorkDetails"><row><col name="id">1</col><col name="date">05-28-2013</col><col name="visited_city_ID">1264</col><col name="employee_ID">107</col><col name="work_type_ID">1</col><col name="name">null</col><col name="customer_Id">null</col><col name="customer_type_ID">null</col><col name="sub_customer_id">null</col><col name="reason_ID">14</col><col name="reason">ABM SM MEETING</col><col name="remarks">gfhii</col><col name="work_with">211,162</col><col name="IsCustomer">N</col><col name="created_by">107</col><col name="position_id">72</col><col name="designation_Id">3</col><col name="submit_date">05-28-2013</col><col name="IsFinal">null</co
The String is stopping in the middle. For the sake of example i have only put small example string above. In reality my database has multiple records and i have counted length of the string to around 15640, before abrupt end of the string.
Are there any limitations with StringBuilder in regards to storing characters? I suppose there is memory issue since i have been able to get string fully for records fewer than 10. Problem seems to be arising when records go into upwards of 10. Any help in understanding of solving this issue would be much appreciated.
Please check
It may happen your output is perfect but your Log cat is not displaying it whole.
Log.v("XML TO String", "" + temp);
Log.v("Lengths : ", "" + temp.length() + " " + sb.length());
See reference
I created this class to read strings from a xml file saved in internal storage device, it returns a list , if you want the whole extended string you only need concatenate to link together, if doesn't found the file return an empty list this is all you need to read XML files and parse to Strings, I hope to help!
public class readXMLFile {
private String filePath = "FileStorage";
private String fileName = "File.xml";
private final String tag = "Internal Read Persistence";
File internalFileData;
public readXMLFile() {// default constructor
}
public File getXMLFile(Context context){
File directory = null;
ContextWrapper cw = new ContextWrapper(context);
directory = cw.getDir(filePath, Context.MODE_PRIVATE);
internalFileData = new File(directory, fileName);
if(internalFileData.exists()){
Log.i("ReadXMLFile","File returned");
return internalFileData;
}
else{
Log.i(tag,"the file doesn't exists!");
return null;
}
}
public List<String> readFile(Context context) {
List<String> l = new LinkedList<String>();
try {
File directory = null;
ContextWrapper cw = new ContextWrapper(context);
directory = cw.getDir(filePath, Context.MODE_PRIVATE);
internalFileData = new File(directory, fileName);
if (internalFileData.exists()) {
Log.i("Internal Data", "the root exists!!");
try {
FileInputStream fis = new FileInputStream(internalFileData);
DataInputStream in = new DataInputStream(fis);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String line;
while ((line = br.readLine()) != null) {
l.add(line);
}
try {
if (in != null) {
in.close();
}
} catch (Exception e) {
Log.i(tag, "Exception closing persistence connection");
}
} catch (Exception e) {
Log.wtf("Fatal Exception", "Exception: " + e.getMessage());
}
} else {
Log.i(tag, "File doesn't exists");
return l;//return empty list
}
} catch (Exception e) {
Log.wtf(tag, "Exception DATA READING: " + e.getMessage());
return l;
}
Log.i(tag, "file found return");
return l;
}
}