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

    Default Help calling a string to another method.

    Well I am back. I need to get the string encodedString from the method encode able to be used in the decode method.

    Java Code:
       public String encode(String plainText)  
       {
          int prepareString;
          int shiftChar;
          String preparedString2 = prepareString(plainText);
          
          String encodedString = ""; 
          for(int c = 0 ; c < preparedString2.length();c++)
          if(Character.isAlphabetic(preparedString2.charAt(c)))
          {   
             encodedString = encodedString+shiftChar(preparedString2.charAt(c),shift); 
          }
          return encodedString; 
       }
       public String decode(String cipherText)
       {
          
          String cipherText = "";
          for(int z = 0 ; z < encodedString.length();z++)
          if(Character.isAlphabetic(encodedString.charAt(z)))
          {
              cipherText = cipherText+shiftChar(encodedString.charAt(z),-shift);
          }
          return cipherText;
       }   
    }
    Last edited by SmokeyWoody; 10-23-2014 at 11:30 PM. Reason: Typo in the title

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Help calling a string to another method.

    If you are getting error messages, copy the full text and paste it here.

    Otherwise explain what problem you are having. Copy the program's output and paste it here and add some comment saying what is wrong with it.
    If you don't understand my response, don't ignore it, ask a question.

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

    Default Re: Help calling a string to another method.

    Quote Originally Posted by Norm View Post
    If you are getting error messages, copy the full text and paste it here.

    Otherwise explain what problem you are having. Copy the program's output and paste it here and add some comment saying what is wrong with it.
    Well the I want to use the String encodedString that is found in the encode method in the decode method. The problem is that it shows as it being undeclared or that it can't find the symbol when I run the compiler. I am not good with calling.

    Java Code:
    public class ShiftEncoderDecoder
    {
       
       private int shift;
       
       public ShiftEncoderDecoder(int shift)
       {
          setShift(shift);
       }
       
       public int getShift()
       {
          return shift;
     public String encode(String plainText)  
       {
          int prepareString;
          int shiftChar;
          String preparedString2 = prepareString(plainText);
          
          String encodedString = ""; 
          for(int c = 0 ; c < preparedString2.length();c++)
          if(Character.isAlphabetic(preparedString2.charAt(c)))
          {   
             encodedString = encodedString+shiftChar(preparedString2.charAt(c),shift); 
          }
          return encodedString; 
       }
       public String decode(String cipherText)
       {
          String cipherText = "";
          for(int z = 0 ; z < encodedString.length();z++)
          if(Character.isAlphabetic(encodedString.charAt(z)))
          {
              cipherText = cipherText+shiftChar(encodedString.charAt(z),-shift);
          }
          return cipherText;
       }   
    }

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Help calling a string to another method.

    I want to use the String encodedString that is found in the encode method in the decode method
    That variable is defined locally to the encode method. It is not in scope in another method.

    Should decode use the String that is passed to it as an argument?
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Help calling a string to another method.

    I believe you have a problem with your curly braces.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

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

    Default Re: Help calling a string to another method.

    Quote Originally Posted by jim829 View Post
    I believe you have a problem with your curly braces.

    Regards,
    Jim
    Actually I had to do some tinkering with the return function.

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: Help calling a string to another method.

    I don't even know what 'calling a String to another method' is supposed to mean ... You can't call Strings; you can call a method and possibly pass a String as a parameter ... the 'to another method' part doesn't make sense to me at all ...

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Help calling a string to another method.

    Java Code:
    String myEncodedString = myEncoderDecoder.encode(<some string here>);
    String myDecodedString = myEncoderDecoder.decode(myEncodeString);
    Essentially, wherever you are calling your encode from, you need to store the returned String somewhere so you can use it.

    Note that the above is not copy/paste code. It's only to show you the concept.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Replies: 0
    Last Post: 12-14-2011, 06:21 PM
  2. Thread problem, calling method in run method
    By majk in forum Threads and Synchronization
    Replies: 4
    Last Post: 09-27-2010, 12:40 PM
  3. Replies: 29
    Last Post: 09-25-2008, 08: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
  •