views:

49

answers:

5

Basically, my situation requires me to check to see if the String that is defined by user input from the keyboard is only alphabetical characters in one case and only digits in another case. This is written in Java.

my current code:

switch (studentMenu) {
                case 1: // Change all four fields
                    System.out.println("Please enter in a first name: ");

                    String firstNameIntermediate = scan.next();
                    firstName = firstNameIntermediate.substring(0,1).toUpperCase() + firstNameIntermediate.substring(1);
                    System.out.println("Please enter in a middle name");
                    middleName = scan.next();
                    System.out.println("Please enter in a last name");
                    lastName = scan.next();
                    System.out.println("Please enter in an eight digit student ID number");
                    changeID();
                    break;
                case 2: // Change first name
                    System.out.println("Please enter in a first name: ");
                    firstName = scan.next();
                    break;
                case 3: // Change middle name
                    System.out.println("Please enter in a middle name");
                    middleName = scan.next();
                    break;
                case 4: // Change last name
                    System.out.println("Please enter in a last name");
                    lastName = scan.next();
                case 5: // Change student ID:
                    changeID();
                    break;
                case 6: // Exit to main menu
                    menuExit = true;
                default:
                    System.out.println("Please enter a number from 1 to 6");
                    break;
            }
        }
    }

public void changeID() {
    studentID = scan.next();
    }

I need to make sure the StudentID is only numerical and each of the name segments are alphabetical.

A: 

Im not sure this is the best way to do, but you could use Character.isDigit() and Character.IsLiteral() mabybe like this:

for( char c : myString.toCharArray() ) {
    if( !Character.isLiteral(c) ) {
        // 
    }
}
InsertNickHere
A: 

I don't think you can prevent the users from entering invalid values, but you have the option of validating the data you receive. I'm a fan of regular expressions. Real quick, something like this maybe (all values initialized to empty Strings):

while (!firstName.matches("^[a-zA-Z]+$")) {
    System.out.println("Please enter in a first name");
    firstName = scan.next();
}

...

while (!studentID.matches("^\\d{8}$")) {
    System.out.println("Please enter in an eight digit student ID number");
    changeID();
}

If you go this route, you might as well categorize the different cases you need to validate and create a few helper methods to deal with each.

"Regex" tends to seem overwhelming in the beginning, but learning it has great value and there's no shortage of tutorials for it.

Lauri Lehtinen
A: 

try regexp: \d+ -- numerical, [A-Za-z]+ -- alphabetical

Alex
+1  A: 

It's probably easiest to do this with a regular expression. Here's some sample code:

import java.util.regex.*;

public class Test
{
    public static void main(String[] args) throws Exception
    {
        System.out.println(isNumeric("123"));
        System.out.println(isNumeric("abc"));
        System.out.println(isNumeric("abc123"));

        System.out.println(isAlpha("123"));
        System.out.println(isAlpha("abc"));
        System.out.println(isAlpha("abc123"));
    }

    private static final Pattern NUMBERS = Pattern.compile("\\d+");
    private static final Pattern LETTERS = Pattern.compile("\\p{Alpha}+");

    public static final boolean isNumeric(String text)
    {
        return NUMBERS.matcher(text).matches();
    }

    public static final boolean isAlpha(String text)
    {
        return LETTERS.matcher(text).matches();
    }
}

You should probably write methods of "getAlphaInput" and "getNumericInput" which perform the appropriate loop of prompt/fetch/check until the input is correct. Or possibly just getInput(Pattern) to avoid writing similar code for different patterns.

You should also work out requirements around what counts as a "letter" - the above only does a-z and A-Z... if you need to cope with accents etc as well, you should look more closely at the Pattern docs and adapt appropriately.

Note that you can use a regex to validate things like the length of the string as well. They're very flexible.

Jon Skeet
+2  A: 
polygenelubricants