1

I'm currently doing a project in my computer science class and we are suppose to validate each character of a variable to see if it is legal or not. If it starts with a number it's illegal. If it starts with a special character it's legal but bad style. If it has a space it is again illegal. I'll post my current code now:

import java.util.Scanner;

public class classOfValidation {

    public static void main(String[] args) {    
        Scanner scan = new Scanner(System.in);    
        String theVariable = null;

        System.out.println("This program checks the validity of variables");    
        System.out.println("Please enter a variable (or press 'q' to quit");

        theVariable = scan.nextLine();

        do {        
            System.out.println("The variable is illegal");    
            theVariable = scan.nextLine();    
        } while (theVariable.startsWith("[0123456789]"));

        do {    
            System.out.println("The variable is illegal");    
            theVariable = scan.nextLine();    
        } while (theVariable.contains("[ ]"));

        do {    
            System.out.println("The variable is legal, but has bad style");    
            theVariable = scan.nextLine();    
        } while (theVariable.startsWith("[!@#$%^&*]"));         
    }    
}

If you couldn't already tell i'm new to programming and as confused as i possibly could be. If you have any advice or anything else you need me to explain then please leave a comment. Thanks everyone

1
  • If you enter a variable like " asdbasdb" for the first time, the program will skip the first while and enter the second one allowing me to use "1234" as variable name without saying it's wrong. You should put your code into one while (or do...while) loop and check for different aspects with if statements only remembering if one of these ifs was true during the current run of your loop (a boolean variable like "isValidName" could help here). Commented Oct 26, 2015 at 14:18

2 Answers 2

1

You can use the single regex to validate your input via String#matches() method. But as for the example you've provided, you should use while loop, but not do-while, because in do while case, you are always running it's body once befor condition checked. So, you better do it like:

theVariable = scan.nextLine();

while (theVariable.startsWith("[0123456789]")) {
    System.out.println("The variable is illegal");
    theVariable = scan.nextLine();
}

while (theVariable.contains("[ ]")) {
    System.out.println("The variable is illegal");
    theVariable = scan.nextLine();
}

while (theVariable.startsWith("[!@#$%^&*]")) {
    System.out.println("The variable is legal, but has bad style");
    theVariable = scan.nextLine();
}

The second, in your solution, you are using String.startsWith() method and passing into it some regex. Take a look at javadoc for this method. It's said there:

Tests if this string starts with the specified prefix.

That means, that this method doesn't support regexes, but simply checks whether the string starts with the passed string. So, your conditions seems, never to become true. I don't think, someone will input the [0123456789] or [!@#$%^&*].

One more, any conditions are checked once, but after that user can modify the input and the previewsly passed condition will not be checked again. Seems, it's better to run into infinite loop with continue and break in some conditions, like:

 //infinit loop, until user enter the `q` or the input is correct
 while (true) {

    //read the input
    theVariable = scan.nextLine();

    //chtck, whether is `quit` command entered
    if ("q".equals(theVariable)) {
        break;
    }

    //if string starts with digit or contains some whitespaces
    //then print alert and let the user to 
    //modify the input in a new iteration
    if (theVariable.matches("^\d+.*|.*\s+.*")) {
        System.out.println("The variable is illegal");
        continue;
    }

    //if string contains some special characters print alert 
    //and let the user to modify the input in a new iteration
    if (theVariable.matches("^[!@#$%^&*].*")) {
        System.out.println("The variable is legal, but has bad style");
        continue;
    }

    //if all the conditions checked, then break the loop
    break;
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the example, but do you know why when i add the while statement, every time i try inputting anything, the program terminates?
@TheNewYo I think, the problem is on the conditions you use. You are passing the regex to startsWith method, but it doesn't take the argument as the regex, but as a simple string.
This fixed my problem and just saved my grade, thank you
0

I think the best way if you are use regex.

Here is an answer how to do that.

1 Comment

Could you explain more such as should i use if statements or not and could you possibly provide an example of how i can incorporate it into my code? Thanks for the answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.