6

I am trying to get my code to prevent a user input from having a number in it.

Essentially I want the code to do as follows:

  1. ask for input
  2. receive input
  3. test whether or not the input contains a number(ex: 5matt vs matt)
  4. if contains a number I want to System.out.println("Error: please do not input a number");

Heres the kicker (and why it's not a duplicate question): I can't use loops or other statements we haven't learned yet. So far the only true statements we've learned are if/else/else if statements. That means I can not use for loops, like some of the answers are suggesting. While they're great answers, and work, I'll lose points for using them.

System.out.println("Please input the first name: ");    
String name1 = in.next();
System.out.println("Please input the second name: ");
String name2 = in.next();
System.out.println("Please input the third name: ");
String name3 = in.next();

name1 = name1.substring(0,1).toUpperCase() + name1.substring(1).toLowerCase();
name2 = name2.substring(0,1).toUpperCase() + name2.substring(1).toLowerCase();
name3 = name3.substring(0,1).toUpperCase() + name3.substring(1).toLowerCase();

I have this already but I can't figure out how to test if the input only contains letters.

8
  • 4
    You should get used to reading through the API documentation. Specifically, Character has an isDigit() method. Commented Sep 14, 2016 at 22:39
  • Or, use a regular expression like str.matches(".*\\d.*") (where str is a String), which will tell you if the String contains any digits. Commented Sep 14, 2016 at 22:39
  • if(name1.matches(".*\\d+.*") || name2.matches(".*\\d+.*") || name3.matches(".*\\d+.*")){ // stuff here; } else { //no nums; } Commented Sep 14, 2016 at 22:40
  • 3
    Possible duplicate of Check if a String contains numbers Java Commented Sep 14, 2016 at 22:42
  • 1
    "Small rant: this wouldn't be so hard if my professor actually taught us a way to do this." - Well I think your professor is doing you a big favor here. In the real world of programming, you will often encounter small (and big) programming problems that nobody has taught you how to solve. One of the things programmers need to learn is how to solve these problems for themselves. Commented Sep 14, 2016 at 22:48

4 Answers 4

3

Okay, there are many ways to deal with this. A good thing would be to use Regex (text matching stuff). But it seems that you should only use very basic comparison methods. So, let's do something very basic and easy to understand: We iterate over every character of the input and check whether it's a digit or not.

String input = ...
// Iterate over every character
for (int i = 0; i < input.length(); i++) {
    char c = s.charAt(i);

    // Check whether c is a digit
    if (Character.isDigit(c)) {
        System.out.println("Do not use digits!");
    }
}

This code is very straightforward. But it will continue checking even if a digit was found. You can prevent this using a helper-method and then returning from it:

public boolean containsDigit(String text) {
    // Iterate over every character
    for (int i = 0; i < input.length(); i++) {
        char c = s.charAt(i);

        // Check whether c is a digit
        if (Character.isDigit(c)) {
            return true;
        }
    }
    // Iterated through the text, no digit found
    return false;
}

And in your main program you call it this way:

String input = ...
if (containsDigit(input)) {
   System.out.println("Do not use digits!");
}
Sign up to request clarification or add additional context in comments.

8 Comments

At least use a for-each loop.
At least break on the first digit
At first I wanted but then I thought of OP maybe not knowing such stuff as it seems that he is a beginner.
Yes, but even a beginner might enjoy typing `for (Character c : input...) instead of the for int counter thingy.
containsDigit needs a return false at the end.
|
2

Use a regular expression to filter the input

Eg

str.matches(".*\\d.*")

See this link for more info

Comments

0

There are several ways you could do this, among others:

  • Iterate over all the chars in the string and check whether any of them is a digit.
  • Check whether the string contains the number 1, number 2, number 3, etc.
  • Use a regular expression to check if the string contains a digit.

(Java Regular Expressions)

Comments

0

If you're allowed to define functions, you can essentially use recursion to act as a loop. Probably not what your prof is going for, but you could be just inside the requirements depending on how they're worded.

public static boolean stringHasDigit(String s) {
    if (s == null) return false; //null contains no chars
    else return stringHasDigit(s, 0);
}

private static boolean stringHasDigit(String s, int index) {
    if (index >= s.length()) return false; //reached end of string without finding digit
    else if (Character.isDigit(s.charAt(index))) return true; //Found digit
    else return stringHasDigit(s, index+1);
}

Only uses if/elseif/else, Character.isDigit, and String.charAt, but recursion might be off limits as well.

Comments

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.