Java comparing csv file values - java

I'm trying to create a csv file where only 1 team name is shown per row, so when you click the button twice it will only add the team name if its not already there. currently it adds the team "UWE" every single time you press the button. the code for this is below:
public void showStats(ActionEvent event){
try {
File matchFile = new File("src/sample/matchData.csv");
File scoreFile = new File("src/sample/scoreData.csv");
Scanner matchReader = new Scanner(matchFile);
Scanner scoreReader = new Scanner(scoreFile);
while (matchReader.hasNextLine()) {
String data = matchReader.nextLine();
List<String> matchList = Arrays.asList(data.split(","));
while (scoreReader.hasNextLine()) {
String dataScore = scoreReader.nextLine();
List<String> dataScoreList = Arrays.asList(dataScore.split(","));
if (dataScoreList.get(0).equals(matchList.get(0))) {
//
} else {
writeExcel("scoreData", matchList.get(0)) ;
}
System.out.println(dataScoreList);
}
System.out.println(matchList);
}
matchReader.close();
scoreReader.close();
} catch (FileNotFoundException e) {
System.out.println("An error occurred.");
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
The csv file "matchData" contains:
UWE,KCC,Jin,Julia,Chris,Ryan,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,5,0
The csv file "scoreData" has one empty line in it

You can first go through your source CSV file and put in a map only the lines that contain unique team key....
while (matchReader.hasNextLine()) {
String data = matchReader.nextLine();
String[] record = data.split(",", 2);
Map<String, String> matchList = new TreeMap<>();
matchList.putIfAbsent(record[0], record[1]); // only unique keys are entered.
}
// TODO write to Excel each entry in the map (you don't need to check for unique keys)
Notice that writing to Excel is done after the map is complete. This is the best approach; or at least better than what you showed in your original post. With this approach, you are letting the data structure simplify your process (and no nested loops).
UPDATE:
I forgot to mention that matchList.putIfAbsent(K, V) works with Java 8 and later. If using Java 7 or older (should upgrade Java ASAP), then you will have to do the following:
String value = matchList.get(record[0]);
if (value == null) {
matchList.put(record[0], record[1]);
}
This is because Map#get(K) returns null is no entry is found OR the map allowed for null values to be entered for a given key. Otherwise, it will return the previous value. The new method introduced in Java 8 does this check automatically.

Related

Move from string to array but after that select by the first character (Record Type 1, 2, 5)

I need your help, I am new in Java
I need to read a flat file with 5 different of records
the way to differentiate each record is the first characters, after that I have the idea to move to an 5 different array to play with with the data inside.
example
120220502Name Last Name1298843984 $1.50
120220501other client 8989899889 $23.89
2Toronto372 Yorkland drive 1 year Ontario
512345678Transfer Stove Pay
522457839Pending Microwave Interactive
any help will quite appreciated
Break the problem into chunks. The first problem is reading the file:
try (BufferedReader reader = new BufferedReader(new FileReader("path/to/file"))) {
parseData(reader); //method to do the work.
} catch (IOException e) {
e.printStackTrace();
}
Then you need to decide what kind of record it is:
public void parseData(BufferedReader input) throws IOException {
for (String line = input.readLine(); line != null; line = input.readLine()) {
if (line.startsWith("1")) {
parseType1(line);
} else if (line.startsWith("2")) {
parseType2(line);
} else if (line.startsWith("5")) {
parseType5(line);
} else {
throw new Exception("Unknown record type: " + line.charAt(0));
}
}
}
Then you'll need to create the various parseTypeX method to handle turning the text into usable chunks and then into classes.
public Type1Record parseType1(String data) {
//create a Type1Record
Type1Record record = new Type1Record();
//split the string something like
String [] fields = data.split("\\s+");
//Assign those chunks to the record
record.setId(fields[0]);
record.setFirstName(fields[1]);
record.setLastName(fields[2]);
record.setTotal(fields[3]); //if you want this to be a real number, you'll need to remove the $
}
Repeat the process with the other record types. You'll likely need to group records together, but that should be easy enough.

How can I parse a file and map input into several subrecords?

I have to start of with saying that I'm a super noob when it comes to Java and programming in general.
My issue:
I have a problem with parsing a textfile and putting the data of the file into subrecords. The key whether to put the data into a new subrecord or not is in my case based on the name of the tag in the file.
Here's an example of a file that I want to parse/split into subrecords.
SHP
DATA 1
DATA 2
DATA 3
ITEM A
DATA B
DATA C
ITEM A
DATA B
DATA C
SHP
So basically when I encounter the first occurrence of SHP I want to create a new SHP Class and then map the following tags (DATA in this example) into fields of the new SHP Object. However if the next tag is ITEM, I then need to create a new ITEM Object in SHP and then map the following DATA tags until a new ITEM tag is found... What makes it worse is that number of SHP tags can also be multiple in the file that I'm parsing.
What I've done so far is to put all the contents of the file into a ArrayList and then I iterate over this list and depending of the value of the current "record" I then create a new Object or map the value of the record in the list.
However I get totally lost with all my loops and I need some help! :)
Is there a good way of doing this? How can I easily fetch chunks of data from an ArrayList and extract and map this into new objects depending on the value of the current record?
I'm thinking that the same questions would appear if you would to create a parser from scratch that maps the data from an XML file and puts this into Objects, right?
Extended version of issue with real examples
Maybe it's easier if I provide some real examples and real data to illustrate my issue. Initially I just thought that it would make it harder to understand but hopefully this will be easier.
The file and the content:
Example file
So what I have done so far is to create some classes that should represent the data in the file. I even names the fields/variables of the class so that it would be easier for me to map.
Here are the classes:
public class REQUEST {
public SHIPMENT[] SHIPMENTS;
}
public class SHIPMENT {
public String IVNO;
public String CNNAME;
public String CNADDRESS1;
public String CNPC;
public String CNCITY;
public PACKAGE[] PACKAGES;
public ITEM[] ITEMS;
}
public class PACKAGE {
public String GOODSLINE;
public String GOODSDESCR;
public String GRWEIGHT;
}
public class ITEM {
public String ITEMLINE;
public String ARTNO;
public String GRWEIGHT;
}
And below is the code that I've done that doesn't work properly. I manage to create multiple SHIPMENT's and also create the ITEM's but for some reason the data isn't mapped. I also before calling this mapMethod put the entire content of the file into an ArrayList. One fiel row per record in the ArrayList.
Ok, so here it is in it's total and please be gentle with me :)
public REQUEST mapRequest(ArrayList<String> inputfile)
throws IllegalArgumentException, IllegalAccessException {
REQUEST request = new REQUEST();
ArrayList<SHIPMENT> shipments = new ArrayList<>();
for (int i = 0; i < inputfile.size(); i++) {
String recordIdentifier = getRecordIdentifier(inputfile.get(i));
if (StringUtils.equals(recordIdentifier, "IVNO")) {
SHIPMENT shipment = new SHIPMENT();
ArrayList<ITEM> items = new ArrayList<>();
int j = 0;
for (j = i; j < inputfile.size(); j++) {
String currShpRecIdentifier = getRecordIdentifier(inputfile.get(j));
String nextShpRecIdentifier = StringUtils.EMPTY;
if (j + 1 < inputfile.size()) {
nextShpRecIdentifier = getRecordIdentifier(inputfile.get(j + 1));
}
try {
Field field = shipment.getClass().getField(currShpRecIdentifier);
String shipmentRecordValue = getRecordValue(inputfile.get(j));
field.set(shipment, shipmentRecordValue);
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
}
if (StringUtils.equals(nextShpRecIdentifier, "ITEMLINE")) {
ITEM item = new ITEM();
int k = 0;
for (k = j; k < inputfile.size(); k++) {
String currItemRecIdentifier = getRecordIdentifier(inputfile.get(k));
String nextItemRecIdentifier = StringUtils.EMPTY;
if (k + 1 < inputfile.size()) {
nextItemRecIdentifier = getRecordIdentifier(inputfile
.get(k + 1));
}
try {
Field field = item.getClass().getField(currItemRecIdentifier);
String itemRecordValue = getRecordValue(inputfile.get(k));
field.set(item, itemRecordValue);
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
}
if (StringUtils.equals(nextItemRecIdentifier, "ITEMLINE")) {
break;
}
}
items.add(item);
j = k;
}
if (StringUtils.equals(nextShpRecIdentifier, "IVNO")) {
break;
}
}
shipment.ITEMS = items.toArray(new ITEM[items.size()]);
shipments.add(shipment);
i = j;
}
}
request.SHIPMENTS = shipments.toArray(new SHIPMENT[shipments.size()]);
return request;
}
private String getRecordIdentifier(String in) {
return StringUtils.left(in, 15).trim();
}
private String getRecordValue(String in) {
return StringUtils.substring(in, 16).trim();
}
I don't really get where you're having trouble with that.
FileReader input = new FileReader(filePathToYourFile);
BufferedReader bufRead = new BufferedReader(input);
String readLine = null;
String shp = "SHP"; //or define them as "constants" with static final
String item = "ITEM";
String data = "DATA";
ArrayList<SHPType> listOfSHPObjects = new ArrayList<SHPType>();
SHPType lastSHP;
ItemType lastItem;
while ( (readLine = bufRead.readLine()) != null)
{
String[] splittedLine = readLine.split(" ");
if(splittedLine[0].equals(shp))
{ //if it's an SHP, make an SHP object, and add it to a list.
lastSHP = new SHPType();
listOfSHPObjects.add(lastshp);
}
if(splittedLine[0].equals(data) && lastSHP != null)
{ //if it's a data object, either add it to the property/field/list/collection of the latest SHP, or if you have found an item, add it to the item.
//of course if you haven't found any SHP objects, discard it, as it doesn't belong anywhere
if(lastItem == null)
lastSHP.CollectionThatHoldsData.add(new DataType());
else
lastItem.CollectionThatHoldsData.add(new DataType());
}
if(splittedLine[0].equals(item) && lastSHP != null)
{
//create a new last item and add it to the item list of the last SHP found. New data will be added to this item
lastItem = new ItemType();
lastSHP.CollectionThatHoldsItems.add(lastItem);
}
}
//close the file etc...
This assumes that if you find the following:
DATA
ITEM
SHP
etc
The first DATA and ITEM are meaningless. If you want to assign them to the first SHP that you have found, store them in a waitingForSHP arrayList, and as soon as you find the first SHP, add them there.
Also, if you want to create an SHP/Item/Data based on the second value (fe in DATA 5, meaning 5), pass the 5 (splittedLine[1] variable in my example) as an argument to the constructor of the SHPType/ItemType/DataType object. It will be a string, so if you want to pass it as an integer, you will have to convert it ( Integer.parseInt(splittedLine[1]) ).

ComboBox adds all the values together as one value

I'm making an application at the moment and now I need to fill a comboBox with all the values that I get from another class in an arrayList type.
This is the code that I use to fill my combobox:
public void setComboBox(){
MessageConsole mc = new MessageConsole(textArea);
//The redirectOut will redirect the text from the System.out.prtnln to the text area.
mc.redirectOut();
List<String> arrayList = new ArrayList<String>();
if(gf.loadCombo("config") != null){
arrayList.addAll(gf.loadCombo("config"));
for (int i = 0; i < arrayList.size(); i++) {
String s = arrayList.get(i);
configName.removeItem(s);
configName.addItem(s);
}
}
}
This is the code from the other class:
public Collection<String> loadCombo(String property) {
//Check if the property file is present in the given directory.
if(cf.checkProperty()==true){
//Check is there are any properties saved in the property file.
if(cf.getProperty() != null){
Properties prop = new Properties();
FileInputStream fis;
try {
fis = new FileInputStream("config.properties");
prop.load(fis);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//Check if the properties match with the entered configuration.
List<String> arrayList = new ArrayList<String>();
arrayList.addAll(cf.getProperty());
Collection<String> propertyList = new ArrayList<String>();
for (int i = 0; i < arrayList.size(); i++) {
String s = arrayList.get(i);
if(s.startsWith(property)){
System.out.println("This value has been added to the arrayList from the other class: "+s);
propertyList.add(cf.getPropertyValue(s));
}
}
return propertyList;
}
//The system prints a message of the missing properties.
else{
System.out.println("You need to save a configuration first.");
}
}
//The system prints a message of the missing property file.
else{
System.out.println("The property file is either missing or could not be found. in de Load class");
}
return null;
}
Following is a screenshot of the result:
As you can see all the values are added as 1 long String"[3, 2, 1]" in the comboBox. Could anyone tell me why this happens?
Thanks in advance,
Lordarnoud
P.S. I hope this is the correct way to ask my question and I hope my question is clear enough for everyone to understand.
At a first look it appears the problem could be one of two things:
The value "[3 2 1]" is returned from the loadCombo method. More specifically from the cf.getProperty(). It is not clear from the context provided what cf is.
The value "[3 2 1]" was added to your combobox at some other point in you code. If this might be case, try using configName.removeAllItems() instead of removing then adding each item in your collection.
Additionally in your loadCombo method you load the config.properties file into a Properties object and then do nothing with it. It seems your intentions were to load the config properties from that file instead of the cf object.

How to speedup multiple searches on an HashTable

I have two files (with almost 5000 lines each) with logs. The files in each line has a set of rules associated too an email, like this:
Y#12#EMAIL_1#RULE_1,RULE_2,RULE_3,RULE_4#time=993470174
Y#12#EMAIL_2#RULE_1,RULE_2,RULE_3,RULE_4#time=993470175
Y#12#EMAIL_3#RULE_1,RULE_2,RULE_3#time=9934701778
I use the following function to read the file, and get the rules for each email:
private void processFile()
{
ArrayList<String[]> lSplitRules = new ArrayList<>();
try {
FileInputStream fileStream = new FileInputStream("log.log");
DataInputStream fileIn = new DataInputStream(fileStream);
BufferedReader fileBr = new BufferedReader(new InputStreamReader(fileIn));
String strLine;
while ((strLine = fileBr.readLine()) != null)
{
String[] lTokens = strLineSpam.split("#");
String lRawRules = lTokens[3];
lSplitRules.add(lRawRules.split(","));
}
} catch (FileNotFoundException e) {
System.out.println("File: log.log, not found. Error: " + e.getMessage());
} catch (IOException e) {
System.out.println("Couldn't open log.log. Error: " + e.getMessage());
}
So far so, good. In each "space" of the ArrayList I'll have an String[] containing the rules for each email. In other hand i have also an HashMap containing one unique list of rules and it's value like this:
RULE_NAME - VALUE
RULE_1 - 0.1
RULE_2 - 0.5
RULE_3 - 0.6
...
I need to compare every rule of every email too see if it exists on the HashMap. If exist returns the value of the rule for some calculations
I use this function for that:
private Double eval (String rule, Map<String, Double> scores)
{
for (Entry<String, Double> entry : scores.entrySet()) {
if (entry.getKey().equalsIgnoreCase(rule))
{
return entry.getValue();
}
}
return 0.0;
}
The problem is that i need to compare every email and it's rules multiple times (more then 10.000), since I'm using a Genetic Algorithm to try to optimize the VALUE of each RULE. Is there anyway to optimize the comparison of the rules of each email through the HASHMAP? Since i need speed, I'm doing 100 verifications in 8 minutes now.
Sorry for my english.
Regards
The whole point of having a hash table is so youc an do a single hash lookup. If you are just going to loop through the keys, you may as well use a List.
I don't know where you are building your scores, but you can normalise the case.
scores.put(key.toLowerCase(), value);
for a case insensive lookup
Double d= scores.get(key.toLowerCase());

how to clear file before putting data there?

I need to save some data into text file. I'm using class Files with its method write().
If such file doesn't exist - everything alright. The problem is if such file already exists it appends new data to the end of the file. And I need to clear it first. The code is:
public static void main(String[] args) {
DepoList test0 = new DepoList();
test0.init();
ArrayList<Depo> list0 = test0.getList();
Collections.sort(list0);
for (Depo depo : list0) {
String str = String.format("sum = %1$8.2f interest = %2$7.2f\n", depo.getSum(), depo.getIncome());
System.out.format(str);
try {
Files.write(Paths.get("depo.txt"), str.getBytes(), StandardOpenOption.CREATE, StandardOpenOption.APPEND);
} catch (IOException e) {
e.printStackTrace();
}
}
System.out.println();
I think I need to add some another StandardOpenOperation. How to clear the file before putting data there?
Remove StandardOpenOption.CREATE,Standardoption.APPEND this just appends your new data to the existing one
Use Files.write((Paths.get("depo.txt"), str.getBytes());

Categories

Resources