Results 1 to 6 of 6
  1. #1
    Scivies's Avatar
    Scivies is offline Member
    Join Date
    Aug 2010
    Posts
    2
    Rep Power
    0

    Default Error when using String.format method

    Hi,
    Hopefully I've posted this question in the right forum. I'm new (Obviously) so please bare with me.

    I'm trying to create a subclass that contains a public String toString. But when trying to use the method "return String.format(...) I'm getting an error stating,
    " cannot find symbol,
    symbol: method format(java.lang.String,java.lang.String,doubl,jav a.lang.String,double)
    Location: class java.util.Scanner"

    I've imported a util.Scanner and created a constructor for it. I've also tried to use a util.toString and it still doesn't work. I've added the code for the subclass below. Any detials or help would be appreciated.


    Java Code:
    package program3;
    import java.util.Scanner;
    import java.util.*;
    import java.io.PrintStream;
    
    public class Total_RestockFee extends LP {
    
        Scanner scanner = new Scanner(System.in);
    
        private double RestockFee;
        private double Total;
    
        Total_RestockFee(String LPTitle, double LPItemNumber, double LPUnitPrice, double LPStockQty, double RestockFee, double Total){
            //explicit call to super
            super (LPTitle, LPItemNumber, LPUnitPrice, LPStockQty);
                    setTotal(Total);
                    setRestockFee(RestockFee);
                    }
    
        public double getRestockFee() {
            return RestockFee;
        }
    
        public void setRestockFee(double RestockFee) {
            this.RestockFee = (RestockFee * .05);
        }
    
        public double getTotal() {
            return Total;
        }
    
        public void setTotal(double Total) {
            this.Total = Total;
        }
    
        @Override
        public String toString(){
                return String.format("%s: %.2f: \n%s: %.2f \n","Restock Fee: $", RestockFee, "Total: $", Total) ; //String.format("%.2f\n%s: %.2f\n%s", "Restock Fee:$ ", RestockFee, "Total:$ ", Total);
        }
    
    }
    Thanks,
    Ben
    Last edited by Fubarable; 08-12-2010 at 10:10 PM. Reason: Moderator Edit: Code tags added

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

    Default

    String#format has only been around since Java 1.5. You're possibly compiling with 1.4 or at a setting of 1.4 compliance level.

  3. #3
    Scivies's Avatar
    Scivies is offline Member
    Join Date
    Aug 2010
    Posts
    2
    Rep Power
    0

    Default String.format error

    Hi,
    Thanks for the response. I looked at my Java and I'm showing JRE 1.6.0.21. Any additional ideas?

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

    Default

    Quote Originally Posted by Scivies View Post
    Hi,
    Thanks for the response. I looked at my Java and I'm showing JRE 1.6.0.21. Any additional ideas?
    This may not be enough info, and I'm not giving up on my idea so readily. How are you compiling it? Command line? If so, show your command line. IDE such as NetBeans or Eclipse? If so, then you had better check the IDE documentation to see how you set the Java compiler compliance level and if 1.4 or lower, increase it to 1.6.

  5. #5
    Prajin's Avatar
    Prajin is offline Senior Member
    Join Date
    Jun 2010
    Location
    Ktm, Nepal
    Posts
    120
    Rep Power
    0

    Default

    First if you have error message copy and paste don't write it.
    next,
    " cannot find symbol,
    symbol: method format(java.lang.String,java.lang.String,doubl,jav a.lang.String,double)
    Location: class java.util.Scanner"
    Look where you have called this method. Are you passing correct number of parameters? or method name is right? I mean looking at your 1st post, I can say this error has nothing to do with code that have you posted.

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,825
    Rep Power
    19

    Default

    I'm wondering why it's saying:
    Location: class java.util.Scanner

    rather than the class that's being compiled.

Similar Threads

  1. Text/String format..??
    By Neullson in forum New To Java
    Replies: 14
    Last Post: 07-23-2010, 08:51 AM
  2. Help with checking for a certain format in a String
    By SteroidalPsycho in forum New To Java
    Replies: 2
    Last Post: 03-26-2010, 04:56 AM
  3. format content of String[]
    By eponcedeleon in forum Advanced Java
    Replies: 9
    Last Post: 02-23-2010, 05:12 AM
  4. String.format not in javadoc
    By ortollj in forum New To Java
    Replies: 3
    Last Post: 11-08-2009, 05:14 PM
  5. Help with Format.justify method
    By lenny in forum AWT / Swing
    Replies: 1
    Last Post: 08-07-2007, 06:29 AM

Posting Permissions

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