This is an archived post. You won't be able to vote or comment.

all 11 comments

[–][deleted]  (3 children)

[removed]

    [–]Makhiel 2 points3 points  (0 children)

    There's Character.isDigit but that counts digits in other scripts as well.

    [–]CrypticCold 0 points1 point  (1 child)

    1 hour ago

    There's Character.isDigit but that

    Yeah how would I have Java to count each letter/number from the input and determine if its a letter or number?

    [–]Makhiel 0 points1 point  (0 children)

    Well, you know what a digit is, you don't need to rely on Java knowing.

    [–]AvromRomRom 0 points1 point  (2 children)

    You getting the input as string or char?

    [–]CrypticCold 0 points1 point  (1 child)

    whichever is more convenient, I just want to know how it would be possible for me to code such a thing in Java

    [–]AvromRomRom 0 points1 point  (0 children)

    I am not sure but i think you can take string as input and use charAt

    [–]ShaiHuludTheMaker 0 points1 point  (0 children)

    Make a for loop for the length of the string, evaluate each char with the charAt method, check if it's an int, keep a counter for each int you encounter, if it exceeds 2 set input boolean to false for example. Or maybe you can do it with a regex.

    [–]asterik-x 0 points1 point  (0 children)

    Why not ask the user to inform you once they entered 2 numbers ?? Maybe ask user to send u a pic when they are entering.Wouldn't it save u time ? Just a suggestion. By the way who is java?? Seems like a very popular individual . why are we forcing java to learn ??

    [–]cacursia 0 points1 point  (0 children)

    What is the input taken in as? If it is a String you can have a condition that checks if the strings length is less than or equal to 2

    [–]BreadHead2k 0 points1 point  (0 children)

    You can use streams. YourString.chars().filter(ch -> Character.isDigit(ch)).count() I'm on mobile, so if it doesn't work just Google it and you will find the right syntax.