Results 1 to 20 of 41
- 06-26-2008, 01:28 AM #1
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?
- 06-26-2008, 01:31 AM #2
My text file contains the numbers in row. See attached.
- 06-26-2008, 03:01 AM #3
Senior Member
- Join Date
- May 2008
- Location
- Makati, Philippines
- Posts
- 234
- Rep Power
- 6
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.
- 06-26-2008, 04:11 AM #4
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
- 06-26-2008, 06:15 AM #5
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.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(); } }
- 06-26-2008, 06:32 AM #6
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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.
- 06-26-2008, 06:39 AM #7
How to use long in my case?
final double PCOUNT = infinity; should change to final double PCOUNT = long; rite?
- 06-26-2008, 06:44 AM #8
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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.
- 06-26-2008, 06:50 AM #9
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?
- 06-26-2008, 07:01 AM #10
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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(); } }
- 06-26-2008, 07:12 AM #11
Senior Member
- Join Date
- May 2008
- Location
- Makati, Philippines
- Posts
- 234
- Rep Power
- 6
Thats a good start. The only problem i see in your code is this area
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:final double PCOUNT = infinity; double a[]= new double[PCOUNT];
and this areaJava Code:private double a[],y[]; private int i=0;
In this area i suggest you should use a while loop. It goes something like thisJava Code:for(i=0; i++) { inLine = fi.readLine(); a[i] = Double.parseDouble(inLine); y = ((4.2*a[i]/1010)-1.35); } fi.close();
and edit this part tooJava 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++; }
I suggest that you use a different filename for the output so that it will not overwrite the other file ^_^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 hope that helpsLast 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.
- 06-26-2008, 08:45 AM #12
Thanks for the guides Eranga and Eku. The program guides work fine,
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.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
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.
- 06-26-2008, 08:49 AM #13
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Yes, try it in your way. Better to make an attempt and have questions here. Otherwise you don't have done anything on this.
- 06-26-2008, 08:56 AM #14
Senior Member
- Join Date
- May 2008
- Location
- Makati, Philippines
- Posts
- 234
- Rep Power
- 6
here is a simple way
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 javaJava 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){}Mind only knows what lies near the heart, it alone sees the depth of the soul.
- 06-26-2008, 08:37 PM #15
Hi, Eku. I have tested the code, but the program compile with errors below.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){} } }
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.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
- 06-27-2008, 02:01 AM #16
Senior Member
- Join Date
- May 2008
- Location
- Makati, Philippines
- Posts
- 234
- Rep Power
- 6
Im so sorry it was a typo error.
Its FileWriter not Printwrite or PrinWriter ^_^ I apologize. Im kinda sleepy yesderday. ^_^ Can you please try this oneJava 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){}Mind only knows what lies near the heart, it alone sees the depth of the soul.
- 06-27-2008, 02:20 AM #17
Hi, Eku. I am not sure what is the mistakes.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){} } }
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
}
- 06-27-2008, 03:01 AM #18
Senior Member
- Join Date
- May 2008
- Location
- Makati, Philippines
- Posts
- 234
- Rep Power
- 6
My fault again, ^_^ Im sorry typo error i misplace a bracketJava 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){} } }
Can you try again pleaseMind only knows what lies near the heart, it alone sees the depth of the soul.
- 06-27-2008, 03:46 AM #19
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.
The program gives error as shown below: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){} } }
Testing.java:43: cannot find symbol
symbol : variable writeTemp
location: class java.io.BufferedWriter
out.writeTemp.substring(4);
^
1 error
- 06-27-2008, 04:17 AM #20
Senior Member
- Join Date
- May 2008
- Location
- Makati, Philippines
- Posts
- 234
- Rep Power
- 6
Similar Threads
-
Need a solution to read and store data from a file
By sheetalnri in forum New To JavaReplies: 10Last Post: 09-30-2010, 06:43 AM -
[SOLVED] How to read a file and compare Array values
By DonCash in forum Advanced JavaReplies: 2Last Post: 04-02-2008, 02:22 PM -
[SOLVED] getting values from a text file
By dav9999 in forum New To JavaReplies: 8Last Post: 04-01-2008, 01:51 AM -
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 -
How to read attributes and values in a xml file using servlet
By pragathi_forum in forum Advanced JavaReplies: 1Last Post: 12-18-2007, 05:46 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks