Results 1 to 18 of 18
  1. #1
    SmokeyWoody is offline Member
    Join Date
    Oct 2014
    Posts
    36
    Rep Power
    0

    Default Calling one method to another method that are in the same class.

    I am trying to call a private method to another method that are located in the same class. I have many threads online but can't find anything that works for it so I am falling back here for better help. Any help is greatly appreciated.

    I am trying to call prepareString to the encode method below.

    For some reason when I tried to post my whole code it would just give me a page that said "Page not found" so gave the part of code that I need help with.



    Java Code:
    public class ShiftEncoderDecoder
       private String prepareString(String plainText)
       {
          String preparedString = "";
          for(int i = 0 ; i < plainText.length();i++)
          if(Character.isAlphabetic(plainText.charAt(i)))
          {
             preparedString = preparedString+Character.toUpperCase(plainText.charAt(i));        
          }
          return preparedString;
       }
       public String encode(String plainText)  
       {
          return plainText; 
       }

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

    Default Re: Calling one method to another method that are in the same class.

    Are you trying to call the encode function from the prepareString function, or are you trying to call the prepareString function from the encode function?

    Where are you trying to make that method call?
    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
    SmokeyWoody is offline Member
    Join Date
    Oct 2014
    Posts
    36
    Rep Power
    0

    Default Re: Calling one method to another method that are in the same class.

    Quote Originally Posted by KevinWorkman View Post
    Are you trying to call the encode function from the prepareString function, or are you trying to call the prepareString function from the encode function?

    Where are you trying to make that method call?
    Sorry, messed up with my wording. I am trying to call the prepareString function to the encode method so I can have it make changes to an user input along with another method that is in my code but like I said, I can't post all of it since I just get a web page error.

  4. #4
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    393
    Rep Power
    9

    Default Re: Calling one method to another method that are in the same class.

    Hi SmokeyWood, welcome.

    Calling a private method from another method within the same class is not a problem. Private means it cannot be accessed from outside of that class, something you are not trying to accomplish.
    Controlling Access to Members of a Class
    There does not seem to be any attempt to call either method from within the other. If you have modified this code from the ones you are having issues with then it would be worth presenting that along with any associated error messages you are receiving.

    Regards.

  5. #5
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    393
    Rep Power
    9

    Default Re: Calling one method to another method that are in the same class.

    Have you tried calling the method to see what happens?

    Regards.

  6. #6
    SmokeyWoody is offline Member
    Join Date
    Oct 2014
    Posts
    36
    Rep Power
    0

    Default Re: Calling one method to another method that are in the same class.

    Quote Originally Posted by Ronin View Post
    Hi SmokeyWood, welcome.

    Calling a private method from another method within the same class is not a problem. Private means it cannot be accessed from outside of that class, something you are not trying to accomplish.
    Controlling Access to Members of a Class
    There does not seem to be any attempt to call either method from within the other. If you have modified this code from the ones you are having issues with then it would be worth presenting that along with any associated error messages you are receiving.

    Regards.
    I did in fact modify it since I wasn't sure if I was doing it correctly or what I was doing.

    Java Code:
    public class ShiftEncoderDecoder
       private String prepareString(String plainText)
       {
          String preparedString = "";
          for(int i = 0 ; i < plainText.length();i++)
          if(Character.isAlphabetic(plainText.charAt(i)))
          {
             preparedString = preparedString+Character.toUpperCase(plainText.charAt(i));       
          }
          return preparedString;
       }
       public String encode(String plainText) 
       {
          int prepareString
          int setShift
          plainText = prepareString(setShift(shift))
          return plainText;
       }

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

    Default Re: Calling one method to another method that are in the same class.

    That code will not compile, as it is not syntactically valid. You're missing semicolons, and you don't set your variables to any value.

    You really might want to go back and consult some basic tutorials before proceeding.
    How to Ask Questions the Smart Way
    Static Void Games - GameDev tutorials, free Java and JavaScript hosting!
    Static Void Games forum - Come say hello!

  8. #8
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    393
    Rep Power
    9

    Default Re: Calling one method to another method that are in the same class.

    That code is confusing in terms of what you want to achieve.

    Within the encode method, the int prepareString is not used so is not required.
    The call to prepareString is expecting a String input, does setShift(shift) return a suitable value?
    The encode method parameter plaintext is never used. Is this value you wish to perform the manipulation on?

    Regards.

  9. #9
    SmokeyWoody is offline Member
    Join Date
    Oct 2014
    Posts
    36
    Rep Power
    0

    Default Re: Calling one method to another method that are in the same class.

    Sorry, I have gotten maybe 14 hours for the past five days and doing multiple things at once, just a mock up pretty much.

    It would be something like this.

    I am trying to get the method prepareString along with setShift to be used inside of the encode method and make changes to a user's input, shift.

    Java Code:
    int prepareString
    int setShift //Code in the class that isn't displayed because of length.
    preparedString2 = prepareString(shift);
    plainText = setShift(preparedString2);

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

    Default Re: Calling one method to another method that are in the same class.

    And what problems are you having implementing something like that?

    If you want help but can't post your full code (we don't want to see your full code anyway), please post an MCVE that demonstrates the problem you're having.
    How to Ask Questions the Smart Way
    Static Void Games - GameDev tutorials, free Java and JavaScript hosting!
    Static Void Games forum - Come say hello!

  11. #11
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    393
    Rep Power
    9

    Default Re: Calling one method to another method that are in the same class.

    It would be useful to know what your inputs are, expected and actual outputs are along. Are you receiving compilation errors or just unexpected results?

    Assuming it is an unexpected output it would be worth debugging your application either by stepping through or using additional print statements to watch the changing values. It could be that the methods originally highlighted are working correctly and the parameters passed in are incorrect for whatever reason.

    Regards.

  12. #12
    SmokeyWoody is offline Member
    Join Date
    Oct 2014
    Posts
    36
    Rep Power
    0

    Default Re: Calling one method to another method that are in the same class.

    Quote Originally Posted by KevinWorkman View Post
    And what problems are you having implementing something like that?

    If you want help but can't post your full code (we don't want to see your full code anyway), please post an MCVE that demonstrates the problem you're having.
    This is the parts of the code that I am using and are necessary to my question. I am trying to have an user's text input be altered by prepareText then be altered by by shiftChar along with the use of the users shift input. It is a cipher program to change the letter according the how much the shift is.

    The compiler is saying that prepareText is a symbol that it can't find and that shiftChar can't be applied to the user's string input.

    Java Code:
    public class ShiftEncoderDecoder
    {
       
       private int shift;
       
       public ShiftEncoderDecoder(int shift)
       {
          setShift(shift);
       }
       
       public int getShift()
       {
          return shift;
       }
       private String prepareString(String plainText)
       {
          String preparedString = "";
          for(int i = 0 ; i < plainText.length();i++)
          if(Character.isAlphabetic(plainText.charAt(i)))
          {
             preparedString = preparedString+Character.toUpperCase(plainText.charAt(i));        
          }
          return preparedString;
       }
       private char shiftChar(char letter,int amount)
       {
          letter = (char)(letter + amount);
          if(letter < 'A' )
          {
             letter = (char)(letter + 26);
          }
          if(letter > 'Z' )
          {
             letter = (char)(letter - 26);
          }
          return letter;
       }
       public String encode(String plainText)  
       {
          int prepareText;
          int shiftChar;
          String preparedString2 = prepareText(plainText);
          String preparedString3 = shiftChar(preparedString2);
          return plainText; 
       }
       public String decode(String cipherText)
       {
         return cipherText;
       }   
    }

  13. #13
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    393
    Rep Power
    9

    Default Re: Calling one method to another method that are in the same class.

    Quote Originally Posted by SmokeyWoody View Post
    The compiler is saying that prepareText is a symbol that it can't find and that shiftChar can't be applied to the user's string input.
    prepareText is a method which has not been declared.
    shiftChar is expecting two parameters, a char and an int. You are calling the method whilst passing in just a String.

    Regards.

  14. #14
    SmokeyWoody is offline Member
    Join Date
    Oct 2014
    Posts
    36
    Rep Power
    0

    Default Re: Calling one method to another method that are in the same class.

    ... How do I declare it? I did miss a couple days due to being sick but trying to get caught back up while studying for one exam, one test, one mid-quarter. My classes just don't agree with each other.

  15. #15
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    393
    Rep Power
    9

    Default Re: Calling one method to another method that are in the same class.

    Line 42 calls the method prepareText yet on line 40 prepareText was declared as an int. I assume the intent was to call the method prepareString instead but this is a guess.
    Defining Methods

    Regards.

  16. #16
    SmokeyWoody is offline Member
    Join Date
    Oct 2014
    Posts
    36
    Rep Power
    0

    Default Re: Calling one method to another method that are in the same class.

    Quote Originally Posted by Ronin View Post
    Line 42 calls the method prepareText yet on line 40 prepareText was declared as an int. I assume the intent was to call the method prepareString instead but this is a guess.
    Defining Methods

    Regards.
    Yeah I did notice that and have made the changes but now getting new errors with the program.

    This is what encode looks like now.

    Java Code:
       public String encode(String plainText)  
       {
          int prepareString;
          int shiftChar;
          preparedString2 = prepareString(plainText);
          preparedString3 = shiftChar(preparedString2,shift);
          return plainText; 
       }
    Now it can't find the symbol for preparedString2 and preparedString3.

    By the way I have to thank you for the help. Just super stressed right now, know you don't care but just saying. :P

  17. #17
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    393
    Rep Power
    9

    Default Re: Calling one method to another method that are in the same class.

    Quote Originally Posted by SmokeyWoody View Post
    By the way I have to thank you for the help. Just super stressed right now, know you don't care but just saying. :P
    You're welcome. ;)

    Before you use anything in Java you need to declare its type, irrespective of whether it is a method, object or primitive.
    Just as the method prepareText was not previously declared, preparedString2/3 have not been declared prior to use. Have a look at what type is being returned from the methods. The variable assigned the value returned should be of the same type.

    Regards.

  18. #18
    SmokeyWoody is offline Member
    Join Date
    Oct 2014
    Posts
    36
    Rep Power
    0

    Default Re: Calling one method to another method that are in the same class.

    Quote Originally Posted by Ronin View Post
    You're welcome. ;)

    Before you use anything in Java you need to declare its type, irrespective of whether it is a method, object or primitive.
    Just as the method prepareText was not previously declared, preparedString2/3 have not been declared prior to use. Have a look at what type is being returned from the methods. The variable assigned the value returned should be of the same type.

    Regards.
    Well I have this code now but now I realize I have to figure out how to check every letter of the preparedString2 and change all the characters individually. Hopefully I can figure out how to do that.

    Thank you again and you'll probably see me back here again but hopefully I get good enough to where I can actually help others out.

    Java Code:
       {
          int prepareString;
          int shiftChar;
          String preparedString2 = prepareString(plainText);
          Char preparedString3 = shiftChar(preparedString2,shift);
          return plainText; 
       }

Similar Threads

  1. Calling a method from another class
    By andrewche in forum New To Java
    Replies: 5
    Last Post: 05-04-2011, 04:46 AM
  2. Calling a method in the same class
    By Teclis in forum New To Java
    Replies: 8
    Last Post: 03-30-2011, 02:20 PM
  3. Calling a method from another class
    By snoopy11 in forum New To Java
    Replies: 4
    Last Post: 03-13-2011, 04:18 AM
  4. How to know which class is calling a method?
    By ranju4u6 in forum New To Java
    Replies: 14
    Last Post: 09-16-2010, 08:41 AM
  5. Replies: 29
    Last Post: 09-25-2008, 07:55 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
  •