Help with Encryption Problem - Getting Answer


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 7 of 7

Thread: Help with Encryption Problem - Getting Answer

  1. #1
    Join Date
    Sep 2004
    Posts
    103

    Help with Encryption Problem - Getting Answer

    OKay I created this program that's supposed to be encrypted. The only thing is I can' t get the right answer, What I did here is attempt to print out all the viable answers that could be correct within this program. The only thing is none of them work for the program, Can someone take a look and possibly see what the solution is, I keep getting the wrong answer HEre's the program

    In the IF STatement is where I attempt to print out the right answer, but that answer does not work when I run the program over again, can someone help?

    import java.util.Scanner;


    public class javacrp
    {

    String strAlph [] = {"abcdefghijklmnopqrstuvwxyz"};
    private Scanner stdin;

    public javacrp() {

    stdin = new Scanner( System.in );
    System.out.println("Please Enter in Password");
    String strPassword = stdin.next();
    checkPassword(strPassword);

    }

    public String checkPassword(String strPassword)
    {

    int intLen;
    intLen = strAlph.length;
    String strCheck;

    strCheck = "";

    for (int i = 0; i < intLen; i ++){
    strCheck = strCheck + strAlph[i];
    System.out.println(strCheck);
    }


    if (strCheck == strPassword)
    {
    System.out.println("Correct Password ");
    System.out.println(strCheck);
    }
    else{
    System.out.println("Incorrect Password");
    System.out.println("Correct Password is " + strCheck);
    }

    return strCheck;

    }
    }

  2. #2
    Join Date
    Aug 2005
    Location
    Melbourne...Australia
    Posts
    279
    From what I can see, you may have to use
    Code:
    charAt
    eg
    Code:
     strCheck = strCheck + strAlph[i];
    should be

    Code:
     strCheck = strCheck + strAlph.charAt(i);
    You will need to import
    Code:
    import java.lang.String;
    Anyway see how this goes for you..

    btw you should use code tags, it makes your code much easier to read..

  3. #3
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560

    Check my comments

    Code:
    import java.util.Scanner;
    
    public class JavaCrp {
      /**
       * this is a ONE element string array containing the element:
       * "abcdefghijklmnopqrstuvwxyz", this is Java not C.
       */
      String strAlph[] = {
          "abcdefghijklmnopqrstuvwxyz"};
    
      private Scanner stdin;
    
      public JavaCrp() {
    
        stdin = new Scanner(System.in);
        System.out.println("Please Enter in Password");
        String strPassword = stdin.next(); // I write password 'abracadabra'
        checkPassword(strPassword);
    
      }
    
      public String checkPassword(String strPassword) {
    
        int intLen;
        intLen = strAlph.length;
        String strCheck;
    
        strCheck = "";
    
        /**
         * The following loop just copies the contents of strAlph into strCheck,
         * it loops once, intLen = 1.
         * Why the loop is here I have no idea.
         */
        for (int i = 0; i < intLen; i++) {
          strCheck = strCheck + strAlph[i];
          System.out.println(strCheck);
        }
        /**
         * The following tries to ensure that the only accepted password is
         * "abcdefghijklmnopqrstuvwxyz".  However, using the '==' operator for
         * comparing objects (String in this case) compares referece (address),
         * not content.  Use the equals(Object ob) method like:
         * if (strCheck.equals(strPassword)
         *
         * But, still the only accepted pasword here is "abcdefghijklmnopqrstuvwxyz"
         */
        if (strCheck == strPassword) {
          System.out.println("Correct Password ");
          System.out.println(strCheck);
        }
        else {
          System.out.println("Incorrect Password");
          System.out.println("Correct Password is " + strCheck);
        }
    
        return strCheck;
    
      }
    }
    eschew obfuscation

  4. #4
    Join Date
    Sep 2004
    Posts
    103

    Thanks,

    I will start to implement some tages, I have another question, Have you ever developed Pages in ASp.nET Sjalle? The reason I'm asking this is do you know any good resources on how to write cookies in java?

  5. #5
    Join Date
    Sep 2004
    Posts
    103
    Quote Originally Posted by Code_Nerd
    Code:
     strCheck = strCheck + strAlph.charAt(i);

    I changed it from to this above but get this Error.
    Cannot find symbol .CharAt(int);

    import java.util.Scanner;
    import java.lang.String;

    public class javacrp
    {
    // array of multiple lettersnow instead of just 1 long string of words

    String strAlph [] = {"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v"," w","x","y","z"};
    private Scanner stdin;

    public javacrp() {

    stdin = new Scanner( System.in );
    System.out.println("Please Enter in Password");
    String strPassword = stdin.next();
    checkPassword(strPassword);

    }

    public String checkPassword(String strPassword)
    {

    // 26 should be the correct number that intLen Gets or is equal to
    int intLen;
    intLen = strAlph.length;
    String strCheck;

    strCheck = "";

    // For loops cycles through 26 times I believe. Error is Here where it sets
    // strAlph.CharAt(i);
    for (int i = 0; i < intLen; i ++){

    strCheck = strCheck + strAlph.charAt(i);
    System.out.println(strCheck);
    }


    // Making an attempt to print out the correct Password
    // I can't get it to equal any real input
    if (strCheck == strPassword)
    {
    System.out.println("Correct Password ");

    }
    else{
    System.out.println("Incorrect Password");
    System.out.println("Correct Password is " + strCheck);
    System.out.println(strCheck);
    System.out.println(intLen);
    System.out.println(strPassword);
    }

    return strCheck;

    }
    }

  6. #6
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    its charAt, no sensible method in java starts with an uppercase.

    do you know any good resources on how to write cookies in java
    Writing cookies in java is a piece of cake , this is the constructor:

    Code:
    Cookie(java.lang.String name, java.lang.String value)
    Check out the package javax.servlet.http
    Last edited by sjalle; 12-06-2005 at 10:28 AM.
    eschew obfuscation

  7. #7
    Join Date
    Sep 2004
    Posts
    103
    strCheck = strCheck + strAlph.charAt(i);

    It is Lowercase in the code not in the //comment field though!!

Similar Threads

  1. Help With Encryption Problem
    By RPBLEA in forum Java
    Replies: 2
    Last Post: 12-08-2005, 07:52 PM
  2. Problem with Search
    By Irina in forum ASP.NET
    Replies: 0
    Last Post: 11-29-2002, 10:47 PM
  3. a problem with font and language
    By Roseta in forum VB Classic
    Replies: 0
    Last Post: 11-14-2001, 03:24 AM
  4. Replies: 0
    Last Post: 06-23-2000, 02:17 PM
  5. Arabic problem view
    By Ayman in forum VB Classic
    Replies: 8
    Last Post: 04-03-2000, 08:13 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
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center