The Artima Developer Community
Sponsored Link

Java Answers Forum
Help,,How can i read data from file to int or float???

2 replies on 1 page. Most recent reply: Nov 27, 2003 3:02 PM by Charles Bell

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
thanawat

Posts: 5
Nickname: follownet
Registered: Nov, 2003

Help,,How can i read data from file to int or float??? Posted: Nov 25, 2003 6:35 PM
Reply to this message Reply
Advertisement
I am new on Java.
Now i trying to read data from file by using

while ((line=inFile.readLine()) !=null)
{
datas.addElement(line);
}

From this i can get data to display like

System.out.print(datas.elementAt(2));
it show data OK.
But i need some thing like

a=datas.elementAt(1)+datas.elementAt(2);

I cannot get this command work.

But my data is float(5.6) so i also need to use my data in calculation. i cannot convert it to int/float?

How to do this???

Thank


Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: Help,,How can i read data from file to int or float??? Posted: Nov 26, 2003 5:16 AM
Reply to this message Reply
Sure it doesn't work.

Did you ever look at the readLine() - Syntax in the Java API?
This Method only delivers Strings (you ARE using a BufferedReader of some type, right?).

a=datas.elementAt(1)+datas.elementAt(2);

would not even work if readLine would deliver something else than String, because you obviously store the data in a Vector.
A Vector can't store primitive Datatypes like float, int, double, only Classes extending the class Object (like Float, Integer, Double, String, ... = everything except primitive datatypes).
The Method getElementAt() delivers your data as Object, to do something useful with it, you have to cast it to it's original Cass type.

Your file does NOT contain 5.6, but "5.6"

What you have to do?
1. To get something useful from the Vector, you must convert the term to String:
(String)datas.elementAt(1) //delivers for example the String "1.333" wich is not equal to 1.333

2. You have to PARSE the text you got from the file, wich in this case would be:
Float.parseFloat ((String)datas.elementAt(1))

Your addition example looks would look like this:
float a = Float.parseFloat ((String)datas.elementAt(1)) + Float.parseFloat ((String)datas.elementAt(2));

If your Vector contains only Strings with float-Values, you can use the following method to convert it do an array of floats.

private float[] toArray (Vector v) throws NumberFormatException {
float a[] = new float[v.size()];
for (int i = c.size()-1; i >= 0; i--)
a = Float.parseFloat ((String)datas.elementAt(i));
}

There is no error correction in this method (it assumes every value is String and can be parsed to float), but you WILL have to implement it.


Call

float f[];
try {
f = toArray(datas);
} catch (NumberformatException e) {
//do something useful
}

The try-catch Code is to catch Errors (TRY , if [Exception] occurs, execute the code in CATCH).
Now you can access your float Values with
f[i]
float a = f[1] + f[2];

Charles Bell

Posts: 519
Nickname: charles
Registered: Feb, 2002

Re: Help,,How can i read data from file to int or float??? Posted: Nov 27, 2003 3:02 PM
Reply to this message Reply
Your variable line must be referring to a String object
line=inFile.readLine()

and the object you call datas is storing string objects when you execute the line:

datas.addElement(line);

so your code line System.out.print(datas.elementAt(2));
is displaying the String object at datas index of 2 or the third element.

The line:

a=datas.elementAt(1)+datas.elementAt(2);

is concatenating two string objects, datas.elementAt(1) and datas.elementAt(2) into a third string object.
This is like "banana"="ba"+"nana"

If you have declared the variable a as a String it will compile and run.

If a is a float then you are going to get a compile error and you have to convert your string object into a float primitive using the static string parser method of the Float class, while checking for a parse exception.
float a  = 0;
float firstNumber = 0;
float secondNumber = 0;
try{
    firstNumber = Float.parseFloat(datas.elementAt(1));      
secondNumber = Float.parseFloat(datas.elementAt(2));
a = firstNumber  + secondNumber;
 
}catch(NumberFormatException nfe){
    System.err.println("Either the first or second number string will not convert to a float.");
}
 


I am assuming you mean float(5.6) as the floating point number 5.6 and not some string formatting thing like is used with FORTRAN print and read statements. (I go way back in computers!)


I hope I have explained it okay.
If not try me again. You can get me email by rght clicking my name. Just don't send me your homework assignments.

Flat View: This topic has 2 replies on 1 page
Topic: Database Previous Topic   Next Topic Topic: Scheduled Task Servlet

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use