Home > Net >  Character Class Method issues
Character Class Method issues

Time:05-14

So I'm currently working on a school assignment which is to design a program that allows the user to enter some text, and then the program checks:

  1. The first letter of the first word is a capital letter (it needs to be lowercase)
  2. That there are only letters and numbers in the entire user input
  3. That there are no spaces in the user input

So the program does work but the issue I'm having is with the print statements, I'll post my code below and explain:

public static void main(String[] args) {
            try (Scanner stdln = new Scanner(System.in)) {
            String userInput;
            
            boolean legal = true;
            boolean style = true;
            
            char input; //Checks the userInput variable to be sure that boolean is false if there's no lowercase letter at char0   if there are no letters
            char loop;
            
            System.out.println("The program checks the properness of a proposed Java variable name.");
            System.out.println("\nPlease enter a variable name (q to quit): ");
            userInput = stdln.nextLine();
            input = userInput.charAt(0);
            
            do
            {
                
                if (!(Character.isLetter(input)))
                {
                    legal = false;
                }
                if (userInput.contains(" "))
                {
                    legal = false;
                }
                if (!(Character.isLowerCase(input)))
                {
                    style = false;
                }
                    for (int i = 1; i < userInput.length() &&legal; i  )
                    {
                        loop = userInput.charAt(i);
                                    
                        if (!(Character.isLetterOrDigit(loop)))
                        {
                            style = false;
                        }
                    }
                
                if (!(legal) && !(style))
                {
                    System.out.println("Illegal.");
                } 
                else if (legal && !(style)) 
                {
                    System.out.println("Legal, but uses poor style.");
                } 
                else  
                {
                    System.out.println("Good.");
                }
                
                System.out.println("\nPlease enter a variable name (q to quit): ");
                userInput = stdln.nextLine();
                input = userInput.charAt(0);
                
            } while (!(userInput.equalsIgnoreCase("q"))); 
        }
    }
}

So the code works and the first input I test comes out as it should, however, once I get a response that isn't "Good.", then the same response will print for every entry, here's a sample from a session I just did:

The program checks the properness of a proposed Java variable name.

  1. Please enter a variable name (q to quit): streetAddress2 Good.

  2. Please enter a variable name (q to quit): StreetAddress2 Legal, but uses poor style.

  3. Please enter a variable name (q to quit): streetAddress2 Legal, but uses poor style.

  4. Please enter a variable name (q to quit): Street Address2 Illegal.

  5. Please enter a variable name (q to quit): streetAddress2 Illegal.

In that sample session, 3 and 5 should return the statement "Good." but for some reason, it just prints the statement from the previous entry. I'm still fairly new to Java so I'm a little stumped. Any ideas?

CodePudding user response:

You forgot to reset to true your legal and style boolean variables.

At every iteration, the legal and style variables will keep containing the result of the previous input. For example, if on your first input you immediately write a variable name with an illegal syntax and poor style, you'll see that any following name will show the same result. Even though those names are good or they only lack in style, the output will still be the same (wrong) because both variables have been left to false and nothing sets them back to true.

Besides, the logic to print the output messages didn't account for all combinations correctly.

Both variable logic and output printing could be re-written as follows:

do {
    //forgotten reset
    legal = true;
    style = true;

    //excat same implementation of yours
    if (!(Character.isLetter(input))) {
        legal = false;
    }
    if (userInput.contains(" ")) {
        legal = false;
    }
    if (!(Character.isLowerCase(input))) {
        style = false;
    }
    for (int i = 1; i < userInput.length() && legal; i  ) {
        loop = userInput.charAt(i);

        if (!(Character.isLetterOrDigit(loop))) {
            style = false;
        }
    }

    //If it's illegal it does not matter whether the variable name has a poor or good style, it's illegal anyway
    if (!legal) {
        System.out.println("Illegal.");
        
        //If we're in this else branch then the variable name is legal, but we have to check its style.
        //If it has poor style then we print the "poor style" message.
    } else if (!style) {
        System.out.println("Legal, but uses poor style.");
    } else {
        //Last case where the variable name is legal and has a good style
        System.out.println("Good.");
    }

    System.out.println("\nPlease enter a variable name (q to quit): ");
    userInput = stdln.nextLine();
    input = userInput.charAt(0);

} while (!(userInput.equalsIgnoreCase("q")));

CodePudding user response:

You have to reset legal and style to true at the start of each iteration. However, it is not the only problem with your code. The logic is not correct.

Right now in the for loop you check all the characters being letters or digits. If this condition fails you set style to false. However, you should set legal to false instead, because such identifiers are not allowed.

Also, when you print the result you don't check the conditions correctly. For example, if legal is false, but style is true your code will print Good.

  • Related