CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Feb 2008
    Posts
    24

    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!

  2. #2
    Join Date
    Apr 2007
    Posts
    442

    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(...)

  3. #3
    Join Date
    Feb 2008
    Posts
    24

    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"

  4. #4
    Join Date
    Feb 2008
    Posts
    966

    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.

  5. #5
    Join Date
    Feb 2008
    Posts
    24

    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

  6. #6
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    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

  7. #7
    Join Date
    Feb 2008
    Posts
    966

    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.

  8. #8
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Converting String to float

    Quote 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.

  9. #9
    Join Date
    Feb 2008
    Posts
    24

    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.

  10. #10
    Join Date
    Feb 2008
    Posts
    24

    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

  11. #11
    Join Date
    Feb 2008
    Posts
    24

    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
  •  





Click Here to Expand Forum to Full Width

Featured