 |
AppletTalk.com Java discussions newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Dimitris Guest
|
Posted: Sun Mar 27, 2005 12:40 pm Post subject: StringTokenizer to.. double Array[ ] |
|
|
I am reading a .txt file, and trying to store each double number to an
array for further calculations.
I have problems adding the numbers to the array.
Here is what i am trying to do:
public void read(File afile) {
FileInputStream fInpStr = null;
BufferedReader bRead = null;
StringTokenizer strTok = null;
String line = null;
String next = null;
String[] jim;
int i = 0;
//temporary variables
String variableName;
double variableValue;
//open the variable text file and catch any error.
try {
fInpStr = new FileInputStream(afile);
}
catch (FileNotFoundException e) {
JOptionPane.showMessageDialog(null, "File not found " + afile,
"Error",
JOptionPane.ERROR_MESSAGE);
return;
}
variableName = "";
variableValue = 0.0;
try {
bRead = new BufferedReader(new InputStreamReader(fInpStr));
//first line
variableName = bRead.readLine();
while (strTok.hasMoreTokens()) {
}
// second line
line = bRead.readLine();
fInpStr.close();
}
catch (IOException e) {
System.out.println(e.toString());
JOptionPane.showMessageDialog(null, "Error reading file " +
afile,
"Error",
JOptionPane.ERROR_MESSAGE);
}
} //end of read
_______________________________________
this is the code so far for reading the values. I think i have to add a
for loop to "loop" through the array, but i am cofused.
|
|
| Back to top |
|
 |
Anthony Borla Guest
|
Posted: Sun Mar 27, 2005 9:11 pm Post subject: Re: StringTokenizer to.. double Array[ ] |
|
|
"Dimitris" <dimitris123 (AT) gmail (DOT) com> wrote
| Quote: | I am reading a .txt file, and trying to store each double
number to an
array for further calculations.
I have problems adding the numbers to the array.
Here is what i am trying to do:
public void read(File afile) {
FileInputStream fInpStr = null;
BufferedReader bRead = null;
StringTokenizer strTok = null;
String line = null;
String next = null;
String[] jim;
int i = 0;
//temporary variables
String variableName;
double variableValue;
//open the variable text file and catch any error.
try {
fInpStr = new FileInputStream(afile);
}
catch (FileNotFoundException e) {
JOptionPane.showMessageDialog(null, "File not found "
+ afile,
"Error",
JOptionPane.ERROR_MESSAGE);
return;
}
variableName = "";
variableValue = 0.0;
try {
bRead = new BufferedReader(new
InputStreamReader(fInpStr));
//first line
variableName = bRead.readLine();
while (strTok.hasMoreTokens()) {
}
// second line
line = bRead.readLine();
fInpStr.close();
}
catch (IOException e) {
System.out.println(e.toString());
JOptionPane.showMessageDialog(null,
"Error reading file " + afile,
"Error",
JOptionPane.ERROR_MESSAGE);
}
} //end of read
this is the code so far for reading the values. I think i have
to add a for loop to "loop" through the array, but i
am cofused.
|
The code below - though incomplete - 'try' block is closer to what may be
needed. Some recommendations:
* Look more closely at error handling - after printing a message,
what are you going to do ?
* Consider using a resizable container to store your data. As
should be clear from the example below, arrays are of fixed
size. Using a resizable container will also make adding elements
easier as less 'checking code' is needed
* If more complex tokenising is needed consider using
'StreamTokenizer'
I hope this helps.
Anthony Borla
// *** Code is pre-1.5
try {
bRead = new BufferedReader(new
InputStreamReader(fInpStr));
final int MAX_ENTRIES = 128;
double[] dArray = new double[MAX_ENTRIES];
while ((line = bRead.readLine()) != null)
{
strTok = new StringTokenizer(line);
while (strTok.hasMoreTokens()) {
// *** Attempt token conversion, and add
// to your array / container
try
{
if (i < MAX_ENTRIES)
{
dArray[i] =
Double.parseDouble(strTok.nextToken());
}
else
{
// *** Array size exceeded
;
}
i++;
}
catch (NumberFormatException e) {
System.out.println(e.toString());
JOptionPane.showMessageDialog(null,
"Error converting data", "Error",
JOptionPane.ERROR_MESSAGE);
System.exit(1);
}
System.out.println(strTok.nextToken());
}
}
fInpStr.close();
}
|
|
| Back to top |
|
 |
Dimitris Guest
|
Posted: Tue Apr 12, 2005 6:44 pm Post subject: Re: StringTokenizer to.. double Array[ ] |
|
|
Thank you Anthony.
I think I understand the mistake.
:-) :-)
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|