Page 1 of 3 123 LastLast
Results 1 to 20 of 41
  1. #1
    janeansley's Avatar
    janeansley is offline Member
    Join Date
    Jun 2008
    Posts
    23
    Rep Power
    0

    Default How to Read data from text file and calculate the values?

    Dear all,

    Just get to learn Java for fun.
    I would like to make a program that can read from text file and calculate each of the value in row using the equation y=((4.2x/1010)-1.35) from the text file "row_numbers.txt" and output the answers to another text file.

    Anyway to do it?

  2. #2
    janeansley's Avatar
    janeansley is offline Member
    Join Date
    Jun 2008
    Posts
    23
    Rep Power
    0

    Default

    My text file contains the numbers in row. See attached.
    Attached Files Attached Files

  3. #3
    Eku
    Eku is offline Senior Member
    Join Date
    May 2008
    Location
    Makati, Philippines
    Posts
    234
    Rep Power
    7

    Default

    i think you should read the tutorial first.
    -File Input and Output (how to read a file you can also search about BufferedReader)
    -String Manipulation (How to get yourself around string and converting them to other formats such as integers and floats)

    Then try making your code. If there is any problem you can ask again here ^_^
    Mind only knows what lies near the heart, it alone sees the depth of the soul.

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

    Default

    Quote Originally Posted by janeansley View Post
    Anyway to do it?
    Yes there are lots of ways to do it. But as Eku says have to try something yourself first. If you stuck on something ask here. We all are ready to help you.

  5. #5
    janeansley's Avatar
    janeansley is offline Member
    Join Date
    Jun 2008
    Posts
    23
    Rep Power
    0

    Default

    Java Code:
    import java.util.Scanner;
    import java.io.*;
    
    public class TestCalculate
    {
       public static void main( String args[] )
       throws java.io.IOException
       {
    
    	   final double PCOUNT = infinity;
    	   double a[]= new double[PCOUNT];
    	   double i, y=0;
    
    	   String filename, inLine;
    
    
    	   //keyboard input stream
    	   InputStreamReader isr = new InputStreamReader(System.in);
    	   BufferedReader br = new BufferedReader(isr);
    
    	   //get the input data
    	   System.out.print( "Enter the txt file name: " );
    	   filename = br.readLine();
    
    	   //file input stream
    	   BufferedReader fi = new BufferedReader(new FileReader(filename));
    
    	   //read existing data and calculate
    	   for(i=0; i++)
    	   {
    		   inLine = fi.readLine();
    		   a[i] = Double.parseDouble(inLine);
    		   y = ((4.2*a[i]/1010)-1.35);
    	   }
    	   fi.close();
    
    	   //store the calculated value in another txt file
    	   PrintWrite pw = new PrinWriter(new BufferedWriter(new FileWriter(filename)));
    
    	   for(i=0; i++)
    	   pw.println(y[i]);
    
    	   System.out.print( "A calculated Output.txt file is created" );
    	   pw.close();
    
    
       }
    
    }
    I have tried to create something here, I am not sure is correct as I am new still learning, but my program having errors. I have problem with the calculation and create the "Output.txt". Please correct me. Thanks.

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

    Default

    First thing it you can't define a variable as 'infinity'

    If you want to use a large number use a long value. I don't think you can have such a big size array. You comes with memory issues.

  7. #7
    janeansley's Avatar
    janeansley is offline Member
    Join Date
    Jun 2008
    Posts
    23
    Rep Power
    0

    Default

    How to use long in my case?
    final double PCOUNT = infinity; should change to final double PCOUNT = long; rite?

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

    Default

    No pal, you completely wrong. long is a primitive data type.You have to learn a lot. How did you right this code.

    Defining for loop also wrong. Major issue you have is with data types conflict.

  9. #9
    janeansley's Avatar
    janeansley is offline Member
    Join Date
    Jun 2008
    Posts
    23
    Rep Power
    0

    Default

    I refer some books to write this code. I remember that I study a basic Java before about single input to calculate an output. I just need to make a program that can calculate a series of input from the txt file. Could you give me some guidelines?

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

    Default

    Simply test this code see where you going wrong. I have made some changes.

    Java Code:
    import java.io.*;
    
    public class TestCalculate
    {
       public static void main( String args[] )
       throws java.io.IOException
       {
    
    	   final int PCOUNT = 2000;
    	   int a[]= new int[PCOUNT];
    	   int i = 0;
               double y=0;
    
    	   String filename, inLine;
    
    
    	   //keyboard input stream
    	   InputStreamReader isr = new InputStreamReader(System.in);
    	   BufferedReader br = new BufferedReader(isr);
    
    	   //get the input data
    	   System.out.print( "Enter the txt file name(full path): " );
    	   filename = br.readLine();
    
    	   //file input stream
    	   BufferedReader fi = new BufferedReader(new FileReader(filename));
    
    	   //read existing data and calculate
               while((inLine = fi.readLine()) != null) {
                   a[i] = Integer.parseInt(inLine);
                   y = ((4.2*a[i]/1010)-1.35);
                   System.out.println(y);
                   i++;
               }
               
              // Close the stream for safety 
              fi.close();
       }
    
    }

  11. #11
    Eku
    Eku is offline Senior Member
    Join Date
    May 2008
    Location
    Makati, Philippines
    Posts
    234
    Rep Power
    7

    Default

    Thats a good start. The only problem i see in your code is this area

    Java Code:
    final double PCOUNT = infinity;
    	   double a[]= new double[PCOUNT];
    You cant declare a infinity, what you can use is Long. but of course you can do is declare an array that can grow.
    Java Code:
    private double a[],y[]; 
    private int i=0;
    and this area

    Java Code:
    for(i=0; i++)
    	   {
    		   inLine = fi.readLine();
    		   a[i] = Double.parseDouble(inLine);
    		   y = ((4.2*a[i]/1010)-1.35);
    	   }
    	   fi.close();
    In this area i suggest you should use a while loop. It goes something like this

    Java Code:
    i=0;
    while ((inLine = in.readLine()) != null) {
                              inLine = fi.readLine();
    		   a[i] = Double.parseDouble(inLine);
    		   y[i] = ((4.2*a[i]/1010)-1.35); i++;
                      }
    and edit this part too

    Java Code:
    PrintWriter pw = new PrinWriter(new BufferedWriter(new FileWriter(filename)));
    
    	   for(j=0; j<= i; j++)
    	   pw.println(y[j] + "\n"); \\added a nextline command ^_^
    I suggest that you use a different filename for the output so that it will not overwrite the other file ^_^

    I hope that helps
    Last edited by Eku; 06-26-2008 at 07:14 AM.
    Mind only knows what lies near the heart, it alone sees the depth of the soul.

  12. #12
    janeansley's Avatar
    janeansley is offline Member
    Join Date
    Jun 2008
    Posts
    23
    Rep Power
    0

    Default

    Thanks for the guides Eranga and Eku. The program guides work fine,

    Enter the txt file name(full path): row_numbers.txt
    3.764851485148515
    3.848019801980198
    5.249405940594061
    6.891980198019802
    3.9062376237623764
    6.347227722772278
    2.8583168316831684
    4.9957425742574255
    5.257722772277228
    5.199504950495049
    3.8812871287128714
    3.8812871287128714
    3.7814851485148515
    5.257722772277228
    6.463663366336634
    6.833762376237624
    5.490594059405941
    4.796138613861386
    4.326237623762376
    5.087227722772278
    3.83970297029703
    4.796138613861386
    3.8771287128712877
    5.2909900990099015
    5.083069306930694
    But the problem is that i would need to save the calculated value into another new text file. As when the the row of numbers in the"row_numbers.txt" file is 5000, the display will mess up.



    So , I need to change it to be save into another txt file by giving the new txt file a new name. I have compile the program below but it comes with errors.
    Correct me if I am wrong. Thanks.

    Java Code:
    import java.io.*;
    
    public class Testi
    {
       public static void main( String args[] )
       throws java.io.IOException
       {
    
    	   final int PCOUNT = 5000;
    	   int a[]= new int[PCOUNT];
    	   int i = 0;
    	   int j = 0;
    	   double y[];
    
    	   String filename, inLine;
    
    
    	   //keyboard input stream
    	   InputStreamReader isr = new InputStreamReader(System.in);
    	   BufferedReader br = new BufferedReader(isr);
    
    	   //get the input data
    	   System.out.print( "Enter the txt file name(full path): " );
    	   filename = br.readLine();
    
    	   //file input stream
    	   BufferedReader fi = new BufferedReader(new FileReader(filename));
    
    	   //read existing data and calculate
               while((inLine = fi.readLine()) != null) {
                   a[i] = Integer.parseInt(inLine);
                   y[j] = ((4.2*a[i]/1010)-1.35);
         //          System.out.println(y);
                   i++;
               }
    
              // Close the stream for safety
              fi.close();
    
    
    	   PrintWrite pw = new PrinWriter(new BufferedWriter(new FileWriter(filename)));
    
    	   for(j=0; j<= i; j++)
    	   pw.println(y[j] + "\n");
    
    	   System.out.print( "A calculated Output.txt file is created" );
    	   pw.close();
    
       }
    
    }

    I would like to change "System.out.println(y);" results to be save into a new txt file.

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

    Default

    Yes, try it in your way. Better to make an attempt and have questions here. Otherwise you don't have done anything on this.

  14. #14
    Eku
    Eku is offline Senior Member
    Join Date
    May 2008
    Location
    Makati, Philippines
    Posts
    234
    Rep Power
    7

    Default

    here is a simple way

    Java Code:
    try{
    PrintWrite pw = new PrinWriter(new BufferedWriter(new FileWriter("C:\\output.txt")));
    String Temp = null;
    	   for(j=0; j<= i; j++){
    	   Temp += y[j] + "\n";} 
    pw.print(Temp.substring(4)); pw.close();}
    catch (Exception A){}
    First I collected them in a single String then append each of them in that String then save that String into a File in C:\output.txt. You can change the location if you want just use \\ instead of \. because \ is an escaped character in java
    Mind only knows what lies near the heart, it alone sees the depth of the soul.

  15. #15
    janeansley's Avatar
    janeansley is offline Member
    Join Date
    Jun 2008
    Posts
    23
    Rep Power
    0

    Default

    Java Code:
    import java.io.*;
    
    public class Testing
    {
       public static void main( String args[] )
       throws java.io.IOException
       {
    
    	   final int PCOUNT = 5000;
    	   int a[]= new int[PCOUNT];
    	   int i = 0;
    	   int j = 0;
    	   double y[];
    
    	   String filename, inLine;
    
    
    	   //keyboard input stream
    	   InputStreamReader isr = new InputStreamReader(System.in);
    	   BufferedReader br = new BufferedReader(isr);
    
    	   //get the input data
    	   System.out.print( "Enter the txt file name(full path): " );
    	   filename = br.readLine();
    
    	   //file input stream
    	   BufferedReader fi = new BufferedReader(new FileReader(filename));
    
    	   //read existing data and calculate
               while((inLine = fi.readLine()) != null) {
                   a[i] = Integer.parseInt(inLine);
                   y[j] = ((4.2*a[i]/1010)-1.35);
                   i++;
               }
              fi.close();
    
    
    	   try{
    	   PrintWrite pw = new PrinWriter(new BufferedWriter(new FileWriter("C:\\output.txt")));
    	   String Temp = null;
    	   	   for(j=0; j<= i; j++){
    	   	   Temp += y[j] + "\n";}
    	   pw.print(Temp.substring(4));
    	   pw.close();}
    	   catch (Exception A){}
    
    
    
       }
    
    }
    Hi, Eku. I have tested the code, but the program compile with errors below.

    Testing.java:40: cannot find symbol
    symbol : class PrintWrite
    location: class Testing
    PrintWrite pw = new PrinWriter(new BufferedWriter(new FileWriter("C:\
    \output.txt")));
    ^
    Testing.java:40: cannot find symbol
    symbol : class PrinWriter
    location: class Testing
    PrintWrite pw = new PrinWriter(new BufferedWriter(new FileWriter("C:\
    \output.txt")));
    ^
    2 errors
    Could you check for me please, it's not running as mentioned cannot find symbol? I just wana make the the output program calculated values to be save into a txt file.

  16. #16
    Eku
    Eku is offline Senior Member
    Join Date
    May 2008
    Location
    Makati, Philippines
    Posts
    234
    Rep Power
    7

    Default

    Im so sorry it was a typo error.

    Java Code:
    try{
    FileWriter fw = new FileWriter("C:\\output.txt");
    BufferedWriter out = new BufferedWriter(fw);
    String Temp = null;
    	   for(j=0; j<= i; j++){
    	   Temp += y[j] + "\n";} 
    out.writeTemp.substring(4));
    out.close();
    catch (Exception A){}
    Its FileWriter not Printwrite or PrinWriter ^_^ I apologize. Im kinda sleepy yesderday. ^_^ Can you please try this one
    Mind only knows what lies near the heart, it alone sees the depth of the soul.

  17. #17
    janeansley's Avatar
    janeansley is offline Member
    Join Date
    Jun 2008
    Posts
    23
    Rep Power
    0

    Default

    Java Code:
    import java.io.*;
    
    public class Testing
    {
       public static void main( String args[] )
       throws java.io.IOException
       {
    
    	   final int PCOUNT = 5000;
    	   int a[]= new int[PCOUNT];
    	   int i = 0;
    	   int j = 0;
    	   double y[];
    
    	   String filename, inLine;
    
    
    	   //keyboard input stream
    	   InputStreamReader isr = new InputStreamReader(System.in);
    	   BufferedReader br = new BufferedReader(isr);
    
    	   //get the input data
    	   System.out.print( "Enter the txt file name(full path): " );
    	   filename = br.readLine();
    
    	   //file input stream
    	   BufferedReader fi = new BufferedReader(new FileReader(filename));
    
    	   //read existing data and calculate
               while((inLine = fi.readLine()) != null) {
                   a[i] = Integer.parseInt(inLine);
                   y[j] = ((4.2*a[i]/1010)-1.35);
                   i++;
               }
              fi.close();
    
              try{
    		  FileWriter fw = new FileWriter("C:\\output.txt");
    		  BufferedWriter out = new BufferedWriter(fw);
    		  String Temp = null;
    		  	   for(j=0; j<= i; j++){
    				   Temp += y[j] + "\n";}
    		  out.writeTemp.substring(4));
    		  out.close();
    		  catch (Exception A){}
    
       }
    
    }
    Hi, Eku. I am not sure what is the mistakes.
    I compiled the program and it is still run with errors. Could you guide me the problems?

    Testing.java:43: ';' expected
    out.writeTemp.substring(4));
    ^
    Testing.java:45: 'catch' without 'try'
    catch (Exception A){}
    ^
    Testing.java:45: ')' expected
    catch (Exception A){}
    ^
    Testing.java:45: not a statement
    catch (Exception A){}
    ^
    Testing.java:45: ';' expected
    catch (Exception A){}
    ^
    Testing.java:37: 'try' without 'catch' or 'finally'
    try{
    ^
    Testing.java:49: reached end of file while parsing
    }

  18. #18
    Eku
    Eku is offline Senior Member
    Join Date
    May 2008
    Location
    Makati, Philippines
    Posts
    234
    Rep Power
    7

    Default

    Java Code:
    import java.io.*;
    
    public class Testing
    {
       public static void main( String args[] )
       throws java.io.IOException
       {
    
    	   final int PCOUNT = 5000;
    	   int a[]= new int[PCOUNT];
    	   int i = 0;
    	   int j = 0;
    	   double y[];
    
    	   String filename, inLine;
    
    
    	   //keyboard input stream
    	   InputStreamReader isr = new InputStreamReader(System.in);
    	   BufferedReader br = new BufferedReader(isr);
    
    	   //get the input data
    	   System.out.print( "Enter the txt file name(full path): " );
    	   filename = br.readLine();
    
    	   //file input stream
    	   BufferedReader fi = new BufferedReader(new FileReader(filename));
    
    	   //read existing data and calculate
               while((inLine = fi.readLine()) != null) {
                   a[i] = Integer.parseInt(inLine);
                   y[j] = ((4.2*a[i]/1010)-1.35);
                   i++;
               }
              fi.close();
    
              try{
    		  FileWriter fw = new FileWriter("C:\\output.txt");
    		  BufferedWriter out = new BufferedWriter(fw);
    		  String Temp = null;
    		  	   for(j=0; j<= i; j++){
    				   Temp += y[j] + "\n";}
    		  out.writeTemp.substring(4);
    		  out.close();
                            }catch (Exception A){}
    
       }
    
    }
    My fault again, ^_^ Im sorry typo error i misplace a bracket
    Can you try again please
    Mind only knows what lies near the heart, it alone sees the depth of the soul.

  19. #19
    janeansley's Avatar
    janeansley is offline Member
    Join Date
    Jun 2008
    Posts
    23
    Rep Power
    0

    Default

    Thanks for the guide, I did't notice that even a more of ")" will give so many errors.

    Now, I compiled the program again, the program cannot run.
    It mentioned cannot find symbol: variable writeTemp.
    Where should I correct the symbol problem? Please guide me.

    Java Code:
    import java.io.*;
    
    public class Testing
    {
       public static void main( String args[] )
       throws java.io.IOException
       {
    
    	   final int PCOUNT = 5000;
    	   int a[]= new int[PCOUNT];
    	   int i = 0;
    	   int j = 0;
    	   double y[];
    
    	   String filename, inLine;
    
    
    	   //keyboard input stream
    	   InputStreamReader isr = new InputStreamReader(System.in);
    	   BufferedReader br = new BufferedReader(isr);
    
    	   //get the input data
    	   System.out.print( "Enter the txt file name(full path): " );
    	   filename = br.readLine();
    
    	   //file input stream
    	   BufferedReader fi = new BufferedReader(new FileReader(filename));
    
    	   //read existing data and calculate
               while((inLine = fi.readLine()) != null) {
                   a[i] = Integer.parseInt(inLine);
                   y[j] = ((4.2*a[i]/1010)-1.35);
                   i++;
               }
              fi.close();
    
              try{
    		  FileWriter fw = new FileWriter("C:\\output.txt");
    		  BufferedWriter out = new BufferedWriter(fw);
    		  String Temp = null;
    		  	   for(j=0; j<= i; j++){
    				   Temp += y[j] + "\n";}
    		  out.writeTemp.substring(4);
    		  out.close();
                            }catch (Exception A){}
    
       }
    
    }
    The program gives error as shown below:

    Testing.java:43: cannot find symbol
    symbol : variable writeTemp
    location: class java.io.BufferedWriter
    out.writeTemp.substring(4);
    ^
    1 error

  20. #20
    Eku
    Eku is offline Senior Member
    Join Date
    May 2008
    Location
    Makati, Philippines
    Posts
    234
    Rep Power
    7

    Default

    please change it to

    out.write(Temp.substring(4));

    missing parenthesis T_T Sorry again. thats strike 3 for me
    Mind only knows what lies near the heart, it alone sees the depth of the soul.

Page 1 of 3 123 LastLast

Similar Threads

  1. Need a solution to read and store data from a file
    By sheetalnri in forum New To Java
    Replies: 10
    Last Post: 09-30-2010, 06:43 AM
  2. [SOLVED] How to read a file and compare Array values
    By DonCash in forum Advanced Java
    Replies: 2
    Last Post: 04-02-2008, 02:22 PM
  3. [SOLVED] getting values from a text file
    By dav9999 in forum New To Java
    Replies: 8
    Last Post: 04-01-2008, 01:51 AM
  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, 09:13 AM
  5. How to read attributes and values in a xml file using servlet
    By pragathi_forum in forum Advanced Java
    Replies: 1
    Last Post: 12-18-2007, 05:46 PM

Posting Permissions

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