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?

import java.io.IOException;
import java.util.Scanner; // must have this to take in input (cin)


public class Main {
    
    public Main() {
    }
    
    
    public static void main(String[] args) throws IOException {
        
        // Create scanner variable (cin variable)
        Scanner scan = new Scanner(System.in);
        
        System.out.println("\nWelcome to the Interest Calculator");
        
        char decision = 'y';
        
        while (decision == 'y' || decision == 'Y')   // cin >> decision
        {
            System.out .println("\nEnter loan amount:   ");
            float loan = scan.nextFloat();
            
            System.out.println("Enter interest rate: ");
            float rate = scan.nextFloat();
            
            System.out.println("\nLoan amount:         $" + loan);
            System.out.println("Interest rate:       " + (rate*100) + "%");
            
            float interest = (loan*rate);
            
            System.out.println("Interest:            $" + interest);
            
            System.out.println("Continue? (y/n): ");
            decision = (char)System.in.read();
        }
    }
    
    public String alter(float n) {
        /// Pre: variable n must be of type float
        /// Post: return loan amount as a string with commas inserted
        
        String number = Float.toString(number); // ERROR HERE convert float to string;
        
        number.length();
        
        return number;
    }
    
}

Dani AI

Generated

The compile error is coming from a simple name/scope mistake inside alter(float n): the method declares the parameter n but your conversion call uses an undefined name (number in the original snippet). The compiler will complain with messages like "cannot find symbol: variable number" or "variable number might not have been initialized." Fix that by converting the actual parameter, and remember alter is an instance method — either call it from main with new Main().alter(loan) or make alter static.

Both and point in the right directions: you can convert a primitive float to a string directly, or use the wrapper if you really want an object. Prefer the lightweight static/formatting approaches for clarity and performance, and avoid creating a wrapper object just to call toString().

If your goal is "a loan amount as a string with commas and fixed decimals," formatting classes are the easiest and most robust options. Examples:

DecimalFormat df = new DecimalFormat("#,##0.00");
String formatted = df.format(n);
String formatted = String.format(Locale.US, "%,.2f", n);
NumberFormat cf = NumberFormat.getCurrencyInstance(Locale.US);
String money = cf.format(n);

A few additional tips: don’t mix low-level System.in.read() with Scanner — leftovers like newline characters make input behave oddly. To read a single choice with Scanner, read the next token and take its first char, for example:

String ans = scan.next();
char decision = ans.charAt(0);

These changes will fix the compile error and give you clean, locale-aware formatting for monetary values.

Recommended Answers

All 2 Replies

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:

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.

I always use the static methods of the String Object.

String number = String.valueOf(n);

You can also use that for various other primitive types like int, long, char, double etc.

commented: Much neater than my suggestion :) +3
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.