Results 1 to 3 of 3
  1. #1
    bobbychiken is offline Member
    Join Date
    Nov 2009
    Posts
    8
    Rep Power
    0

    Default 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

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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) {

  3. #3
    bobbychiken is offline Member
    Join Date
    Nov 2009
    Posts
    8
    Rep Power
    0

Similar Threads

  1. Looking for assistance
    By s_dawg101 in forum New To Java
    Replies: 32
    Last Post: 11-04-2009, 03:49 AM
  2. New to the forum + assistance :)
    By quemadissimo in forum New To Java
    Replies: 4
    Last Post: 10-31-2009, 07:41 AM
  3. In need of some assistance
    By Boer84 in forum New To Java
    Replies: 2
    Last Post: 07-08-2008, 05:14 PM
  4. X-Tremely new to this...Need assistance...
    By Johnny562 in forum New To Java
    Replies: 5
    Last Post: 07-01-2008, 10:17 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •