-
January 12th, 2009, 02:45 PM
#1
Converting String to float
Hi everyone.
I need help again lol, im trying to code a load function, ive got it to read the files and put the values into strings, but i need to convert these to float type and nothing i try seems to work!
Code:
//Loads players saved statistics
void loadFile()
{
try {
FileReader file = new FileReader("winstat.dat");
BufferedReader buff = new BufferedReader(file);
String line = buff.readLine();
trans1 = line;
buff.close();
}
catch (IOException e) {
System.out.println("Error -- " + e.toString());
}
try {
FileReader file = new FileReader("loosestat.dat");
BufferedReader buff = new BufferedReader(file);
String line = buff.readLine();
trans2 = line;
buff.close();
}
catch (IOException e) {
System.out.println("Error -- " + e.toString());
}
try {
FileReader file = new FileReader("drawstat.dat");
BufferedReader buff = new BufferedReader(file);
String line = buff.readLine();
trans3 = line;
buff.close();
}
catch (IOException e) {
System.out.println("Error -- " + e.toString());
}
message.setText(trans1);
}
The files contain single float numbers, e.g.: 6.0
Thanks for any help!
-
January 12th, 2009, 03:06 PM
#2
Re: Converting String to float
Primitive types have wrapper classes, such as Float for float. Each of numberlike ones have parsing methods, that turn a suitable String to that type of number, eg. Float.parseFloat(...)
-
January 12th, 2009, 03:21 PM
#3
Re: Converting String to float
Ok, im not sure i fully understand, ive tried this before:
Code:
Float f = new Float(trans1);
winStat = f;
This compiles but winStat remains empty, although trans1 holds the value "6.0"
-
January 12th, 2009, 03:31 PM
#4
Re: Converting String to float
That should work unless there are extra spaces / values in your string. Try using myString.trim() to get rid of any extra spaces.
Also, there are two other ways to use the Float class to convert a string to a float. Please read the API and you will see them.
-
January 12th, 2009, 03:41 PM
#5
Re: Converting String to float
Ok ive now tried this as well:
Code:
trans1.trim();
Float f = new Float(trans1);
winStat = f.floatValue();
Code:
trans1.trim();
Float f = new Float(trans1);
winStat = f.valueOf(trans1);
Still coming up empty
-
January 12th, 2009, 03:49 PM
#6
Re: Converting String to float
When you say winStat is coming up empty what exactly do you mean? ie how have you determined it is empty and what type is winStat? ie is it a float or a Float.
Last edited by keang; January 12th, 2009 at 05:43 PM.
Reason: typo
-
January 12th, 2009, 03:53 PM
#7
Re: Converting String to float
Print out the value of trans1 please. I have a feeling you have something in there that isn't valid. I ran the following program with the following results so I know that it isn't Java or the Float class:
Code:
public class ConvertStringToFloat {
public static void main(String[] args) {
String number = "8945.342";
Float f1 = new Float(number);
System.out.println("f1: " + f1.floatValue());
float f2 = Float.valueOf(number);
System.out.println("f2: " + f2);
float f3 = Float.parseFloat(number);
System.out.println("f3: " + f3);
}
}
Code:
f1: 8945.342
f2: 8945.342
f3: 8945.342
Paste the code for the winstat variable and what it is supposed to do.
-
January 12th, 2009, 05:49 PM
#8
Re: Converting String to float
Originally Posted by ProgramThis
Print out the value of trans1 please. I have a feeling you have something in there that isn't valid
I agree we need more info from the OP but if trans1 is not valid then trying to convert it to a float will result in a NumberFormatException being thrown. Of course, I suppose it's possible the OP is catching and ignoring exceptions and so doesn't even know an exception has been raised or does know and has just neglected to tell us.
-
January 12th, 2009, 06:18 PM
#9
Re: Converting String to float
Okay, trans1 is definatly holding "6.0"
Heres my whole project:
http://cid-3ce0ac785929e198.skydrive...kjackswing.zip
So have a look yourself guys
loadFile function starts on line 532, the main class where the problem is, is blackjack.java
winStat is just a float which holds the number of wins the player has
No errors at all are being thrown
Last edited by boburob; January 12th, 2009 at 06:25 PM.
-
January 12th, 2009, 08:38 PM
#10
Re: Converting String to float
Ok apologies, i have been working with swing, and its not showing the stats on swing however they are being uploaded! Im not sure why there not showing up on my swing program though
-
January 12th, 2009, 08:38 PM
#11
Re: Converting String to float
omg i just realised the problem...i am an idiot! I have been using the wrong variables, sorry for wasting all your time
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|