Convert float to string gets error

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Apr 2009
Posts: 149
Reputation: gretty is an unknown quantity at this point 
Solved Threads: 7
gretty gretty is offline Offline
Junior Poster

Convert float to string gets error

 
0
  #1
Oct 10th, 2009
Hello

I'm new to java & I am trying to convert a float to a string in my simple program. But I get an error on that line. Can you tell me why or what I need to do to fix it?

Maybe I haven't imported the right things or sumthing?

  1. import java.io.IOException;
  2. import java.util.Scanner; // must have this to take in input (cin)
  3.  
  4.  
  5. public class Main {
  6.  
  7. public Main() {
  8. }
  9.  
  10.  
  11. public static void main(String[] args) throws IOException {
  12.  
  13. // Create scanner variable (cin variable)
  14. Scanner scan = new Scanner(System.in);
  15.  
  16. System.out.println("\nWelcome to the Interest Calculator");
  17.  
  18. char decision = 'y';
  19.  
  20. while (decision == 'y' || decision == 'Y') // cin >> decision
  21. {
  22. System.out .println("\nEnter loan amount: ");
  23. float loan = scan.nextFloat();
  24.  
  25. System.out.println("Enter interest rate: ");
  26. float rate = scan.nextFloat();
  27.  
  28. System.out.println("\nLoan amount: $" + loan);
  29. System.out.println("Interest rate: " + (rate*100) + "%");
  30.  
  31. float interest = (loan*rate);
  32.  
  33. System.out.println("Interest: $" + interest);
  34.  
  35. System.out.println("Continue? (y/n): ");
  36. decision = (char)System.in.read();
  37. }
  38. }
  39.  
  40. public String alter(float n) {
  41. /// Pre: variable n must be of type float
  42. /// Post: return loan amount as a string with commas inserted
  43.  
  44. String number = Float.toString(number); // ERROR HERE convert float to string;
  45.  
  46. number.length();
  47.  
  48. return number;
  49. }
  50.  
  51. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 796
Reputation: darkagn has a spectacular aura about darkagn has a spectacular aura about darkagn has a spectacular aura about 
Solved Threads: 110
darkagn's Avatar
darkagn darkagn is offline Offline
Master Poster
 
1
  #2
Oct 10th, 2009
  1. String number = Float.toString(number);
I think what you are trying to do is convert the number n to a string yes?

What I would do is this:
  1. String number = new Float(n).toString();

Here I create a Float object from my float primitive, then convert it to a String. You have tried to call the toString method statically and then pass the variable number instead of n.
There are no stupid questions, only those too stupid to ask for help.
echo is a web developer's best friend.
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 120
Reputation: Jocamps is an unknown quantity at this point 
Solved Threads: 7
Jocamps's Avatar
Jocamps Jocamps is offline Offline
Junior Poster
 
1
  #3
Oct 11th, 2009
I always use the static methods of the String Object.
  1. String number = String.valueOf(n);

You can also use that for various other primitive types like int, long, char, double etc.
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC