Mr Plow
Posts: 18
Nickname: mrplow
Registered: Jun, 2003
|
|
Re: Reading values from an array
|
Posted: Jul 14, 2003 1:57 PM
|
|
I took a look at your try block code where you were trying to write output to a file.
Here are a couple of commments and some working code which I hope are helpful:
1) I believe that the statement:
String newData = new String("s3"+","+"s6"+","+"distance"); is not doing what you intend. If you surround an reference like s3 with double quoatation marks, the java compiler will interpet the identifier to be a String object rather than a reference to an object.
That is to say that if:
String s3 = "1", s6 = "2", distance = "3"; The way that you have written the statement, the value of String newData would be "s3,s6,distance". To get the desired value of "1,2,3" you need to write the statement like:
String newData = s3 + "," + s6 + ", " + distance;
2) To use the Java I/O classes correctly they must piggy-back on each others functionality. To demonstrate how this works for outputing text to a file, I have created a small "Tester" class below in which I simplified the code in your try/catch block.
import java.io.*;
public class Tester { public static void main(String[] args) { String s3 = "1", s6 = "2", distance = "3"; String outFileName = "out.txt";
try { String output = s3 + ", " + s6 + ", " + distance; PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(outFileName))); out.println(output); out.close(); } catch (IOException e) { System.err.println("IOException thrown:" + e ); } } };
|
|