944,167 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 2649
  • Java RSS
Oct 10th, 2009
0

Convert float to string gets error

Expand Post »
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?

java Syntax (Toggle Plain Text)
  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. }
Similar Threads
Reputation Points: 10
Solved Threads: 7
Junior Poster
gretty is offline Offline
158 posts
since Apr 2009
Oct 10th, 2009
1
Re: Convert float to string gets error
java Syntax (Toggle Plain Text)
  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:
java Syntax (Toggle Plain Text)
  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.
Reputation Points: 395
Solved Threads: 192
Veteran Poster
darkagn is offline Offline
1,136 posts
since Aug 2007
Oct 11th, 2009
1
Re: Convert float to string gets error
I always use the static methods of the String Object.
Java Syntax (Toggle Plain Text)
  1. String number = String.valueOf(n);

You can also use that for various other primitive types like int, long, char, double etc.
Reputation Points: 30
Solved Threads: 7
Junior Poster
Jocamps is offline Offline
120 posts
since Jun 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Illegal Start of type error
Next Thread in Java Forum Timeline: constructors?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC