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:
Code :
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!
Re: Java Program that reads .txt files?
What's not working with your current code?
Re: Java Program that reads .txt files?
Quote:
Originally Posted by
curmudgeon
What's not working with your current code?
I keep getting an error with the .readLine and .nextInt
Re: Java Program that reads .txt files?
Quote:
Originally Posted by
bhattia2
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!
Re: Java Program that reads .txt files?
Quote:
Originally Posted by
curmudgeon
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)
Re: Java Program that reads .txt files?
Quote:
Originally Posted by
bhattia2
Sorry about that! Probably would have made it a lot easier.
Heres the error message: ...
The errors are pretty self-explanatory:
Quote:
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.
Quote:
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.
Quote:
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.
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