AppletTalk.com Forum Index AppletTalk.com
Java discussions newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

StringTokenizer to.. double Array[ ]

 
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java Help
View previous topic :: View next topic  
Author Message
Dimitris
Guest





PostPosted: Sun Mar 27, 2005 12:40 pm    Post subject: StringTokenizer to.. double Array[ ] Reply with 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.

Back to top
Anthony Borla
Guest





PostPosted: Sun Mar 27, 2005 9:11 pm    Post subject: Re: StringTokenizer to.. double Array[ ] Reply with quote



"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





PostPosted: Tue Apr 12, 2005 6:44 pm    Post subject: Re: StringTokenizer to.. double Array[ ] Reply with quote



Thank you Anthony.
I think I understand the mistake.

:-) Smile Smile Smile Smile Smile Smile Smile Smile Smile Smile :-)

Back to top
Display posts from previous:   
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java Help All times are GMT
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.