|
|
Welcome to the Java Forums.
You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:
- have access to post topics
- communicate privately with other members (PM)
- not see advertisements between posts
- have the possibility to earn one of our surprises if you are an active member
- access many other special features that will be introduced later.
Registration is fast, simple and absolutely free so please, join our community today!
If you have any problems with the registration process or your account login, please contact us.
|
|

06-26-2008, 02:28 AM
|
 |
Member
|
|
Join Date: Jun 2008
Posts: 23
|
|
|
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, 02:31 AM
|
 |
Member
|
|
Join Date: Jun 2008
Posts: 23
|
|
|
My text file contains the numbers in row. See attached.
|
|

06-26-2008, 04:01 AM
|
|
Senior Member
|
|
Join Date: May 2008
Location: Makati, Philippines
Posts: 188
|
|
|
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, 05:11 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 2,875
|
|
Originally Posted by janeansley
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.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. (Close on September 4, 2008)
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

06-26-2008, 07:15 AM
|
 |
Member
|
|
Join Date: Jun 2008
Posts: 23
|
|
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.
|
|

06-26-2008, 07:32 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 2,875
|
|
|
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.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. (Close on September 4, 2008)
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

06-26-2008, 07:39 AM
|
 |
Member
|
|
Join Date: Jun 2008
Posts: 23
|
|
|
How to use long in my case?
final double PCOUNT = infinity; should change to final double PCOUNT = long; rite?
|
|

06-26-2008, 07:44 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 2,875
|
|
|
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.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. (Close on September 4, 2008)
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

06-26-2008, 07:50 AM
|
 |
Member
|
|
Join Date: Jun 2008
Posts: 23
|
|
|
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, 08:01 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 2,875
|
|
Simply test this code see where you going wrong. I have made some changes.
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();
}
}
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. (Close on September 4, 2008)
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

06-26-2008, 08:12 AM
|
|
Senior Member
|
|
Join Date: May 2008
Location: Makati, Philippines
Posts: 188
|
|
Thats a good start. The only problem i see in your code is this area
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.
private double a[],y[];
private int i=0;
and this area
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
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
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
__________________
Mind only knows what lies near the heart, it alone sees the depth of the soul.
Last edited by Eku : 06-26-2008 at 08:14 AM.
|
|

06-26-2008, 09:45 AM
|
 |
Member
|
|
Join Date: Jun 2008
Posts: 23
|
|
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.
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, 09:49 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 2,875
|
|
|
Yes, try it in your way. Better to make an attempt and have questions here. Otherwise you don't have done anything on this.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. (Close on September 4, 2008)
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

06-26-2008, 09:56 AM
|
|
Senior Member
|
|
Join Date: May 2008
Location: Makati, Philippines
Posts: 188
|
|
here is a simple way
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.
|
|

06-26-2008, 09:37 PM
|
 |
Member
|
|
Join Date: Jun 2008
Posts: 23
|
|
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.
|
|

06-27-2008, 03:01 AM
|
|
Senior Member
|
|
Join Date: May 2008
Location: Makati, Philippines
Posts: 188
|
|
Im so sorry it was a typo error.
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.
|
|

06-27-2008, 03:20 AM
|
 |
Member
|
|
Join Date: Jun 2008
Posts: 23
|
|
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
}
|
|

06-27-2008, 04:01 AM
|
|
Senior Member
|
|
Join Date: May 2008
Location: Makati, Philippines
Posts: 188
|
|
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.
|
|

06-27-2008, 04:46 AM
|
 |
Member
|
|
Join Date: Jun 2008
Posts: 23
|
|
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.
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;
| | |