-
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;
}
}
-
From what I can see, you may have to use 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..
-
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
-
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?
-
 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;
}
}
-
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
-
strCheck = strCheck + strAlph.charAt(i);
It is Lowercase in the code not in the //comment field though!!
Similar Threads
-
Replies: 2
Last Post: 12-08-2005, 07:52 PM
-
By Irina in forum ASP.NET
Replies: 0
Last Post: 11-29-2002, 10:47 PM
-
By Roseta in forum VB Classic
Replies: 0
Last Post: 11-14-2001, 03:24 AM
-
By Wade Balzer in forum VB Classic
Replies: 0
Last Post: 06-23-2000, 02:17 PM
-
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
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks