Results 1 to 14 of 14
Thread: Read two matrices from file
- 05-17-2010, 12:54 AM #1
Read two matrices from file
Hello to everybody. I ve got a txt file, for instance 4x4 and i want to two matrices 4x2. I know how to read variables from a file. Have you any idea how can I do it for the matrices??
My code is this:
I have define everything that i ve to.Java Code:public void OpenFileRead(String fileName) { //anoigma tou arxeiou gia diavasma try { input = new Scanner(new File(fileName)); } catch (FileNotFoundException e) { //sfalma kata tin evresi kai to anoigma tou arxeiou System.err.println("Σφάλμα κατά το άνοιγμα αρχείου"); System.exit(0); //eksodos } } public void Load() { //anagnwsi dedomenwn apo arxeio try { while (input.hasNext()) { //oso tha iparxei apothikeumeni eggrafi String s1 = new Integer(input.nextInt()).toString(); int v = Integer.parseInt(s1.trim()); //pairnei to string kai metafrazei to antistoixo integer String s2 = new Integer(input.nextInt()).toString(); int s = Integer.parseInt(s2.trim()); //pairnei to string kai metafrazei to antistoixo Long System.out.println(v); } } catch (NoSuchElementException e) { System.err.println("Σφάλμα κατά την τροποποίηση του αρχείου"); System.err.println(e.getMessage()); input.close(); System.exit(0); } catch (IllegalStateException e) { System.err.println("Σφάλμα κατά την ανάγνωση από αρχείο"); System.exit(0); }
- 05-17-2010, 05:57 AM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
In which way the content of the file is appear?
Basically what you can do is read line by line of the file and split them on space, I hope you've separate all numbers by using a space. Then arrange them as you which into new file.
- 05-17-2010, 11:15 AM #3
ok for example i ve this file
and i want to make two matrices.Java Code:1 2 13 19 3 4 14 20 5 6 15 21 7 8 16 22 9 10 17 23 11 12 18 24
- 05-17-2010, 12:15 PM #4
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Fine. Then as I said read line by line and process as what you want. How looks like your new two metrics?
- 05-17-2010, 12:16 PM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,400
- Blog Entries
- 7
- Rep Power
- 17
- 05-18-2010, 12:36 AM #6
THanks a lot for your appreciable help. I ve got another problem when i ve numbers like 0.001 also when i ve something like 1.001 it appeared to me like 1001.0!!
- 05-18-2010, 12:42 AM #7
Could you show the input and output that demonstrates this?another problem when i ve numbers like 0.001 also when i ve something like 1.001 it appeared to me like 1001.0!!
- 05-18-2010, 01:32 AM #8
Ok i believe that the blame lies with my system, since i ve the greek edition of Windows.
- 05-18-2010, 02:11 AM #9
Do your numbers swap the use of the . and the , ?
1.000 is one thousand
1,000 is 1
Then you need to set the locale to a european standard for your numbers
- 05-18-2010, 09:18 PM #10
yea precisely i ve done it. THanks a lot for your useful help.
- 05-19-2010, 04:06 AM #11
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
If you've solve the problem, please mark the thread solved.
- 05-20-2010, 02:33 AM #12
[SOLVED]Read two matrices from file
I dont if I must begin a new thread for this. I ve tried to use arraylist to store the data from file. I want a two dimensional array so i used an arraylist of arraylist. In the function Load that I made i ve defined this arraylist instead of the two dimensional matrix.
The code is this:
w1 works properly and thanks again for your help. When i tried to use w2 i got this message:Java Code:public void Load() { //anagnwsi dedomenwn apo arxeio double[][] w1=new double[50][9]; int a=w1.length; ArrayList<ArrayList<Double>> w2 = new ArrayList<ArrayList<Double>>(); try { while (input.hasNext()) { //oso tha iparxei apothikeumeni eggrafi for (int i= 0; i < 50; i++){ for (int j= 0; j < 9; j++){ w1[i][j]= input.nextDouble(); double d=input.nextInt(); w2.add(new ArrayList<Double>()); w2.add(new Double(d)); w2=input.nextDouble(); // System.out.print(w1[i][j]+" "); System.out.print(w2); } System.out.println(); } } } catch (NoSuchElementException e) { System.err.println("Σφάλμα κατά την τροποποίηση του αρχείου"); System.err.println(e.getMessage()); //emfanisi tou minimatos sfalmatos input.close(); System.exit(0); } catch (IllegalStateException e) { System.err.println("Σφάλμα κατά την ανάγνωση από αρχείο"); System.exit(0); } }
cannot find symbol
symbol:method add(java.long.Double)
location class java.util.ArrayList<java.util.ArrayList<java.lang. Double>>
- 05-20-2010, 02:55 AM #13
When posting error messages, please COPY and post all of the message. Compiler error messages have source code line numbers in them and they don't contain misspelled words or misplaced blanks.
- 05-20-2010, 03:03 AM #14
Senior Member
- Join Date
- May 2010
- Posts
- 436
- Rep Power
- 4
w2 is an ArrayList of an ArrayList and you can't add a double or a Double directly into this. You'll have to create your ArrayList<Double> and add your double or Double to this, and then add this ArrayList<Double> into your w2 ArrayList<ArrayList<Double>>.
e.g.,
Java Code:for (int i = 0; i < 50; i++) { ArrayList<Double> innerList = new ArrayList<Double>(); for (int j = 0; j < 9; j++) { // do you need to check first input.hasNextDouble()? double nextDouble = input.nextDouble(); w1[i][j] = nextDouble; innerList.add(nextDouble); } w2.add(innerList); }Last edited by curmudgeon; 05-20-2010 at 03:10 AM.
Similar Threads
-
how to read openproj(Projity) file i.e. ,POD file(Project Management file)
By mahendra.athneria in forum New To JavaReplies: 0Last Post: 02-11-2009, 09:53 AM -
How to read and write to a file without taking out the comments in the file
By MAGNUM in forum New To JavaReplies: 5Last Post: 02-05-2009, 10:28 AM -
How to multiply two matrices
By Java Tip in forum java.langReplies: 0Last Post: 04-14-2008, 08:50 PM -
How to read a text file from a Java Archive File
By Java Tip in forum Java TipReplies: 0Last Post: 02-08-2008, 09:13 AM -
implementing sparse and nonsparse matrices together
By ishakteyran in forum New To JavaReplies: 0Last Post: 12-07-2007, 08:10 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks