1 2 Previous Next 17 Replies Latest reply: Aug 22, 2007 2:06 PM by 807605 RSS

    Reading values from txt file

    807605
      Hi,
      I was hoping someone could help me. I am trying to read these values
      3.182, 0.02734, 1126.0, 4.998E-4, 0.01581, 11.0, -150.3, 10.46, 3.735, 0.8474, -0.4854, 0.7866, 1.475, 0.9439, 0.769, 0.1846, 0.07678, -0.9491, -1.236, -0.9908, 0.5265, -0.27, 0.3569, -0.09167, 0.1295, -0.04507, 0.1029, -0.03005, 0.0, 0.03752, 0.001151, 0.2935, 74.84, 19080.0, 6.086, etc
      from a txt file. I have done some coding on it but dont seem to be getting the desired results. I want to take these values and put them in a 2D array.. but its not doing it. Can you see any mistakes i have done please ?
      public double[][] readFile(){
                
                 double[][] dArray = new double[164][37];
                 int a  = 0;
              int b = 0;  
             
                      try
                      {  FileReader fin = new FileReader("myFile.txt");
                                 BufferedReader in = new BufferedReader(fin);
                                   String line;
                              
                                   while((line = in.readLine()) != null){
                                  String[] value = line.split(", ");
                              
                                   
                                  for (int x=0; x<value.length ; x++){
                                       System.out.println("a:  "+a+" || b: "+b);
                                           if (b == 37) {
                                                  a = a++;  
                                              } else 
                                             {
                                                  dArray[a] = Double.parseDouble(value[x]);
                               System.out.println("value: "+dArray[1][1]);
                               b++;
                          }     
      }
                          } 
        • 1. Re: Reading values from txt file
          807605
          lets start at the beginning :

          in your code fragment :

          - there is no a class definition
          - there is no main method
          - you cannot have a try without a catch....
          -...

          try to fix these for a start
          • 2. Re: Reading values from txt file
            807605
            Yes thats because its part of larger code.. it is just a method within my code.

            but your right about the catch but so yeah sorry i missed it when i copied pasted. I ve also added you a main method.
            public double[][] readFile(){
                      
                       double[][] dArray = new double[164][37];
                       int a  = 0;
                    int b = 0;  
                   
                            try
                            {  FileReader fin = new FileReader("myFile.txt");
                                       BufferedReader in = new BufferedReader(fin);
                                         String line;
                                         
                                                                  
                                         while((line = in.readLine()) != null){
                                        String[] value = line.split(", ");
                                    
                                         
                                        for (int x=0; x<value.length ; x++){
                                             System.out.println("a:  "+a+" || b: "+b);
                                                 if (b == 37) {
                                                        a = a++;  
                                                    } else 
                                                   {
                                                        dArray[a] = Double.parseDouble(value[x]);
                                     System.out.println("value: "+dArray[1][1]);
                                     b++;
                                }     
            }
                                }
                      
                                     
                                
                      }
                      
                      catch (IOException e){}
                      return dArray;     

            public static void main(String[] args) {
                      new Extract();
                 }
                      }/code]
            • 3. Re: Reading values from txt file
              807605
              Sorry i messed up the last copy paste again!
              code]
                   public double[][] readFile(){
                        
                        double[][] dArray = new double[164][37];
                        int a = 0;
              int b = 0;

                        try
                        {  FileReader fin = new FileReader("myFile.txt");
                             BufferedReader in = new BufferedReader(fin);
                                  String line;
                                  
                                  
                                  while((line = in.readLine()) != null){
              String[] value = line.split(", ");

                                  
              for (int x=0; x<value.length ; x++){
                   System.out.println("a: "+a+" || b: "+b);
                   if (b == 37) {
                                       a = a++;
                                  } else
                                  {
                                       dArray[a] = Double.parseDouble(value[x]);
                                       System.out.println("value: "+dArray[1][1]);
                                       b++;
                                  }     
              }
                                  }
                        }
                        
                        catch (IOException e){}
                        return dArray;     
                        }

              • 4. Re: Reading values from txt file
                807605
                dArray[a] = Double.parseDouble(value[x]);

                dArray[a] is a array, and Double.parseDouble will return a double...

                so it is complaining that it cannot put a double in a double[]
                • 5. Re: Reading values from txt file
                  807605
                  k i see what your saying, but as i am putting a value into dArray[a]... i need to take the value that is returned by Double.parseDouble(value[x]) and put it in b, incrementing b each time to take the next value. Because the way i done it works with a single array but with a 2d array its acting funny. I just get a load of 0s.
                  • 6. Re: Reading values from txt file
                    807605
                    k i see what your saying, but as i am putting a value into dArray[a]... i need to take the value that is returned by Double.parseDouble(value[x]) and put it in b, incrementing b each time to take the next value. Because the way i done it works with a single array but with a 2d array its acting funny. I just get a load of 0s.
                    • 7. Re: Reading values from txt file
                      807605
                      if you want to put something in dArray[a]
                       double[]  da  = create  the double array
                       dArray[x] = da
                      or
                      for i ..{
                      dArray[a] = Double.parsedouble(..);


                      Message was edited by:
                      bompie

                      null
                      • 8. Re: Reading values from txt file
                        807605
                        I believe you problem is the following: I guess because you didn't ask a question.

                        you insert in your 2 dimensional array
                        dArray[a] = Double.parseDouble(value[x])

                        first [0,0]
                        then [0,1]... [0,37]
                        and now happens
                        [1,37],2,37][3,37]...till [length-27,37]. isnt'it

                        as soon as you reach b==37 I belive you should not only increment a but also reset b to 0.
                        • 9. Re: Reading values from txt file
                          807605
                          Ah thanks ..sorry, i was finding it difficult to explain it. But that sounds like what i wanted . thanks !
                          • 10. Re: Reading values from txt file
                            807605
                            I think your problem is in the following line :

                                 a = a++;



                            The value of a does not change.

                            the correct notation is one of the following :

                            a++;

                            a+=1;

                            a=a+1;
                            Can anyone explain why a is not changed after a= a++; ??? thanks




                            -- when you increment a you 'll have to reset b..
                            • 11. Re: Reading values from txt file
                              807605
                              yes I can.
                              a++ means post increment, this means first everything other is done, then the increment happens.
                              first a=a is performed
                              after the in between temporary workingvariable a (in the expression , thats why temporary) is incremented an so a case for the garbagge collector.
                              • 12. Re: Reading values from txt file
                                796440
                                yes I can.
                                a++ means post increment, this means first everything
                                other is done, then the increment happens.
                                first a=a is performed
                                after the in between temporary workingvariable a (in
                                the expression , thats why temporary) is incremented
                                No.

                                a=a++ is equivalent to
                                tmp = a; // get current value of a, since the value of the a++ expression is a's value before incrementing.
                                
                                a = a + 1; // increment a. This is the ++
                                
                                a = tmp; // this is the "a=" that we explicitly wrote.
                                When you do a = something, "something" is always evaluated completely first, and THEN the result of that evaluation is stuffed into a.

                                an so a case for the garbagge collector.
                                No, GC is not involved in any way.
                                • 13. Re: Reading values from txt file
                                  807605
                                  thanks jverd,

                                  I couldn't understand while doing a x++ the value was unchanged.
                                  I makes sense if I see the equivalent code.

                                  But can you tell me how I can find the equivalent code myself ?
                                  • 14. Re: Reading values from txt file
                                    796440
                                    thanks jverd,

                                    I couldn't understand while doing a x++ the value was
                                    unchanged.
                                    I makes sense if I see the equivalent code.

                                    But can you tell me how I can find the equivalent
                                    code myself ?
                                    Well, that specific code isn't actually generated. It's simply a useful device to explain how it works.

                                    To find the stone tablets that say it works that way, you'd have to dig around the JLS and the VM spec. I'm not sure how I came to know that. Might be somebody told me. Might be I deduced it. Might be I read enough of the JLS to put it together.
                                    1 2 Previous Next