Reading from a CSV file into an array - java

I am making a program that has an array of people. The people have different amounts of info for instance they all have a first name, last name, type, gender. The first issue I have is some also have email and some have a image (using Icon) and some have both of them.
I need to read these pieces of data in and then display them, it is for a college class and the teacher was basically like figure it out.. I have read the API and numerous articles and can't seem to get it to work. Can someone give me a push in the right direction?
I am not looking for you to hand me the answers just a little help.

Read the file line by line and split line with ,.
// you need to create a pojo to hold all the user info.
List<UserObject> users = new ArrayList<UserObject>();
try {
br = new BufferedReader(new FileReader(csvFile));
while ((line = br.readLine()) != null) {
String[] userinfos = line.split(",");
UserObject newUser = new UserObject();
//set the mandatory attributes here
if (userinfos.length > 4) {
// you have the extra fields here.
// set the extra fields to user here
}
users.add(newUser);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
One problem with this is first name or last name might have commas with in them. I suggest you to use any third party csv parser like Open Csv.

Related

Turning a string in from a text file into a variable identifier

Right, so I've seen a lot of these questions phrased in a similar way but none of these are specifically to what I need. I have a text file like so:
~Jerry
10/patio/*
11/bathroom/*
23/home/*
~Jeff
28/patio/*
84/bathroom/*
33/home/*
I can read these fine. What I'm trying to accomplish is to take the lines that start with '~' change them into variable names and supply them with all lines below it that don't start with '~'.
Therefore, I'd like to be able to supply the .txt, take the name Jerry and instantiate it as:
try(BufferedReader br = new BufferedReader(new FileReader(filepath))) {
for(String line; (line = br.readLine()) != null; )
{
if(line.charAt(0) == '~'){
line.replace("~","");
int[] line = new int[];
}
else{
//add the line to the Jerry array
}
}
}
catch(Exception e){
System.out.println(e);
}
Is there a capability for using stored string to declare new variables? or at least provide the illusion of declaring a new variable so that it can be used with other methods? Or do I need to hard code the declaration in and use a case statement or the like?

How To Read A Specific Part Of A Line In A Text File In Java?

I have a text file in which I have written some information line by line like this:
name|Number|amount|PIN
How can I read back data In a way that (for example) I will be able to use just the "name" part in a method?
The sample code is shown in the image below.
in the beginning declare a List to collect the accounts:
import java.util.ArrayList;
...
public Account[] inReader() { //BTW: why do you pass an Account[] here?
ArrayList accountList = new ArrayList();
...
}
replace the for(String records : dataRecords) {...} with
String name = dataRecords[0];
String cardNumber = dataRecords[1];
int pin = Integer.parseInt(dataRecords[2]); //to convert the String back to int
double balance = Double.parseDouble(dataRecords[3]);
Account account = new Account(name, cardNumber, pin, balance);
accountList.add(account);
because you already proceed record by record (while ((line = br.readLine())!=null) {...})
in the end return accountList.toArray(new Account[0]);
You can read the text line by line and then use the "|" delimiter to separate the columns.
try (Stream<String> stream = Files.lines(Paths.get(fileName))) {
stream.forEach(System.out::println);
}
You could read the file line-by-line and split on the delimiter '|'.
The following example assumes the filepath is in args[0] and would read then output the name component of the input:
public static void main(String[] args) {
File file = new File(args[0]);
BufferedReader br = new BufferedReader(new FileReader(file));
while(String line = br.readLine()) != null) {
String[] details = line.split("|");
System.out.println(details[0]);
}
}
As mentioned in the comment above, you can simply split the line on your delimiter, |, and go from there.
Something like:
public class Account {
// ...
public static Account parseLine(String line) {
String[] split = line.split("|");
return new Account(split[0], split[1], split[2], split[3]);
}
}
should work fine (assuming you have a constructor which takes the four things you're putting in). If your Account class has more information than this, you can create an AccountView or similarly named class which does only contain the details you have available here. With this, just iterate line by line, parse your lines to one of these Objects, and use it's properties (including the already available getters) when calling other methods which need name, etc.
First, you need to read the whole content of the file or line by line.
Then, for each line you need to create a function to split the line text by a configurable delimiter. This function can receive the column number and it should return the needed value. For example: extractData(line, 0) should return 'name', extractData(line, 2) should return 'amount' etc.
Also, you need some validation: what if there are only 3 columns and you expect 4? You can throw and exception or you can return null/empty.
There are many possible ways to do it. One of them is to make an object that will hold the data. Example since you know that your data will always have name, number, amount and pin then you can make a class like this:
public class MyData {
private String name;
private String number;
private double amount;
private String pin;
// Add getters and setters below
}
Then while reading the text file you can make a list of MyData and add each data. You can do it like this:
try {
BufferedReader reader = new BufferedReader(new FileReader("path\file.txt"));
String line = reader.readLine();
ArrayList<MyData> myDataList = new ArrayList<MyData>();
while (line != null) {
String[] dataParts = line.split("|"); // since your delimiter is "|"
MyData myData = new MyData();
myData.setName(dataParts[0]);
myData.setNumber(dataParts[1]);
myData.setAmount(Double.parseDouble(dataParts[2]));
myData.setPin(dataParts[3]);
myDataList.add(myData);
// read next line
line = reader.readLine();
} catch (IOException e) {
e.printStackTrace();
}
Then you can use the data like this:
myDataList.get(0).getName(); // if you want to get the name of line 1
myDataList.get(1).getPin(); // if you want to get the pin of line 2
You can convert the file into a csv file and use a library specific for reading csv files, e.g. OpenCSV. This will give you more flexibility in handling the data in the file.

Java, how to extract some text from a large file and import it into a smaller file

I'm relatively new to Java programming and am trying to create an application which will help some colleagues.
The background of what I am trying to do is, read the content of a large file, upto and possibly more than 400,000 lines, which contains XML but is not an valid XML document, as its kind of a log.
What I am trying to do, is build an application where a user enters a unique ID, this then scans the document to find if it exists, if it does, and often the unique ID occurs a few times in the produced XML, then I want to traverse backwards to a node ID of <documentRequestMessage> , then copy everything from that node to its closing node, and put that into it's own document.
I know how to create the new document, but am struggling to find out how to essentially 'find backwards' and copy everything to the closing tag, any help greatly appreciated.
EDIT
Unfortunately, I haven't been able to figure out how to implement of either of the 3 suggestions thus far.
The correlationId is the unique reference previously mentioned.
The current code I have, which works and outputs the findings to the console, is
String correlationId = correlationID.getText();
BufferedReader bf = new BufferedReader(new FileReader(f));
System.out.println("Looking for " + correlationId);
int lineCount = 0;
String line;
while ((line = bf.readLine()) != null) {
lineCount++;
int indexFound = line.indexOf(correlationId);
if (indexFound > -1) {
System.out.println("Found CorrelationID on line " + "\t" + lineCount + "\t" + line);
}
}
bf.close();
Any further help greatfully appreciated, I'm not asking for someone to write it for me, just some really clear and basic instructions :) please
EDIT 2
A copy of the file I'm trying to read and extract from can be found here
While you are reading forward through the file looking for your unique ID, keep a reference to the most recent documentRequestMessage that you encounter. When you find the unique ID, you'll already have the reference that you need to extract the message.
In this context, "reference" can mean a couple of things. Since you are not traversing a DOM (because it's not valid XML) you will probably just store the position in the file where the documentRequestMessage is. If you're using a FileInputStream (or any InputStream where mark is supported), you can just mark/reset to store and return to the place in the file where your message starts.
Here is an implementation of what I believe you are looking for. It makes a lot of assumptions based on the log file that you linked, but it works for the sample file:
private static void processMessages(File file, String correlationId)
{
BufferedReader reader = null;
try {
boolean capture = false;
StringBuilder buffer = new StringBuilder();
String lastDRM = null;
String line;
reader = new BufferedReader(new FileReader(file));
while ((line = reader.readLine()) != null) {
String trimmed = line.trim();
// Blank lines are boring
if (trimmed.length() == 0) {
continue;
}
// We only actively look for lines that start with an open
// bracket (after trimming)
if (trimmed.startsWith("[")) {
// Do some house keeping - if we have data in our buffer, we
// should check it to see if we are interested in it
if (buffer.length() > 0) {
String message = buffer.toString();
// Something to note here... at this point you could
// create a legitimate DOM Document from 'message' if
// you wanted to
if (message.contains("documentRequestMessage")) {
// If the message contains 'documentRequestMessage'
// then we save it for later reference
lastDRM = message;
} else if (message.contains(correlationId)) {
// If the message contains the correlationId we are
// after, then print out the last message with the
// documentRequestMessage that we found, or an error
// if we never saw one.
if (lastDRM == null) {
System.out.println(
"No documentRequestMessage found");
} else {
System.out.println(lastDRM);
}
// In either case, we're done here
break;
}
buffer.setLength(0);
capture = false;
}
// Based on the log file, the only interesting messages are
// the ones that are DEBUG
if (trimmed.contains("DEBUG")) {
// Some of the debug messages have the XML declaration
// on the same line, and some the line after, so let's
// figure out which is which...
if (trimmed.endsWith("?>")) {
buffer.append(
trimmed.substring(
trimmed.indexOf("<?")));
buffer.append("\n");
capture = true;
} else if (trimmed.endsWith("Message:")) {
capture = true;
} else {
System.err.println("Can't handle line: " + trimmed);
}
}
} else {
if (capture) {
buffer.append(line).append("\n");
}
}
}
} catch (IOException ex) {
ex.printStackTrace(System.err);
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException ex) {
/* Ignore */
}
}
}
}
What you can do is read the contents of the file and look for <documentRequestMessage> element. When you find one of the above elements, read till you find </documentRequestMessage> and store it in a list so all the documentRequestMessage will be available in a list.
You can iterate through this list at the end or while adding to list to find the unique id you're looking for. If you find it write to XML Files or ignore.
I'm assuming your log is a series of <documentRequestMessage> contents.
Don't scan the log at all.
Read the log, and each time you encounter a <documentRequestMessage> header, start saving the contents of that <documentRequestMessage> block into a block area.
I'm not sure if you have to parse the XML or you can just save it as a List of Strings.
When you encounter a </documentRequestMessage> trailer, check to see if the ID of the block matches the ID you're looking for,
If the ID matches, write the <documentRequestMessage> block to an output file. If the ID doesn't match, clear the block area and read to the next <documentRequestMessage> header.
This way, there's no backtracking in your file reading.

Java - Use Scanner to read big text files

I have a very big text file with customer information. I would like to read all the customer information from the text file.
This is how my text file is organized:
Costomer 1:
Name:
Erik Andersson
Adress:
Street1
Phone number:
085610540
Costomer 2:
Name:
Lars Larsson
Adress:
Street1
Phone number:
085610540
I would like to be able read all the customer information. Is there any good way to it with? I have read about Scanner and Pattern and was wondering if it is good idea to use them in this case? My text file is very big and contains hundreds of customers.
Dose any one have any idea how I could read all the information from the text file? I have created a class with customer variabled, I only need help with the reading from the text file. I want to read the information in an organized way.
All help is very very appreciated.
Like so:
public void getEmployees(File f) throws Exception {
// An ArrayList of your Employee-Object to hold multiple Employees
ArrayList<Employee> employees = new ArrayList<Employee>();
// The reader to read from your File
BufferedReader in = new BufferedReader(new FileReader(f.getAbsolutePath()));
// This will later contain one single line from your file
String line = "";
// Temporary fields for the constructor of your Employee-class
int number;
String name;
String adress;
String phone;
// Read the File untill the end is reached (when "readLine()" returns "null")
// the "line"-String contains one single line from your file.
while ( (line = in.readLine()) != null ) {
// See if your Line contains the Customers ID:
if (line.startsWith("Customer")) {
// Parse the number to an "int" because the read value
// is a String.
number = Integer.parseInt(s.substring("Customer ".length()).substring(0,s.indexOf(':')));
} else if (line.startsWith("Adress:")) {
// The Adress is noted in the next line, so we
// read the next line:
adress = in.readLine();
} else if (line.startsWith("Phone number:")) {
// Same as the Adress:
phone = in.readLine();
} else if (line.startsWith("Name:")){
// Same as the Adress:
name = in.readLine();
} else if ( line.equals("") ){
// The empty line marks the end of one set of Data
// Now we can create your Employee-Object with the
// read values:
employees.add(new Employee(number,name,adress,phone));
}
}
// After we processed the whole file, we return the Employee-Array
Employee[] emplyeeArray = (Employee[])employees.toArray();
}
Please give +1 and correct for ur hw lol
As a little extension to stas answer:
The originally posted code doesn't work, because a continue skips the current loop-iteration. So unless the line starts with "", nothing is ever done.

Java string comparisions/quotations

My problem is the comparision of two objects and the strings that they return (accessed through getters).
Object one parses a csv file for dates and when printed out through exampleObject.getDateTime() returns the string: "2010-03-26-10-54-06.471000"
Object two has its dateTime string set by the user. When I set the dateTime on object two to be the same as objectOne and then do exampleObjectTwo.getDateTime() it returns 2010-03-26-10-54-06.471000
So the main difference is that one string has quotations from parsing the csv (which contains no quotations) and the user set string when returned has no quotations!
If anyone can offer an explanation as to why this is happening I'd be very grateful!
Many thanks!
BufferedReader input = new BufferedReader(new FileReader(file));
try {
String line = null;
while ((line = input.readLine()) != null) {
SearchResult searchResult = new SearchResult();
if (!line.contains("Date")) {
String[] split = line.split(",");
SearchResult.setDateTime(split[0]);
SearchResults.add(SearchResult);
}
}
} finally {
input.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
edit above is the code that was using to parse the csv file. I checked and the csv file does not contain any quotations.
Thanks for the quick and helpful response!
You need to modify/configure the CSV parser to remove the quotes.
If it's a homegrown CSV parser, doing so should suffice to get rid of the surrounding doublequotes:
field = field.replaceAll("^\"|\"$", "");
If it's a 3rd party API, then you need to consult its documentation (or mention the library name here so that one who is willing to do that can lookup the documentation for you).
See also:
How to parse CSV in Java?
check again how do you perform csv parsing.
or remove quotations from string:
String newDate = oldString.replaceAll("\""," ").trim();

Categories