Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm trying to have the user input a 6-character password. The first 3 characters should be capital letters and the last 3 characters should be numbers. If it's wrong, they have to re-enter the password. Finally, the password should be turned into a "secret password" by increasing the 6 characters by 2 (in the ascii table), and then have that new password printed out. I'm getting a run-time error and I don't understand why. Can anyone solve this?

String password = "", newPassword = password + 2;
		char char1 = password.charAt(1), char2 = password.charAt(2), char3 = password.charAt(3), char4 = password.charAt(4),
				char5 = password.charAt(5), char6 = password.charAt(6);
		
		System.out.println("Enter a 6-character password. The first three characters should be capital letters of "
				+ "the alphabet\nand the last three should be numbers from 1 to 9.");
		password = keyboard.next();
		
		if((char1 != 65 && !(char1 < 90)) || (char2 != 65 && !(char2 < 90)) || (char3 != 65 && !(char3 < 90)))
			System.out.println("Incorrect characters. Re-enter password.");
		password = keyboard.next();
		
		if((char4 != 1 && !(char4 < 9)) || (char5 != 1 && !(char5 < 9)) || (char6 != 1 && !(char6 < 9)))
			System.out.println("Incorrect characters. Re-enter password.");
		password = keyboard.next();
		
			System.out.print(password + "\n" + newPassword);


What I have tried:

I have tried changing the character variable declaration and getting rid of the password.charAt(1). I think that's what's giving me the issue, but I'm not sure how to make it read each character.
Posted
Updated 9-Feb-18 23:06pm
Comments
PIEBALDconsult 9-Feb-18 18:21pm    
Might be good if you provided the error text and the input that led to it.
I don't think you need to read each character on its own.

1 solution

Java well knows toCharArray() Method[^]

Java
String password = "password123456";
char[] characters = password.toCharArray();
for(int i=0;i<characters.length;i++)
{
    System.out.println(characters[i]);
}


Note: array index starts from 0 to count of elements in array -1.

See: Java Arrays[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900