|
|
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-27-2008, 06:20 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,609
|
|
|
janeansley, it's better that you familiar with basis error message handling in Java. All what you get in the code are simple ones.
__________________
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.
|
|

06-27-2008, 07:12 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.write(Temp.substring(4));
out.close();
}catch (Exception A){}
}
}
After corected and compiled, another errors come again.
Testing.java:32: variable y might not have been initialized
y[j] = ((4.2*a[i]/1010)-1.35);
^
Testing.java:42: variable y might not have been initialized
Temp += y[j] + "\n";}
^
2 errors
Why it said variable y might not have been initialized? I might did something wrong there. I am not sure whether my array has been declared properly. Please correct me thanks.
|
|

06-27-2008, 07:17 AM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Posts: 899
|
|
|
It seems to me that your method of programming is:
1) find an error and post it here.
2) fix what is stated here, then go back to step one.
I think that somewhere in this loop you have to have:
3) try to debug it myself.
Don't you think?
|
|

06-27-2008, 07:20 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,609
|
|
|
Even get advice 100 times why these guys don't listen that. Try yourself at least a simple error like this. Error says that 'y' is not initialized. Go through the code and see is it true? If yes initialized it is.
__________________
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.
|
|

06-27-2008, 09:05 AM
|
|
Senior Member
|
|
Join Date: May 2008
Location: Makati, Philippines
Posts: 228
|
|
|
Yup you can figure that one. Try looking into the tutorials on declaration. Look for how to declare an array. ^_^
__________________
Mind only knows what lies near the heart, it alone sees the depth of the soul.
|
|

06-28-2008, 09:48 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;
final double y[]= new double[PCOUNT];
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.write(Temp.substring(4));
out.close();
}catch (Exception A){}
}
}
Hi, I have initialized for the array and now the program can run finally and save into the output.txt file.
But the there's only a value save in the output.txt file in a row?
Please guide my problems. Thanks.
5.0830693069306940.00.00.00.00.00.00.00.00.00.00.0 0.00.00.00.00.00.00.00.00.00.00.00.00.00.0
|
|

06-28-2008, 09:54 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,609
|
|
|
You mean that, you want to write line by line, not in a same row? So write a new line in each case.
__________________
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.
|
|

06-28-2008, 01:49 PM
|
 |
Member
|
|
Join Date: Jun 2008
Posts: 23
|
|
Yes, how to make the answer of the calculated value in the output.txt like this?
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
|
|

06-28-2008, 02:09 PM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,609
|
|
Add the following line,
out.write(System.getProperty("line.separator"));
after the line,
out.write(Temp.substring(4));
in your code.
__________________
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.
|
|

06-28-2008, 02:28 PM
|
 |
Member
|
|
Join Date: Jun 2008
Posts: 23
|
|
Hi, Eranga. Still the same. I get this in the output.txt file after run.
5.0830693069306940.00.00.00.00.00.00.00.00.00.00.0 0.00.00.00.00.00.00.00.00.00.00.00.00.00.0
I wana make the calculated value to be save into the output.txt file,
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
I have done some research on array, but cannot get it.
Please guide the problems. Thanks
|
|

06-28-2008, 02:33 PM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,609
|
|
|
Show me your new code.
__________________
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.
|
|

06-28-2008, 02:49 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;
final double y[]= new double[PCOUNT];
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.write(Temp.substring(4));
out.write(System.getProperty("line.separator"));
out.close();
}catch (Exception A){}
}
}
I have added the code after that, but still the same at my C:\output.txt
which I get tis answers is not correct:
5.0830693069306940.00.00.00.00.00.00.00.00.00.00.0 0.00.00.00.00.00.00.00.00.00.00.00.00.00.0
|
|

06-29-2008, 07:06 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,609
|
|
|
First check that you logic is working fine or not. I can see in your code, only the first time do the calculation and after that nothing is changed. Actually the above output is not what you get, isn't it? You get a output stream with an annoying character like '[]', isn't it? Error is not with the new line print, it's an error with your application logic.
__________________
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.
|
|

06-30-2008, 04:48 AM
|
|
Senior Member
|
|
Join Date: May 2008
Location: Makati, Philippines
Posts: 228
|
|
|
hehehe i got your problem. I used \n for next line. if you used notepad to open the file you would see a straigth line. try using Wordpad ^_^ Wordpad can interpret text formatting such as \n. ^_^
Now for the zero output. There is just one wrong variable in the logic. you just have to look for it. Its a simple error ^_^ Try finding it ^_^
HINT: It is inside the while loop
__________________
Mind only knows what lies near the heart, it alone sees the depth of the soul.
|
|

06-30-2008, 05:54 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,609
|
|
Originally Posted by Eku
HINT: It is inside the while loop
You got the point. Let see our friend can solve it out.
__________________
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.
|
|

07-04-2008, 08:50 AM
|
 |
Member
|
|
Join Date: Jun 2008
Posts: 23
|
|
Hi, thanks Eku and Eranga. I got the right answers now.
The code part changed,
//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++;
j++;
}
I have one more problem is tat the calculated value is not in row by row.
All the output is in one very long row only.
3.764851485148515
3.8480198019801985.2494059405940616.89198019801980 23.90623762376237646.3472277227722782.858316831683 16844.99574257425742555.2577227722772285.199504950 4950493.88128712871287143.88128712871287143.781485 14851485155.2577227722772286.4636633663366346.8337 623762376245.4905940594059414.7961386138613864.326 2376237623765.0872277227722783.839702970297034.796 1386138613863.87712871287128775.29099009900990155. 083069306930694
0.0
Can you guide me how to make them in row by row like this?
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
|
|

07-04-2008, 09:18 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,609
|
|
Your data written part should be like this.
try{
FileWriter fw = new FileWriter("C:\\output.txt");
BufferedWriter out = new BufferedWriter(fw);
for(j=0; j<= i; j++) {
double Temp = 0;
Temp += y[j];
String temp = Double.toString(Temp);
out.write(temp);
out.write(s);
out.flush();
}
out.close();
fw.close();
}
catch (Exception A) {
}
__________________
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.
|
|

07-04-2008, 09:32 AM
|
 |
Member
|
|
Join Date: Jun 2008
Posts: 23
|
|
Hi Eranga, I get this error.
Testing.java:48: cannot find symbol
symbol : variable s
location: class Testing
out.write(s);
^
1 error
Where should I declare the variable "s"?
|
|

07-04-2008, 09:39 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,609
|
|
Ah, that's the new line character I used. Replace it with following.
__________________
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 post | | |