Hi, I'm working on a java program that reads two files, stores the data in two TreeMaps and the I will compare the keys and values of both TreeMaps and perform some operations. The problem I'm having is there's no output file when I try to save my data??? On my computer screen I can see the keys and values of both TreeMap, what's wrong with my code?
Any help is deeply appreciated. Thank you in advance.
P.S. I'm not a programmer but a student
import java.util.*;
import java.io.*;
import java.lang.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.io.FileNotFoundException;
public class MyDataScanner2a {
private static void readFiles(String fileName1, String fileName2) {
try {
Scanner scanner = new Scanner(new File(fileName1));
Scanner scanner2 = new Scanner(new File(fileName2));
scanner.useDelimiter(System.getProperty("line.sepa rator"));
scanner2.useDelimiter(System.getProperty("line.sep arator"));
while (scanner.hasNext()) {
createTreeMaps(scanner.next(), 1);
}
while (scanner2.hasNext()) {
createTreeMaps(scanner2.next(), 2);
}
scanner.close();
scanner2.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void createTreeMaps(String line, int i) {
TreeMap<String, Double> treeMap = new TreeMap<String,Double>(); //a
TreeMap<String, Double> treeMap2 = new TreeMap<String,Double>(); //a
if (i==1) {
//System.out.println("line: "+line);
Scanner lineScanner = new Scanner(line);
lineScanner.useDelimiter("\\t");
String div = lineScanner.next();
String eeN = lineScanner.next();
double hrs = lineScanner.nextDouble();
System.out.println("hours worked: \t\t"+hrs);
//TreeMap<String, Double> treeMap = new TreeMap<String,Double>();
if (!treeMap.containsKey(eeN)) {
treeMap.put(div+"\t"+eeN, hrs);
}
/*for (Map.Entry<String, Double> entry : treeMap.entrySet()) {
System.out.println("Key: " + entry.getKey() + "\tValue: " + entry.getValue());
}*/
} else {
//System.out.println("line: "+line);
Scanner lineScanner = new Scanner(line);
lineScanner.useDelimiter("\\t\\$");
String str = lineScanner.next();
double deds = lineScanner.nextDouble();
System.out.println("deduction: \t\t"+deds);
//TreeMap<String, Double> treeMap2 = new TreeMap<String,Double>();
if (!treeMap2.containsKey(str)) {
treeMap2.put(str, deds);
}
/*for (Map.Entry<String, Double> entry : treeMap2.entrySet()) {
System.out.println("Key: " + entry.getKey() + "\tValue: " + entry.getValue());
}*/
}
}
BufferedWriter out = null;
String outFile = "C:/Deds.txt";
public void myMethod (TreeMap<String, Double> treeMap, TreeMap<String, Double> treeMap2) {
/*TreeMap<String, Double> hours = new TreeMap<String,Double>();
hours.putAll(treeMap);
for (Map.Entry<String, Double> entry : treeMap.entrySet()) {
//System.out.println("Key: " + entry.getKey() + "\tValue: " + entry.getValue());
}
for (Map.Entry<String, Double> entry : treeMap2.entrySet()) {
//System.out.println("Key: " + entry.getKey() + "\tValue: " + entry.getValue());
}*/
try {
File fout = new File (outFile);
FileOutputStream fos = new FileOutputStream(fout); //Open output stream
out = new BufferedWriter(new OutputStreamWriter(fos));
for (Map.Entry<String, Double> entry : treeMap.entrySet()) {
System.out.println("eeData: " + entry.getKey() + "\tAmount: " + entry.getValue());
out.write(entry.getKey() + "\t" + entry.getValue());
out.newLine();
}
} catch( IOException e ) {
e.printStackTrace();
} finally {
try {
// attempt the close the output file
out.close();
} catch( IOException ex ) {
ex.printStackTrace();
}
}
} // end of my Method
public static void main(String[] args) {
String option = "y";
while (option.equals("y") | option.equals("Y")) {
Console console = System.console();
String inString1 = "C:/console.readLine("\nEnter input file 1: ")+".txt";
String inString2 = "C:/console.readLine("Enter input file 2: ")+".txt";
System.out.println();
readFiles(inString1, inString2);
option = console.readLine("\nContinue (y/n) ? : ");
System.out.println();
}
}
}