• Post Reply Bookmark Topic Watch Topic
  • New Topic

What method to use to see if a string contains at least one digit?  RSS feed

 
Shane Williams
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all!

Quick question - What method can I use to see if a string contains at least one digit? The .contains method doesn't seem to work.

P.S. VERY new to Java, so take it easy.

Thanks in advance.
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can try a regular expression, but that means reading the whole of a tutorial.

I have forgotten most of what I knew about regular expressions but you could try ".*\\d.*"
The dot means anything and the * means any number of times. The \\ is escape for backslash and \d means digit. So that is anything any number of times then digit then anything any number of times. Any number includes 0.
 
Carey Brown
Saloon Keeper
Posts: 3328
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:You can try a regular expression, but that means reading the whole of a tutorial.

I have forgotten most of what I knew about regular expressions but you could try ".*\\d.*"
The dot means anything and the * means any number of times. The \\ is escape for backslash and \d means digit. So that is anything any number of times then digit then anything any number of times. Any number includes 0.

Your suggestion works for matches() and find(). If you can accept only using find() then "\\d" will suffice.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:
Your suggestion works for matches() and find(). If you can accept only using find() then "\\d" will suffice.


I think the OP may be asking about the String class (or at least, implying it), so, if true, the find() method is not available.

Henry
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if there is no requirement to use String methods directly, then find("\\d") will probably do nicely. Thank you.

If there is a requirement to use String methods, then we shall have to think again.
 
Carey Brown
Saloon Keeper
Posts: 3328
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
Carey Brown wrote:
Your suggestion works for matches() and find(). If you can accept only using find() then "\\d" will suffice.


I think the OP may be asking about the String class (or at least, implying it), so, if true, the find() method is not available.

Henry

I guess you're right. I was thinking of the regular expression package.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
If there is a requirement to use String methods, then we shall have to think again.


The matches() method, already mentioned, is available.

Henry
 
Charles D. Ward
Ranch Hand
Posts: 99
1
  • Likes 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Personally, the last thing I'd recommend to a fellow beginner to use are regular expressions. Learning Java by itself is overwhelming enough already.

Shane: the Character class has a static isDigit() method that takes a single char as an argument and returns a boolean depending on if such char represents a digit. You could try writing a method that loops through each character of the provided String and check if it's a digit.
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can we do it with a Stream?Yes, you can. But you cannot directly create a Stream<char> nor a Stream<Character> from a char[] (as in the toCharArray method). I think you get a Stream<char[]>. You can't create a List<Character> with Arrays.asList(s.toCharArray()). You get a List<char[]> which is different. Similar problem to earlier attempt. So I ended up using a plain simple for‑each loop to add the individual chars to a List<Character>.

And here, Streams fall flat on their faces and fail miserably. Possibly because we are here dealing with primitives. Even though I got “correct” printouts.
 
Ole Sandum
Ranch Hand
Posts: 76
3
IntelliJ IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Can we do it with a Stream?Yes, you can. But you cannot directly create a Stream<char> nor a Stream<Character> from a char[] (as in the toCharArray method). I think you get a Stream<char[]>. You can't create a List<Character> with Arrays.asList(s.toCharArray()). You get a List<char[]> which is different. Similar problem to earlier attempt. So I ended up using a plain simple for‑each loop to add the individual chars to a List<Character>.

And here, Streams fall flat on their faces and fail miserably. Possibly because we are here dealing with primitives. Even though I got “correct” printouts.


Campbell. In Java 8 String has a method called chars(), which returns an IntStream of the characters in the String. (yes, primitive Streams do exist )
Your code snippet could be simplified to:
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aaaaaaaaaaaaaaaaaaaaaaaaaah! I thought there ought to be a method in Strings to get a Stream.
Can you get ::isDigit to work? It takes a char as its parameter. Oh, I see Character#isDigit is overloaded and will take an int called codePoint, so that method will work nicely.

Once you know about the chars() method, you get a much more elegant solution than what I managed yesterday
 
Rico Felix
Ranch Hand
Posts: 411
5
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To be aware of alternatives you can also do the following:
 
Shane Williams
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Charles D. Ward wrote:Personally, the last thing I'd recommend to a fellow beginner to use are regular expressions. Learning Java by itself is overwhelming enough already.

Shane: the Character class has a static isDigit() method that takes a single char as an argument and returns a boolean depending on if such char represents a digit. You could try writing a method that loops through each character of the provided String and check if it's a digit.


This seems to be the easiest of all the answers. I will give it a go. Thanks all!
 
Shane Williams
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Charles D. Ward wrote:Personally, the last thing I'd recommend to a fellow beginner to use are regular expressions. Learning Java by itself is overwhelming enough already.

Shane: the Character class has a static isDigit() method that takes a single char as an argument and returns a boolean depending on if such char represents a digit. You could try writing a method that loops through each character of the provided String and check if it's a digit.


Sorry to bother you again, but how might I go about this? It isn't for school so you can give me the actual answer.
 
Rico Felix
Ranch Hand
Posts: 411
5
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Simply create a loop to iterate through the length of the string and with each iteration you check if the character at the current position is a digit or not...
The following methods will be needed:
String::length()
String::charAt(int)
Character::isDigit(char)
 
Shane Williams
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rico Felix wrote:Simply create a loop to iterate through the length of the string and with each iteration you check if the character at the current position is a digit or not...
The following methods will be needed:
String::length()
String::charAt(int)
Character::isDigit(char)


Thanks a lot! Now I feel stupid though...
 
Rico Felix
Ranch Hand
Posts: 411
5
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are far from stupid though... Its a simple matter of unfamiliarity with a new environment... You will get along quite fine in due time
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!