9 Replies - 378 Views - Last Post: 04 November 2012 - 07:49 PM Rate Topic: -----

#1 amanjot24  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 07-October 12

Java program that reads .txt files?

Posted 04 November 2012 - 04:16 PM

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

Okay so what my thinking is you need to read each number from each file and then add them. But whenever i try to do so it just does not process.

Would something like this be wrong?
[FileA(00) + FileB(00)] [FileA(01) + FileB(01)] [FileA(02) + FileB(02)]
[FileA(10) + FileB(10)] [FileA(11) + FileB(11)] [FileA(12) + FileB(12)]
[FileA(20) + FileB(20)] [FileA(21) + FileB(21)] [FileA(22) + FileB(22)]

If anyone could help me out figuring out the code that would be great. Thank you!

Is This A Good Question/Topic? 0
  • +

Replies To: Java program that reads .txt files?

#2 michael072  Icon User is offline

  • D.I.C Head

Reputation: 23
  • View blog
  • Posts: 97
  • Joined: 08-February 11

Re: Java program that reads .txt files?

Posted 04 November 2012 - 04:40 PM

I'm going to assume that your matrices are the same size.

The first thing you have to do is read from the files obviously.

http://www.roseindia...e-by-line.shtml

That link will show you how to read from a text file line by line. After you read your line you will want to split that line up into the individual numbers so that you can add them together.

String[] splitArray = input.split(" ");

This will give you an array of strings. You should then iterate through that array and cast each string into an int. Then you just add those numbers with the numbers of the second file and write them out to your file.

Edit: Quit making a new post for your problems when they're all related to your 1 project. Just ask them here.

This post has been edited by michael072: 04 November 2012 - 04:43 PM

Was This Post Helpful? 0
  • +
  • -

#3 amanjot24  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 07-October 12

Re: Java program that reads .txt files?

Posted 04 November 2012 - 06:01 PM

View Postmichael072, on 04 November 2012 - 04:40 PM, said:

I'm going to assume that your matrices are the same size.

The first thing you have to do is read from the files obviously.

http://www.roseindia...e-by-line.shtml

That link will show you how to read from a text file line by line. After you read your line you will want to split that line up into the individual numbers so that you can add them together.

String[] splitArray = input.split(" ");

This will give you an array of strings. You should then iterate through that array and cast each string into an int. Then you just add those numbers with the numbers of the second file and write them out to your file.

Edit: Quit making a new post for your problems when they're all related to your 1 project. Just ask them here.


First off, sorry about that other post, I saw that I needed to edit something and I posted it twice somehow. And thank you for helping out. I get everything with the buffered reader but I get confused as to how to read the file one number at a time I used the method you had told me but I am not sure if I used it right. Sorry I am new to java programming. Here is the code I have right now.

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;

public class FileMatrice {
	public static void main(String[] args) {
		
		try {
			//Reads the names from two .txt files
			FileReader FileA = new FileReader("FileA.txt");//reads first file
			FileReader FileB = new FileReader("FileB.txt");//reads second file
			BufferedReader br1 = new BufferedReader(FileA);
			BufferedReader br2 = new BufferedReader(FileB);
			FileWriter FileC = new FileWriter("output.txt");//output of merge files
			
			String num1;
			num1 = br1.readLine();
			String[] splitArray = num1.split(" ");
			
			System.out.println(splitArray);
		}
		
		//Catching an error 
		catch (Exception e) {
           System.out.println("Error with file(s): " + e);
		}
	}
}

Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Java program that reads .txt files?

Posted 04 November 2012 - 06:07 PM

View Postamanjot24, on 04 November 2012 - 07:16 PM, said:

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

So what are the rules ?
Was This Post Helpful? 0
  • +
  • -

#5 amanjot24  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 07-October 12

Re: Java program that reads .txt files?

Posted 04 November 2012 - 06:17 PM

View Postpbl, on 04 November 2012 - 06:07 PM, said:

View Postamanjot24, on 04 November 2012 - 07:16 PM, said:

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

So what are the rules ?


Did not receive any rules other than to just use the two files and the numbers contained in the files by adding them and outputting the results into an output file.
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Java program that reads .txt files?

Posted 04 November 2012 - 06:22 PM

Sorry that easy ?

two Scanner that scan.nextLine() and String.split(" ")
should do the job
determine the size of the matrix based on first call... but actually will work with a triangular matrix :)
use Integer.parseInt() to read each token

add column by column
Was This Post Helpful? 0
  • +
  • -

#7 amanjot24  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 07-October 12

Re: Java program that reads .txt files?

Posted 04 November 2012 - 06:31 PM

View Postpbl, on 04 November 2012 - 06:22 PM, said:

Sorry that easy ?

two Scanner that scan.nextLine() and String.split(" ")
should do the job
determine the size of the matrix based on first call... but actually will work with a triangular matrix :)
use Integer.parseInt() to read each token

add column by column


okay so this i where i stand right now?

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



Am i in the right direction i get and error with ".readLine and .nextInt" though.
Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Java program that reads .txt files?

Posted 04 November 2012 - 06:52 PM

Use a Scanner lot easier

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) {
       ... not same number of number
   }
   int[] tot = new int[token1.length];
   for(int i = 0; i < tot.length; ++i) {
      tot[i] = Integer.parseInt(token1[i]) + Integer.parseInt(token2[i]));
      ... do whatever you want with to[i] here
   }
}


Was This Post Helpful? 1
  • +
  • -

#9 amanjot24  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 07-October 12

Re: Java program that reads .txt files?

Posted 04 November 2012 - 07:45 PM

View Postpbl, on 04 November 2012 - 06:52 PM, said:

Use a Scanner lot easier

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) {
       ... not same number of number
   }
   int[] tot = new int[token1.length];
   for(int i = 0; i < tot.length; ++i) {
      tot[i] = Integer.parseInt(token1[i]) + Integer.parseInt(token2[i]));
      ... do whatever you want with to[i] here
   }
}



What do you mean by not same number of number?
Was This Post Helpful? 0
  • +
  • -

#10 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Java program that reads .txt files?

Posted 04 November 2012 - 07:49 PM

one file has
10 20 30
the other file has
40 30 20 10

oups... not the same number of columns
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1