Results 1 to 14 of 14
  1. #1
    zenitis's Avatar
    zenitis is offline Member
    Join Date
    May 2010
    Posts
    33
    Rep Power
    0

    Cool 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:
    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);
            }
    I have define everything that i ve to.

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    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.

  3. #3
    zenitis's Avatar
    zenitis is offline Member
    Join Date
    May 2010
    Posts
    33
    Rep Power
    0

    Default

    ok for example i ve this file
    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
    and i want to make two matrices.

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Fine. Then as I said read line by line and process as what you want. How looks like your new two metrics?

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,773
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by zenitis View Post
    ok for example i ve this file
    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
    and i want to make two matrices.
    If you have a Scanner for that file simply call the nextInt() method eight times per matrix, like so:

    Java Code:
    for (int i= 0; i < matrix.length; i++)
       for (int j= 0; j < matrix[i].length; j++)
          matrix[i][j]= yourScanner.nextInt();
    kind regards,

    Jos

  6. #6
    zenitis's Avatar
    zenitis is offline Member
    Join Date
    May 2010
    Posts
    33
    Rep Power
    0

    Default

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

  7. #7
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,883
    Rep Power
    25

    Default

    another problem when i ve numbers like 0.001 also when i ve something like 1.001 it appeared to me like 1001.0!!
    Could you show the input and output that demonstrates this?

  8. #8
    zenitis's Avatar
    zenitis is offline Member
    Join Date
    May 2010
    Posts
    33
    Rep Power
    0

    Default

    Ok i believe that the blame lies with my system, since i ve the greek edition of Windows.

  9. #9
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,883
    Rep Power
    25

    Default

    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

  10. #10
    zenitis's Avatar
    zenitis is offline Member
    Join Date
    May 2010
    Posts
    33
    Rep Power
    0

    Default

    yea precisely i ve done it. THanks a lot for your useful help.

  11. #11
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

  12. #12
    zenitis's Avatar
    zenitis is offline Member
    Join Date
    May 2010
    Posts
    33
    Rep Power
    0

    Default [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:
    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);
                }
        }
    w1 works properly and thanks again for your help. When i tried to use w2 i got this message:

    cannot find symbol
    symbol:method add(java.long.Double)
    location class java.util.ArrayList<java.util.ArrayList<java.lang. Double>>

  13. #13
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,883
    Rep Power
    25

    Default

    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.

  14. #14
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    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 04:10 AM.

Similar Threads

  1. Replies: 0
    Last Post: 02-11-2009, 10:53 AM
  2. Replies: 5
    Last Post: 02-05-2009, 11:28 AM
  3. How to multiply two matrices
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-14-2008, 09:50 PM
  4. How to read a text file from a Java Archive File
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-08-2008, 10:13 AM
  5. implementing sparse and nonsparse matrices together
    By ishakteyran in forum New To Java
    Replies: 0
    Last Post: 12-07-2007, 09:10 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •