Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: What's wrong with my java code?

  1. #1
    Junior Member
    Join Date
    May 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default What's wrong with my java code?

    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();
    }
    }
    }


  2. #2
    Member
    Join Date
    Apr 2012
    Location
    Superior, CO, USA
    Posts
    80
    Thanks
    0
    Thanked 14 Times in 14 Posts

    Default Re: What's wrong with my java code?

    Quote Originally Posted by mjmz View Post
    String inString1 = "C:/console.readLine("\nEnter input file 1: ")+".txt";
    String inString2 = "C:/console.readLine("Enter input file 2: ")+".txt";
    What the heck are you trying to do here?
    Need Java help? Check out the HotJoe Java Help forums!

  3. #3
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: What's wrong with my java code?

    Your literals are being ended prematurely by the ""

    Also, what are you doing? There's no file with that name!

Similar Threads

  1. What is wrong with my code? JAVA-Eclipse
    By bespinoz in forum Loops & Control Statements
    Replies: 9
    Last Post: November 17th, 2012, 04:06 PM
  2. Whats wrong with my java code? I really need help!
    By matthewpipie in forum What's Wrong With My Code?
    Replies: 18
    Last Post: February 18th, 2012, 07:05 PM
  3. Replies: 10
    Last Post: April 5th, 2011, 09:09 AM
  4. [SOLVED] what is wrong for the java code
    By chuikingman in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 11th, 2010, 02:00 AM
  5. i'm new to java. whats is wrong in this code?
    By igorek83 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 30th, 2009, 08:38 PM