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: Java Program that reads .txt files?

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

    Default Java Program that reads .txt files?

    I've been having a problem writing a code that would read two files that contain matrices so for example,

    FileA.txt
    1 2 3
    4 5 6
    7 8 9

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

    then the program should read the files and the the two matrices and print the results as:

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

    This is what I have thus far:

    import java.util.Scanner;
    import java.io.File;
     
     
    // Matrix Addition
    public class Matrices {
    	public final static String filename1 = "FileA.txt";
    	public final static String filename2 = "FileB.txt";
     
    	public static void main(String[] args) {
    		Scanner inFile1;
    		Scanner inFile2;
    		String in1, in2;
    		boolean eof = false, eol;
    		int mv1, mv2, mvs;
     
    		try {
    			inFile1 = new Scanner(new File(filename1));
    		} catch (Exception e) {
    			System.out.println("File could not be opened: " + filename1);
    			System.exit(0);
    		}
    		try {
    			inFile2 = new Scanner(new File(filename2));
    		} catch (Exception e) {
    			System.out.println("File could not be opened: " + filename2);
    			System.exit(0);
    		}
     
    		System.out.println("Result of the matrix addition\n");
     
    		//read file line by line until eof
    		while (!eof) {
    			if (((in1 = inFile1.readLine()) != null) && ((in2 = inFile2.readLine()) != null))
    			{
     
    				//read and add all values per line
    				eol = false;
    				while (!eol) {
    					if (((mv1 = in1.nextInt()) != null) && ((mv2 = in2.nextInt()) != null)) 
    					{
    						mvs = mv1 + mv2;
    						System.out.print(mvs + " ");
    					}
    					else eol = true;
    				} // while (!eol)
    				System.out.println();
    			}
    			else eof = true;
    		} // while (!eof)
     
    		inFile1.close();
    		inFile2.close();
    	}
    }

    I just need help completing the program. Thank you!


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

    Default Re: Java Program that reads .txt files?

    What's not working with your current code?

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

    Default Re: Java Program that reads .txt files?

    Quote Originally Posted by curmudgeon View Post
    What's not working with your current code?
    I keep getting an error with the .readLine and .nextInt

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

    Default Re: Java Program that reads .txt files?

    Quote Originally Posted by bhattia2 View Post
    I keep getting an error with the .readLine and .nextInt
    You might consider posting the error messages. It beats making us guess at these things you know!

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

    Default Re: Java Program that reads .txt files?

    Quote Originally Posted by curmudgeon View Post
    You might consider posting the error messages. It beats making us guess at these things you know!
    Sorry about that! Probably would have made it a lot easier.

    Heres the error message:

    Exception in thread "main" java.lang.Error: Unresolved compilation problems:
    The method readLine() is undefined for the type Scanner
    The method readLine() is undefined for the type Scanner
    The method nextInt() is undefined for the type String
    The method nextInt() is undefined for the type String

    at Matrices.main(Matrices.java:34)

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

    Default Re: Java Program that reads .txt files?

    Quote Originally Posted by bhattia2 View Post
    Sorry about that! Probably would have made it a lot easier.

    Heres the error message: ...
    The errors are pretty self-explanatory:

    Exception in thread "main" java.lang.Error: Unresolved compilation problems:
    You're trying to run a program that doesn't compile. Never do that as it's guaranteed to fail, but instead fix the compilation errors before trying to run.

    The method readLine() is undefined for the type Scanner
    The method readLine() is undefined for the type Scanner
    OK, you're trying to use a method that doesn't exist. You should check the Scanner API online, and only use methods that it actually has.

    The method nextInt() is undefined for the type String
    The method nextInt() is undefined for the type String
    Ditto for the String class.

    Bottom line: you can't make up methods and hope that it works. Get very familiar with the Java API as it will show you what methods are available for objects of all of the core classes.

  7. #7
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Java Program that reads .txt files?

    Please do not create multiple posts about the same problem.

    Thread locked as duplicate of Stuck on .txt files java

Similar Threads

  1. Replies: 3
    Last Post: August 3rd, 2012, 10:40 AM
  2. Write a Java program that reads file
    By Mehwish-S in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: July 28th, 2012, 03:00 PM
  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. Replies: 2
    Last Post: November 30th, 2011, 07:35 PM
  5. TXT files
    By eldiablo3000 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 8th, 2011, 11:46 AM

Tags for this Thread