Results 1 to 6 of 6
Like Tree1Likes
  • 1 Post By KevinWorkman

Thread: What is the error?

  1. #1
    ahmedsameha1 is offline Member
    Join Date
    May 2014
    Posts
    32
    Rep Power
    0

    Default What is the error?

    Dear all,

    What is the error in this code?

    Java Code:
    import java.util.*;
    import java.lang.String;
    
    public class Storage{
    
    public static void main(String[] args){
    
    int storage (String s){
    return s.length() * 2;
    }
    
    
    String a="four";
    int x=storage(a);
    System.out.println(x);
    }
    }
    The compiler is reporting missing 2 ";" in line 9.

    Thank you.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,143
    Rep Power
    15

    Default Re: What is the error?

    You can't have methods inside other methods like that. Put your storage() method in its own block outside of the main() method.

    Proper indentation would help you spot errors like this.
    How to Ask Questions the Smart Way
    Static Void Games - GameDev tutorials, free Java and JavaScript hosting!
    Static Void Games forum - Come say hello!

  3. #3
    ahmedsameha1 is offline Member
    Join Date
    May 2014
    Posts
    32
    Rep Power
    0

    Default Re: What is the error?

    Thank you Kevin, Must I make storage() static?

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,143
    Rep Power
    15

    Default Re: What is the error?

    Depends on how you want to use it. If you want to be able to call it from the main() method, the short answer is yes.

    The longer answer is that you need to learn what it means for a method (or variable) to be static and then decide based on that.
    ahmedsameha1 likes this.
    How to Ask Questions the Smart Way
    Static Void Games - GameDev tutorials, free Java and JavaScript hosting!
    Static Void Games forum - Come say hello!

  5. #5
    ahmedsameha1 is offline Member
    Join Date
    May 2014
    Posts
    32
    Rep Power
    0

    Default Re: What is the error?

    Thank you Kevin, I have made an instance of object Storage. I have started to understand the using of "static" keyword. Here you are what I did.
    Java Code:
    import java.util.*;
    import java.lang.String;
    
    public class Storage{
    int storage (String s){
    return s.length() * 2;
    }
    public static void main(String[] args){
    
    Storage j=new Storage();
    
    
    String a="four";
    int x=j.storage(a);
    System.out.println(x);
    }
    }

  6. #6
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,143
    Rep Power
    15

    Default Re: What is the error?

    Nice, glad you got it working. You still might want to use proper indentation though.
    How to Ask Questions the Smart Way
    Static Void Games - GameDev tutorials, free Java and JavaScript hosting!
    Static Void Games forum - Come say hello!

Similar Threads

  1. Replies: 8
    Last Post: 11-22-2012, 08:16 AM
  2. Replies: 3
    Last Post: 05-08-2012, 06:26 PM
  3. Replies: 1
    Last Post: 12-05-2009, 08:28 AM
  4. Replies: 3
    Last Post: 01-23-2009, 09:53 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
  •