Results 1 to 6 of 6
  1. #1
    logonin is offline Member
    Join Date
    Nov 2015
    Posts
    8
    Rep Power
    0

    Default My code compiles but does not do anything

    Hello I am trying to make a program that will encrypt a password and then show the user the encrypted password. I was able to get the program working but now I have to add in methods and a driver class. After a lot of programming I finally got my code to compile! Unfortunately when run nothing happens... I am not sure what to do next so I am hoping someone can help me. Here is the code for my driver class

    Java Code:
    import javax.swing.JOptionPane;
    public class passwordCodeDriver1
    {
       private static String password = "";
      
    
       
     
      public static void main( String[] args )
      {
      passwordCodeDriver1 codeDriver = new passwordCodeDriver1();
      }
      
      public passwordCodeDriver1()
      {
        password = "";
        
      }
      
     
      
      
      public static void inputPassword()
      {
        password = JOptionPane.showInputDialog("please input your password");
      }
      
        
      //time to get stuff
        
        public static String getPassword()
        {
          return password;
        }
                                
      
      //calling methods
      passwordCodeFinished2 callMethods = new passwordCodeFinished2();
      public  String methodCaller()
      {
      callMethods.removeWhiteSpaceAndConvertToUpper();
      callMethods.substitute();
     callMethods.swapHalfsForEncrypt();
      callMethods.swapFirst2WithLast2();
      callMethods.swapMiddleChars();
    JOptionPane.showMessageDialog(null,password);
    return password;
    }
    }
    and here is the code connected to the driver

    Java Code:
    import javax.swing.JOptionPane;
    
    public class passwordCodeFinished2
    {
      
    static String password = passwordCodeDriver1.getPassword();
    static int length = password.length();
    static int lengthUse = length / 2;
    
    
    
     //converting to upcase 
      public String removeWhiteSpaceAndConvertToUpper()
      {
        password = password.trim();
        password = (password.toUpperCase());
        return password;
      }
    
      // changing stuff
      public  String substitute()
      {
        password = password.replace ( "A" , "@");
        password = password.replace ( "E" , "=");
        password = password.replace ( "I" , "!");
        password = password.replace ( "J" , "?");
        password = password.replace ( "O" , "*");
        password = password.replace ( "P" , "#");
        password = password.replace ( "R" , "&");
        password = password.replace ( "S" , "$");
        password = password.replace ( "T" , "+");
        password = password.replace ( "V" , "^");
        password = password.replace ( "X" , "%");
        password = password.replace ( " " , "_");
        return password;
      }
    
         //Switching halfs
      public String swapHalfsForEncrypt()
      {
          String FirstHalf = password.substring(0, length/2);
          String SecondHalf = password.substring(length/2, length);
          password = SecondHalf + FirstHalf;
          return password;
      }
    
    //Switching first and last letter
     public  String swapFirst2WithLast2()
      {
           String firstChar = password.substring(0,1);
           String lastChar = password.substring(length - 1);
          password = lastChar + password.substring(1, length-1) + firstChar;
          return password;
      }
    
    
          //swapMiddleChars
      public  String swapMiddleChars()
      {                              
         String firstPart = password.substring(lengthUse-2, lengthUse);
         String secondPart = password.substring(lengthUse, lengthUse+2); 
         String beginingOfAlphabet = password.substring(0, lengthUse-2);
         String endOfAlphabet = password.substring(lengthUse+2,length);
       password = beginingOfAlphabet + secondPart + firstPart + endOfAlphabet;
       return password;
      }
    }
    So what can I do to actually get this code to work? I am very confused so any help would be greatly appreciated. Thanks in advanced :)

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

    Default Re: My code compiles but does not do anything

    You create a driver instance but where to you call the password prompt method? And after that, where do you invoke the password encryption method?

    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,001
    Rep Power
    33

    Default Re: My code compiles but does not do anything

    when run nothing happens.
    Have you tried playing compute with your program to see where the execution goes?
    It starts in the PasswordCodeDriver1 class's main() method
    The main method calls the PasswordCodeDriver1 class's constructor
    What happens in the constructor?

    What will cause any of the methods in the class be called? There needs to be statements that implement some logic that call the methods as needed.

    Note: Class names should begin with an uppercase letter. That makes it easier to tell the difference between classnames and other names.
    If you don't understand my response, don't ignore it, ask a question.

  4. #4
    logonin is offline Member
    Join Date
    Nov 2015
    Posts
    8
    Rep Power
    0

    Default Re: My code compiles but does not do anything

    Quote Originally Posted by Norm View Post
    Have you tried playing compute with your program to see where the execution goes?
    It starts in the PasswordCodeDriver1 class's main() method
    The main method calls the PasswordCodeDriver1 class's constructor
    What happens in the constructor?

    What will cause any of the methods in the class be called? There needs to be statements that implement some logic that call the methods as needed.

    Note: Class names should begin with an uppercase letter. That makes it easier to tell the difference between classnames and other names.
    I'm not entirely sure I understand. I tried putting the call methods in the main method. It compiles but when I try to run it a bunch of red text comes up.
    Java Code:
    mport javax.swing.JOptionPane;
    public class passwordCodeDriver1
    {
       private static String password = "";
      
    
       
     
      public static void main( String[] args )
      {
      passwordCodeDriver1 codeDriver = new passwordCodeDriver1();
      passwordCodeFinished2 callMethods = new passwordCodeFinished2();
      callMethods.removeWhiteSpaceAndConvertToUpper();
      callMethods.substitute();
      callMethods.swapHalfsForEncrypt();
      callMethods.swapFirst2WithLast2();
      callMethods.swapMiddleChars();
      }
      
      public passwordCodeDriver1()
      {
        password = "";
        
      }
      
     
      
      
      public static void inputPassword()
      {
        password = JOptionPane.showInputDialog("please input your password");
      }
      
        
      //time to get stuff
        
        public static String getPassword()
        {
          return password;
        }
                                
      
      //calling methods
      passwordCodeFinished2 callMethods = new passwordCodeFinished2();
      public  String passwordPrint()
      {
    JOptionPane.showMessageDialog(null,password);
    return password;
    }
    }
    and here is the red text I get

    "java.lang.StringIndexOutOfBoundsException: String index out of range: 1
    at java.lang.String.substring(Unknown Source)
    at passwordCodeFinished2.swapFirst2WithLast2(password CodeFinished2.java:50)
    at passwordCodeDriver1.main(passwordCodeDriver1.java: 16)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknow n Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Un known Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at edu.rice.cs.drjava.model.compiler.JavacCompiler.ru nCommand(JavacCompiler.java:272)"

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

    Default Re: My code compiles but does not do anything

    "java.lang.StringIndexOutOfBoundsException: String index out of range: 1
    at java.lang.String.substring(Unknown Source)
    at passwordCodeFinished2.swapFirst2WithLast2(password CodeFinished2.java:50)
    The statement on line 50 called the String class's substring method with an invalid index: 1.
    Look at the code on line 50 and see why the code used an index of 1 with a String that has less than 2 characters.
    If you don't understand my response, don't ignore it, ask a question.

  6. #6
    logonin is offline Member
    Join Date
    Nov 2015
    Posts
    8
    Rep Power
    0

    Default Re: My code compiles but does not do anything

    Quote Originally Posted by Norm View Post
    The statement on line 50 called the String class's substring method with an invalid index: 1.
    Look at the code on line 50 and see why the code used an index of 1 with a String that has less than 2 characters.
    YES I WAS ABLE TO FIX IT!!! Thank you so much!!!!
    Last edited by logonin; 02-08-2016 at 04:35 AM.

Similar Threads

  1. Code compiles and runs, but window does not pop up.
    By DrBagelBites in forum New To Java
    Replies: 6
    Last Post: 09-27-2014, 04:44 AM
  2. Code compiles but get null pointer exeption
    By jamesreno in forum Threads and Synchronization
    Replies: 17
    Last Post: 04-08-2014, 04:28 AM
  3. Code compiles but fails to run (works in NetBeans)
    By swilliams236 in forum New To Java
    Replies: 2
    Last Post: 11-07-2011, 09:05 PM
  4. Compiles but won't run
    By Alexishy in forum New To Java
    Replies: 13
    Last Post: 07-04-2011, 09:52 PM
  5. Code compiles in/runs in IDE but not on UNIX system
    By Unclejunebug in forum New To Java
    Replies: 7
    Last Post: 10-08-2009, 08:35 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
  •