0

There's no compiling errors but for some reason I'm always directed to the "Invalid Choice" option after inputing my desired password length. Any advice on how to repair this glitch? Thanks.

import java.util.Scanner;
import java.util.Random;
public class Password
{
    public static void main(String [] args)
    {
        Scanner in = new Scanner(System.in);
        Random randNum = new Random();
        String upperCase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        String lowerCase = "abcdefghijklmnopqrstuvwxyz";
        String symbols = "!@#$%^&*()_+{}|<>?[]=-";
        String numbers = "1234567890";
        System.out.println("Password Generation Menu");

        //Determine Character Sets
        System.out.println("[1] Lowercase Letters)");
        System.out.println("[2] Lowercase & Uppercase Letters)");
        System.out.println("[3] Lowercase, Uppercase, and Numbers");
        System.out.println("[4] Lowercase, Uppercase, Numbers, and Punctuation");
        System.out.println("[5] Quit");

        //Determine Selection
        System.out.print("Enter Selection (1-5): ");
        String choice = in.next();
        System.out.print(choice);
        System.out.println();   

        // Detrermine Password Length
        System.out.print("Password Length (1-14): ");
        int length;
        String passwordLength;
        passwordLength = in.next();                                   
        length = Integer.parseInt(passwordLength);
        System.out.print(length);
        System.out.println();

        //Determine Value Of Activity Level
        String lowerLetter = "";
        String upperLetter = "";
        String symbol = "";
        String number = "";
        if (choice == "1"){
            for (int counter = 0; counter < length; counter++) {
            lowerLetter += lowerCase.charAt(randNum.nextInt(lowerCase.length()));
            System.out.println("Password: " + lowerLetter);
        }
        }else if(choice == "2"){
            for (int counter = 0; counter < length; counter++) {
            lowerLetter += lowerCase.charAt(randNum.nextInt(lowerCase.length()));
            upperLetter += upperCase.charAt(randNum.nextInt(upperCase.length()));
            System.out.println("Password: " + lowerLetter + upperLetter);
        }
        }else if((choice == "3")){
            for (int counter = 0; counter < length; counter++) {
            lowerLetter += lowerCase.charAt(randNum.nextInt(lowerCase.length()));
            upperLetter += upperCase.charAt(randNum.nextInt(upperCase.length()));
            number += numbers.charAt(randNum.nextInt(numbers.length()));
            System.out.println("Password: " + lowerLetter + upperLetter + number);
        }
        }else if((choice == "4")){
            for (int counter = 0; counter < length; counter++) {
            lowerLetter += lowerCase.charAt(randNum.nextInt(lowerCase.length()));
            upperLetter += upperCase.charAt(randNum.nextInt(upperCase.length()));
            number += numbers.charAt(randNum.nextInt(numbers.length()));
            symbol += symbols.charAt(randNum.nextInt(symbols.length()));
            System.out.println("Password: " + lowerLetter + upperLetter + number + symbols); 
        }
        }else if((choice == "5")){       
                System.out.println("Done");
        }else{
            System.out.println("Invalid Choice");
        }
    }
}
3
Contributors
4
Replies
18
Views
2 Weeks
Discussion Span
Last Post by tinstaafl
0

So i would need to add a boolean statment prior to the "if" statments ?

Edited by Rebecca_6

0

My comment about changing from if to a switch is about readability. You have so many if and else's that I didn't see why at the first reading.
To me it's time to clean this up.

I supplied a tutorial for you to take if you haven't used switch before.

1

Aside from the obvious need to refactor. It's worth mentioning the root of your problem, since it will most likely occure again.

When comparing strings you should always use the .equals function instead of the == operator. The == is for checking if 2 objects are the same object and the .equals function tests their values.

Here's how your code would look with the switch statement:

    switch (choice)
    {
        case "1":
            for (int counter = 0; counter < length; counter++) {
                lowerLetter += lowerCase.charAt(randNum.nextInt(lowerCase.length()));
                System.out.println("Password: " + lowerLetter);
            }       break;
        case "2":
            for (int counter = 0; counter < length; counter++) {
                lowerLetter += lowerCase.charAt(randNum.nextInt(lowerCase.length()));
                upperLetter += upperCase.charAt(randNum.nextInt(upperCase.length()));
                System.out.println("Password: " + lowerLetter + upperLetter);
            }       break;
        case "3":
            for (int counter = 0; counter < length; counter++) {
                lowerLetter += lowerCase.charAt(randNum.nextInt(lowerCase.length()));
                upperLetter += upperCase.charAt(randNum.nextInt(upperCase.length()));
                number += numbers.charAt(randNum.nextInt(numbers.length()));
                System.out.println("Password: " + lowerLetter + upperLetter + number);
            }       break;
        case "4":
            for (int counter = 0; counter < length; counter++) {
                lowerLetter += lowerCase.charAt(randNum.nextInt(lowerCase.length()));
                upperLetter += upperCase.charAt(randNum.nextInt(upperCase.length()));
                number += numbers.charAt(randNum.nextInt(numbers.length()));
                symbol += symbols.charAt(randNum.nextInt(symbols.length()));
                System.out.println("Password: " + lowerLetter + upperLetter + number + symbols);       
            }       break;
        case "5":
            System.out.println("Done");
            break;
        default:
            System.out.println("Invalid Choice");
            break;
    }

BTW, this problem makes a great case for using a modern up to date IDE like NetBeans. Not only did it flag the problen using ==, it also gave me the option to refactor the code for the switch statement automatically.

Edited by tinstaafl

Votes + Comments
That's very readable and clear what it does.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.