Results 1 to 3 of 3
Thread: Error assistance
- 11-21-2009, 09:51 PM #1
Member
- Join Date
- Nov 2009
- Posts
- 8
- Rep Power
- 0
Error assistance
I'm writing a part of my code but i have an error either way i write it this is part of the programming im writing. the error lies in that if s is string like it is now return s is the error because of incompatible types. while if i switch it to int i get three errors. How would i fix this so it will run? ( this is only part of the whole program)
public static int leftpad (int value, int width)
{ String s; // String to be returned
int m; // length of s
DecimalFormat fmt = new DecimalFormat("0");
// convert value to a String with two decimal place
s = fmt.format(value);
// determine the length of s
m = s.length();
// pad s by spaces on the left so that the resulting length of s is width
for (int i = 0; i < width - m; i++)
{ s = " " + s;} // one space between the " "
return s;
}
} // end of class
-
You can't left pad an int as it just doesn't make sense. Have the method return a String, and most importantly, declare that the method will return a String:
Java Code:// not this: // public static int leftpad (int value, int width) { // but this: public static String leftpad (int value, int width) {
- 11-21-2009, 10:54 PM #3
Member
- Join Date
- Nov 2009
- Posts
- 8
- Rep Power
- 0
Similar Threads
-
Looking for assistance
By s_dawg101 in forum New To JavaReplies: 32Last Post: 11-04-2009, 02:49 AM -
New to the forum + assistance :)
By quemadissimo in forum New To JavaReplies: 4Last Post: 10-31-2009, 06:41 AM -
In need of some assistance
By Boer84 in forum New To JavaReplies: 2Last Post: 07-08-2008, 04:14 PM -
X-Tremely new to this...Need assistance...
By Johnny562 in forum New To JavaReplies: 5Last Post: 07-01-2008, 09:17 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks