#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2007
    Posts
    7
    Rep Power
    0

    Trying to call a private void method within a constructor.


    Hello everyone!
    I am having some difficulty trying to use the String of a file name and path and transfer it into a String array.

    So I am trying to get a String of the name and path (String d, in the PlayWords constructor), then call the loadDictionary method to put that String into the String [] dictionary located in the instance variables.

    But the loadDictionary method is a private void, and I am not advised to change it.

    I've tried many times, but I am unsure about how to call private void methods within another method.


    Code:
    /** 
     * The PlayWords class, which contains data and methods need to run the game.
     */
    public class PlayWords {
      
      /** An array of words representing a dictionary. **/
      private static String [] dictionary;
      
      /** Random number generator. */
      private static final Random r = new Random();
    
      /** The two-dimensinonal array of characters representing the grid layout. */
      private char [][] grid;
    
    
      public PlayWords(char [][] g, String d) {
    
      ?? how do I declare the loadDictionary method with String d as the parameter??
    I keep getting incompatiable types, found:void; require: String;
    
        this.grid = g;
        
        for(int row = 0; row < grid.length; row++) {
          for(int col = 0; col < grid.length; col++) {
            grid[row][col] = getRandomLetter();
          }
        }
      }
    
    /**
       * Load the dictionary from a file of words.
       * @param dictFile The name of the file that contains the dictionary.
       * @throws IOException
       */
      private void loadDictionary(String dictFile) throws IOException {
        FileReader file = new FileReader(dictFile);
        BufferedReader buffer = new BufferedReader(file);
        int numLines = 0;
        String line = buffer.readLine();
        while (line != null) {
          numLines++;
          line = buffer.readLine();
        }
        buffer.close();
        
        dictionary = new String [numLines];
        BufferedReader buffer2 = new BufferedReader(new FileReader(dictFile));
        line = buffer2.readLine();
        for (int i = 0; i < numLines; i++) {
          dictionary[i] = line;
          line = buffer2.readLine();
        }
      }
    Last edited by hbab; March 24th, 2007 at 02:01 PM. Reason: java code
  2. #2
  3. <?PHP user_title("gimp"); ?>
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2005
    Location
    Internet
    Posts
    7,652
    Rep Power
    6084
    A private method is one not accessible from other classes. If class A has private method X(), A.X() can be called from any instance of A, including subclasses (class B extends A).

    So if A has private X() and constructor A(), why not call it from A()?

    JAVA Code:
    public class A {
         public A() {
              X();
         }
         private void X() {
              doSomething();
         }
    }


    Next time, you should try something before thinking it's a problem... don't invent problems where there are none!

    Using CODE or HIGHLIGHT=JAVA tags is nice, too. See how they make my code neat?

    Oh, and welcome to the forums.
    Chat Server Project & Tutorial | WiFi-remote-control sailboat (building) | Joke Thread
    Rational thinkers deplore the excesses of democracy; it abuses the individual and elevates the mob. The death of Socrates was its finest fruit.
    Use XXX in a comment to flag something that is bogus but works. Use FIXME to flag something that is bogus and broken. Use TODO to leave yourself reminders. Calling a program finished before all these points are checked off is lazy.
    -Partial Credit: Sun

    If I ask you to redescribe your problem, it's because when you describe issues in detail, you often get a *click* and you suddenly know the solutions.
    Ches Koblents
  4. #3
  5. Feelin' Groovy
    Devshed Supreme Being (6500+ posts)

    Join Date
    Aug 2001
    Location
    WDSMIA
    Posts
    10,135
    Rep Power
    5054
    Just call the private method. Example:
    Java Code:
    class Foo {
     
        public static void main(String[] args) {
            new Foo().somePublicMethod();
        }
     
        public void somePublicMethod() {
            System.out.printf("%s is calling a private method.\n", this.getClass());
            somePrivateMethod("bar");
        }
     
        private void somePrivateMethod(String arg) {
            System.out.printf("Called private method with arg -> \"%s\"!\n", arg);
        }
     
    }

    Output:
    Code:
    class Foo is calling a private method.
    Called private method with arg -> "bar"!
    Cheers!

    ~
    Yawmark
    class Sig{public static void main(String...args){\u0066or(int
    \u0020$:"v\"ʲ\"vΤ\"".to\u0043h\u0061rArray()
    )System./*goto/*$/%\u0126//^\u002A\u002Fout.print((char)(($>>
    +(~'"'&'#'))+('<'>>('\\'/'.')/\u002Array.const(~1)\*\u002F)));}}
  6. #4
  7. Feelin' Groovy
    Devshed Supreme Being (6500+ posts)

    Join Date
    Aug 2001
    Location
    WDSMIA
    Posts
    10,135
    Rep Power
    5054
    If class A has private method X(), A.X() can be called from any instance of A, including subclasses (class B extends A).
    Absolutely not. Private methods are not inherited. Class B, which extends A, cannot call A's private methods.

    ~
    Yawmark
    class Sig{public static void main(String...args){\u0066or(int
    \u0020$:"v\"ʲ\"vΤ\"".to\u0043h\u0061rArray()
    )System./*goto/*$/%\u0126//^\u002A\u002Fout.print((char)(($>>
    +(~'"'&'#'))+('<'>>('\\'/'.')/\u002Array.const(~1)\*\u002F)));}}
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2007
    Posts
    7
    Rep Power
    0
    I've tried to call the private method, but I keep getting incompatible types (found:void; required: java.lang.String;

    I've tried making the [public PlayWords] void but it still doesn't work out.
  10. #6
  11. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jan 2004
    Location
    Mobile, Alabama
    Posts
    4,021
    Rep Power
    1285
    Originally Posted by hbab
    I've tried to call the private method, but I keep getting incompatible types (found:void; required: java.lang.String;

    I've tried making the [public PlayWords] void but it still doesn't work out.
    Can you show the way you called it?
  12. #7
  13. <?PHP user_title("gimp"); ?>
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2005
    Location
    Internet
    Posts
    7,652
    Rep Power
    6084
    Originally Posted by Yawmark
    Absolutely not. Private methods are not inherited. Class B, which extends A, cannot call A's private methods.

    ~
    Really? Huh. My bad.
    Chat Server Project & Tutorial | WiFi-remote-control sailboat (building) | Joke Thread
    Rational thinkers deplore the excesses of democracy; it abuses the individual and elevates the mob. The death of Socrates was its finest fruit.
    Use XXX in a comment to flag something that is bogus but works. Use FIXME to flag something that is bogus and broken. Use TODO to leave yourself reminders. Calling a program finished before all these points are checked off is lazy.
    -Partial Credit: Sun

    If I ask you to redescribe your problem, it's because when you describe issues in detail, you often get a *click* and you suddenly know the solutions.
    Ches Koblents
  14. #8
  15. <?PHP user_title("gimp"); ?>
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2005
    Location
    Internet
    Posts
    7,652
    Rep Power
    6084
    Originally Posted by hbab
    I've tried to call the private method, but I keep getting incompatible types (found:void; required: java.lang.String;

    I've tried making the [public PlayWords] void but it still doesn't work out.
    You're probably calling it as "someVar = thatMethod()" which means it should return something.

    Comments on this post

    • Yawmark agrees
    Chat Server Project & Tutorial | WiFi-remote-control sailboat (building) | Joke Thread
    Rational thinkers deplore the excesses of democracy; it abuses the individual and elevates the mob. The death of Socrates was its finest fruit.
    Use XXX in a comment to flag something that is bogus but works. Use FIXME to flag something that is bogus and broken. Use TODO to leave yourself reminders. Calling a program finished before all these points are checked off is lazy.
    -Partial Credit: Sun

    If I ask you to redescribe your problem, it's because when you describe issues in detail, you often get a *click* and you suddenly know the solutions.
    Ches Koblents
  16. #9
  17. Feelin' Groovy
    Devshed Supreme Being (6500+ posts)

    Join Date
    Aug 2001
    Location
    WDSMIA
    Posts
    10,135
    Rep Power
    5054
    Really?
    Absolutely. Per the JLS:

    8.2 Class Members: Members of a class that are declared private are not inherited by subclasses of that class.

    ~
    Yawmark
    class Sig{public static void main(String...args){\u0066or(int
    \u0020$:"v\"ʲ\"vΤ\"".to\u0043h\u0061rArray()
    )System./*goto/*$/%\u0126//^\u002A\u002Fout.print((char)(($>>
    +(~'"'&'#'))+('<'>>('\\'/'.')/\u002Array.const(~1)\*\u002F)));}}
  18. #10
  19. <?PHP user_title("gimp"); ?>
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2005
    Location
    Internet
    Posts
    7,652
    Rep Power
    6084
    Apologies, then. Slipped my mind - now that I think of it obviously you're right.
    Chat Server Project & Tutorial | WiFi-remote-control sailboat (building) | Joke Thread
    Rational thinkers deplore the excesses of democracy; it abuses the individual and elevates the mob. The death of Socrates was its finest fruit.
    Use XXX in a comment to flag something that is bogus but works. Use FIXME to flag something that is bogus and broken. Use TODO to leave yourself reminders. Calling a program finished before all these points are checked off is lazy.
    -Partial Credit: Sun

    If I ask you to redescribe your problem, it's because when you describe issues in detail, you often get a *click* and you suddenly know the solutions.
    Ches Koblents
  20. #11
  21. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2007
    Posts
    7
    Rep Power
    0
    yeah I've been calling it like that, which is why i had trouble. I think I understand what to do now, so I'll try it out and post back tomorrow. Thank you everyone!
  22. #12
  23. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2007
    Posts
    7
    Rep Power
    0
    the main problem was that I didn't add 'throws IOException' to the constructor.

    oh man thats embarrassing

IMN logo majestic logo threadwatch logo seochat tools logo