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

    Default Need help with calling methods

    I have a driver and a main program.

    How would I go along with calling the encode method to the driver class that I made so I can have the user inputs affected by the encode method?

    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;
       }
       public String encode(String plainText)  
       {
          int prepareString;
          int shiftChar;
          String preparedString2 = prepareString(plainText);
          
          String cipherText = ""; 
          for(int c = 0 ; c < preparedString2.length();c++)
          if(Character.isAlphabetic(preparedString2.charAt(c)))
          {   
             cipherText = cipherText+shiftChar(preparedString2.charAt(c),shift); 
          }
          return cipherText; 
       }
       public String decode(String cipherText)
       {
          String decodedText ="";
          for(int z = 0 ; z < cipherText.length();z++)
          if(Character.isAlphabetic(cipherText.charAt(z)))
          {
              decodedText = decodedText+shiftChar(cipherText.charAt(z),-shift);
          }
          return decodedText;
       }   
    }
    Java Code:
    import java.lang.*;
    import java.util.*;
    import java.util.Scanner; 
    
    public class ShiftEncoderDecoderDriver
    {
       public static void main(String[] args)
       {
          Scanner uInput = new Scanner(System.in);
          System.out.print("Please enter the code that you wish to encode...");
          String plainText = uInput.nextLine();
          int shift;
          do
          {
             System.out.println("Please enter the amount of spaces the letters will shift... ");
             shift = uInput.nextInt();
             if(shift <= 25 && shift >= -25)
             {
                System.out.println("The letters will shift "+shift+" places.");
                break;
             }
             else if(shift > 25)
             {
                System.out.println("Your input is larger then how far the letters are allowed to shift, try again.");
             }
             else if(shift < -25)
             {
                System.out.println("Your input is smaller then how far the letters are allowed to shift, try again.");
             }
          }
          while(shift <= 25 || shift >= -25);
          {      
          System.out.println("The input you wish to encode... "+plainText);
          System.out.println("How many spaces the letters will shift... "+shift);
          }
          ShiftEncoderDecoder cipher = new ShiftEncoderDecoder();
          plaintText.encode();
      } 
    }

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

    Default Re: Need help with calling methods

    I believe you want to used cipher.encode(String plainText). But I presume you first have to set the shift amount.

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

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

    Default Re: Need help with calling methods

    Copying the full text of the compiler errors and pasting them here would help us discuss the problems with the code.
    If you don't understand my response, don't ignore it, ask a question.

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

    Default Re: Need help with calling methods

    Quote Originally Posted by Norm View Post
    Copying the full text of the compiler errors and pasting them here would help us discuss the problems with the code.
    This is what I get when I try to call the method. Maybe I didn't call it correctly?
    Java Code:
    ShiftEncoderDecoderDriver.java:37: error: ')' expected
          cipher.encode(String plainText);
                              ^
    ShiftEncoderDecoderDriver.java:37: error: illegal start of expression
          cipher.encode(String plainText);
                                        ^

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

    Default Re: Need help with calling methods

    If you don't understand my response, don't ignore it, ask a question.

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

    Default Re: Need help with calling methods

    Uh, did you take me literally! I was showing you the signature of the proper method call. And please pardon me for asking, but did you write this code? I ask because you are not understanding some basic ideas which are trivial compared to some of the other constructs in your posted code.

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

Similar Threads

  1. Calling methods
    By antnas in forum New To Java
    Replies: 9
    Last Post: 10-21-2012, 07:48 AM
  2. Calling methods
    By Alkor in forum New To Java
    Replies: 15
    Last Post: 02-25-2012, 03:33 PM
  3. Calling for methods
    By soccer_kid_6 in forum New To Java
    Replies: 3
    Last Post: 02-27-2010, 10:12 PM
  4. Calling Methods
    By bluegreen7hi in forum New To Java
    Replies: 3
    Last Post: 12-17-2007, 07:22 AM
  5. need help calling methods
    By lowpro in forum New To Java
    Replies: 2
    Last Post: 11-15-2007, 10: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
  •