English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

Here's my problem - I'm required to create a program using a simplistic form of float formating that is by the words of my professor and textbook, included in the Javax.swing class. The exact syntax used is String.format("%.2f" + floatName) which is supposed to reduce said float to output to two decimal places.

Unfortunately, my compiler (JBuilder 2005) is hellbent on not recognizing the format method. The above class has been imported in its entireity, still, it refuses to recognize the method. The syntax and importations match perfectly with the reference book, and frankly, I'm runing out of ideas and time.

How can I get this method to work, or baring that, what would be an alternative to getting my floats formated?

2007-02-07 09:30:59 · 3 answers · asked by Shawn L 2 in Computers & Internet Programming & Design

Thanks for trying, Milciades P, but no.

2007-02-07 11:14:40 · update #1

3 answers

I believe your problem is that you are trying to concatenate the format string with the float (auto-conversion to String) by using the "+" operator within the argument to format(). The correct form of the method requires you pass 2 or more arguments, not just one. The first is a String representing the format pattern. So, try doing it like this:
String.format("%.2f", floatName);

Notice the comma following the formatting pattern string.

Also, this is a Java 5 method, so make sure JBuilder is using Java 5.

2007-02-09 12:58:41 · answer #1 · answered by vincentgl 5 · 0 0

I’d try
import java.io.*;
import java.util.*;
I’m not really sure but I’d bet that the format method you’re trying to use is not under java.swing try this ones.

2007-02-07 10:17:13 · answer #2 · answered by Milciades P 1 · 0 0

public static void main (String []args) { String input = "aAAcdkljabA"; String replaced = ""; for(char c: input.toCharArray()) { if(Character.toLowerCase(c) == 'a') { replaced += 'b'; }else { replaced += c; } } System.out.println(replaced); }

2016-03-29 09:58:06 · answer #3 · answered by ? 4 · 0 0

fedest.com, questions and answers