1
String roulette = keyboard.next();
    if (roulette.length()!=14)
    {
        System.out.print("Error: 14 digits only");
        System.exit(1);
    }

I did this to make sure I have a length of 14. But my program also requires it to be numbers ONLY. It also needs to be a string. I don't want to parse int, I want my program to System.exit(1) if a letter is entered. Is there any way to do this?

I looked here https://docs.oracle.com/javase/8/docs/api/java/util/Scanner.html and https://docs.oracle.com/javase/8/docs/api/java/lang/Integer.html but cant find what im looking for

1
  • The character class's has an isDigit method that tells you if a character is a number. It's pretty easy to create your own version of that function though. That would actually be a good exercise for you. Anyways, just use that method to check if every character is a digit. Commented Feb 15, 2017 at 17:12

5 Answers 5

3

You can use a simple regular expression check:

if(roulette.matches("[0-9]{14}")) {
    // has length 14 and only digits
} else {
    // wrong format
}

This checks both for length and if only digits are used.

Further reading on regular expressions: https://docs.oracle.com/javase/tutorial/essential/regex/

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much, i was breaking my head with isDigit
Any way to make this include ':' too?
0

You use Java regular expressions :

String pattern1 = "\\d{14}";
Pattern p = Pattern.compile(pattern1);
Matcher m = p.matcher(roulette);
//exit if all 14 not integers, or any non-integer present
if(!m.find())
    System.exit(1);

Comments

0

Use regex for matching and Scanner for input.

public static void main(String[] args) {
    String regex = "\\d+";
    Scanner s=new Scanner(System.in);
    while(s.hasNext())
    {
        String text=s.next();
        if(text.matches(regex))
        {
            System.out.println("Input Strings only");
        }
        else
        {
            System.out.println(text);
            //break;
        }
    }

Comments

0

I'll add this here just to show what I meant by use Character.isDigit. I suggested it since in Clojure, which uses Java, this is easy because it has an every? method which checks if everything in a collection satisfies a predicate (Character.isDigit in this case). In Clojure, this makes it just:

(every? #(Character/isDigit %) "1234")

It's beautiful.

Unfortunately, for a similar method to work in Java, you need to convert the String to a char-array, then ArrayList, then a stream, and call allMatch on it. Instead, here's a manual way using a loop:

import java.util.*;

public class Main {

    public static boolean areAllNums (String numStr) {
        // Loop over each character...
        for (char d : numStr.toCharArray()) {
            //And if we find a non-digit, return false;
            if (!Character.isDigit(d)) {
                return false;
            }
        }
        // If we made it here, all the chars were digits
        return true;
    }

    public static void main(String[] args) {
        System.out.println(Arrays.asList(
            areAllNums("1234"), areAllNums("12g4")));

        // Prints [true, false]
    }
}

Comments

0

I found on SO not long ago (Surprised no-one's mentioned yet):

public static boolean isValid(String str) {
    try {
        Double.parseDouble(str);
        return str.length() == 14;
    } catch(NumberFormatException e) {
        return false;
    }
}

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.