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 7 of 7

Thread: Stuck on .txt files java

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

    Default Stuck on .txt files java

    I have to write a program that reads two .txt files as such:

    FileA.txt
    1 2 3
    4 5 6

    FileB.txt
    0 1 2
    3 4 5
    6 7 8

    Then using the input/output files method i have to create a program that prints out the results of of adding the numbers in the .txt files. So the output would be something like this.

    FileOut.txt
    1 3 5
    7 9 11
    13 15 17

    Thus far I have the following code and I am stuck and cannot figure out how to finish it

    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.util.Scanner;
     
    public class FileMatrice {
    	public static void main(String[] args) {
     
     
    			FileWriter FileC = new FileWriter("output.txt");//output of merge files
    			Scanner in1 = new Scanner(new File("FileA.txt"));
    			Scanner in2 = new Scanner(new File("FileB.txt"));
     
    			while(in1.hasNextLine() && in2.hasNextLine()) {
    			   String line1 = in1.nextLine();   
    			   String line2 = in2.nextLine();
    			   String[] token1 = line1.split(" ");
    			   String[] token2 = line2.split(" ");
    			   if(token1.length != token2.length) {
    			   }
    			   int[] tot = new int[token1.length];
    			   for(int i = 0; i < tot.length; ++i) {
    			      tot[i] = Integer.parseInt(token1[i]) + Integer.parseInt(token2[i]);
    			      FileC.write(i);
    			   }
     
     
    		}
     
     
    		}
    	}
    }

    Could anyone help me complete the code? Thank you!


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,301
    My Mood
    Hungover
    Thanks
    142
    Thanked 620 Times in 531 Posts

    Default Re: Stuck on .txt files java

    What does your code do? What exactly are you stuck on? What confuses you?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Nov 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Stuck on .txt files java

    Quote Originally Posted by KevinWorkman View Post
    What does your code do? What exactly are you stuck on? What confuses you?
    Well tome i feel like the code is complete and should run and print the out file but when i run it, it runs but prints a blank .txt file

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,301
    My Mood
    Hungover
    Thanks
    142
    Thanked 620 Times in 531 Posts

    Default Re: Stuck on .txt files java

    And what happened when you stepped through this with a debugger, or added some print statements? Where does the program's behavior start to differ from what you expected?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Junior Member
    Join Date
    Nov 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Stuck on .txt files java

    Quote Originally Posted by KevinWorkman View Post
    And what happened when you stepped through this with a debugger, or added some print statements? Where does the program's behavior start to differ from what you expected?
    well heres the error i receive.

    Exception in thread "main" java.lang.Error: Unresolved compilation problem:

    at FileMatrice.main(FileMatrice.java:13)

  6. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,301
    My Mood
    Hungover
    Thanks
    142
    Thanked 620 Times in 531 Posts

    Default Re: Stuck on .txt files java

    So your code doesn't compile. What's the actual full text of the compiler error? What line does it occur on?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: Stuck on .txt files java

    Please look here for yesterday's discussion on the same topic: Java Program that reads .txt files?

Similar Threads

  1. Java Program that reads .txt files?
    By bhattia2 in forum File I/O & Other I/O Streams
    Replies: 6
    Last Post: November 5th, 2012, 02:30 PM
  2. Replies: 3
    Last Post: August 3rd, 2012, 10:40 AM
  3. How can I read txt files in java program??
    By sakura_smile in forum What's Wrong With My Code?
    Replies: 5
    Last Post: June 9th, 2012, 06:18 PM
  4. reading .txt files
    By deependeroracle in forum File I/O & Other I/O Streams
    Replies: 5
    Last Post: February 8th, 2012, 11:47 AM
  5. TXT files
    By eldiablo3000 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 8th, 2011, 10:46 AM

Tags for this Thread