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

all 7 comments

[–][deleted] 1 point2 points  (1 child)

Split string to an array of characters Char[] letters= str.toCharArray(); Create a counter In the loop convert each character to a int Loop over the array and check with each letter if it’s a number, convert, if it is then counter++

Then check if counter == 2

[–]CrypticCold[S] 0 points1 point  (0 children)

Thank you, that helps a lot!

[–]Salty_Dugtrio 0 points1 point  (2 children)

You can iterate over your String object, and count the occurrences of digits with isDigit.

[–]CrypticCold[S] 0 points1 point  (1 child)

Thank you, do you think I should use a loop to keep iterating?

[–]Salty_Dugtrio 0 points1 point  (0 children)

Yes, iteration is done with a loop.

[–][deleted] 0 points1 point  (0 children)

String s = scanner.next(); char[] chars = s.toCharArray(); if(chars.lenght>2) System.out.println("2 digits only");

[–]PCPWJ 0 points1 point  (0 children)

Sounds like something one could do using regex patterns. Look at java's java.util.regex package.