Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent integer input in Java?

Tags:

java

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.

like image 540
matt kaminski Avatar asked Sep 14 '16 22:09

matt kaminski


People also ask

What are the 3 ways to input in Java?

Java provides three classes to take user input: BufferedReader, Scanner, and Console.

How do you use try catch to prevent string input where an int is needed?

Use Integer. parseInt(input) in try block. If input is not an int, then it will throw an exception and catch the exception in catch{} block .


1 Answers

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!");
}
like image 157
Zabuzard Avatar answered Sep 28 '22 01:09

Zabuzard