tag:blogger.com,1999:blog-8712770457197348465.post8073684355657149875..comments2024-03-28T02:47:19.159-07:00Comments on Javarevisited: How to check if a String is Number in Java - Regular Expression Examplejavin paulhttp://www.blogger.com/profile/15028902221295732276noreply@blogger.comBlogger17125tag:blogger.com,1999:blog-8712770457197348465.post-90129852573195637512020-02-09T09:15:59.170-08:002020-02-09T09:15:59.170-08:00public static void main(String[] args) {
S...public static void main(String[] args) {<br /> String strNumber = "123456";<br /> try{<br /> long number = Long.parseLong(strNumber);<br /> System.out.println("It is a number:-"+number );<br /> }catch (NumberFormatException|NullPointerException e){<br /> System.out.println("It is not a number");<br /> }<br srinuhttps://www.blogger.com/profile/01414524634664763786noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-48296693821193380522020-01-26T12:16:20.320-08:002020-01-26T12:16:20.320-08:00I don't no whats the correct structure of this...I don't no whats the correct structure of this programAnonymoushttps://www.blogger.com/profile/15969593295607346212noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-33507509896078556662019-07-25T20:07:24.349-07:002019-07-25T20:07:24.349-07:00private static boolean onlyDigits(String s) {
St...private static boolean onlyDigits(String s) {<br /> String regex = "[0-9]";<br /> return (s.replaceAll(regex, "").length() == 0);<br /> }Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-55430256924134896452019-03-09T19:32:36.112-08:002019-03-09T19:32:36.112-08:00Change in the regular expression in 0 to 9Change in the regular expression in 0 to 9Anonymoushttps://www.blogger.com/profile/06752682470587800324noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-64891349230714961962019-02-23T09:40:23.508-08:002019-02-23T09:40:23.508-08:00-123 is also number but it returns false, how to ...-123 is also number but it returns false, how to handled itDVhttps://www.blogger.com/profile/09148317961780213954noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-7338463235524446832018-10-19T23:54:19.773-07:002018-10-19T23:54:19.773-07:00package String;
public class Numberstrig {
pu...package String;<br /><br />public class Numberstrig {<br /> public static void main(String [] args){<br /> String s ="12345";<br /> if(s.matches("[0-9]+")){<br /> System.out.println("The given string is an numeric string");<br /> }else{<br /> System.out.println("The given string is not a numeric string");<br />Anonymoushttps://www.blogger.com/profile/07761789280063852433noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-22292203857480745732018-10-19T23:53:50.347-07:002018-10-19T23:53:50.347-07:00public class Number {
public static void main(...public class Number {<br /> public static void main(String [] args){<br /> String s ="123a45";<br /> int count =0;<br /> for(int i=0;i<s.length();i++){<br /> if(Character.isDigit(s.charAt(i))){<br /> count++;<br /> }<br /> }<br /> if(count==s.length()){<br /> System.out.println("The given Anonymoushttps://www.blogger.com/profile/07761789280063852433noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-87164763672220261782018-06-28T23:52:37.893-07:002018-06-28T23:52:37.893-07:00public static bool IsDigit(string strIsDigit)
... public static bool IsDigit(string strIsDigit)<br /> {<br /> <br /> foreach(char c in strIsDigit)<br /> {<br /> if (c < 48 || c > 57)<br /> return false;<br /> }<br /> return true;<br /> }EthZenahttps://www.blogger.com/profile/15981405554043877042noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-85059018202969945102017-01-10T06:12:45.815-08:002017-01-10T06:12:45.815-08:00private static void checkStringContainsOnlyDigits(...private static void checkStringContainsOnlyDigits(String string) {<br /> try{<br /> Double doub = Double.parseDouble(string);<br /> System.out.println(doub);<br /> } catch(NumberFormatException e){<br /> System.out.println(" given string is alpha numeric");<br /> }<br /> <br /> <br /> }Anonymoushttps://www.blogger.com/profile/00363822071475466562noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-66690517513990392842016-12-09T21:39:57.363-08:002016-12-09T21:39:57.363-08:00Your regex is incorrect. It should be ".*^[0-...Your regex is incorrect. It should be ".*^[0-9]*." rather than ".*^[0-9].*"Tarhttps://www.blogger.com/profile/02167894975152969868noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-70279540911255396012016-08-10T18:11:33.244-07:002016-08-10T18:11:33.244-07:00@Anonymous, it's not possible to give a Java p...@Anonymous, it's not possible to give a Java program input like that i.e. {25, 45}, though you can write little bit code to achieve that. Since that is String, just write a parser which reads this string, split it and create an array of String. Alternatively, you can also check this post to learn <a href="https://javarevisited.blogspot.com/2016/07/javin paulhttps://www.blogger.com/profile/15028902221295732276noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-63027810291484422902016-08-10T12:43:33.222-07:002016-08-10T12:43:33.222-07:00I have query
I want to give input like this {25,4...I have query <br />I want to give input like this {25,45} using array is it possible. for the main function i need to use numbers only.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-86482577060831697602016-08-09T01:56:37.946-07:002016-08-09T01:56:37.946-07:00Scanner scn = new Scanner(System.in);
boolean fl...Scanner scn = new Scanner(System.in);<br /> boolean flag = false;<br /> System.out.println("Enter digits");<br /> try {<br /> Integer i = scn.nextInt();<br /> } catch(InputMismatchException ie) {<br /> flag = true;<br /> }<br /> if(!flag) {<br /> System.out.println("All are digits");<br /> } else {<br /> System.out.println("All entries are not digit")Anonymoushttps://www.blogger.com/profile/04477309282229399859noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-14174190669559373522016-02-13T18:27:46.851-08:002016-02-13T18:27:46.851-08:00public boolean containsOnlyDigits(String s) {
i...public boolean containsOnlyDigits(String s) {<br /> <br /> if (s.isEmpty()) return false;<br /> <br /> for (int i = 0; i < s.length(); i++) {<br /> char temp = s.charAt(i) - '0';<br /> if (temp < 0 || temp > 9)<br /> return false;<br /> }<br /> <br /> return true;<br />}Anonymoushttps://www.blogger.com/profile/04690105156692949500noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-79520148268769998692016-01-09T16:23:44.514-08:002016-01-09T16:23:44.514-08:00How about this solution:
public static boolean isD...How about this solution:<br />public static boolean isDigit(char c){<br /> int x=(int)c-(int)'0';<br /> if(x<0||x>9){<br /> return false;<br /> }<br /> return true;<br /> <br /> }<br /> <br /> public static boolean isOnlyDigit(String s){<br /> <br /> for(int i=0;i<s.length();i++){<br /> if(!isDigit(s.charAt(i))){<br /> return false;<br /> }<br /> }<br /> return Anonymoushttps://www.blogger.com/profile/16255195707119582170noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-59193093631220556392013-09-30T03:19:05.769-07:002013-09-30T03:19:05.769-07:00Hello Javin, Can you please share How to ignore ce...Hello Javin, Can you please share How to ignore certain digits from any number e.g. I want to ignore only non zero digits, please suggest?Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-12499388000720178312012-10-20T11:32:29.678-07:002012-10-20T11:32:29.678-07:00Nice information indeed. But, if your application ...Nice information indeed. But, if your application is small and you don't use pattern matching very frequently, String class's matches() method is a shortcut for above.<br /><br /> //Java docs<br /> public boolean matches(String regex) {<br /> return Pattern.matches(regex, this);<br /> }<br /><br />Please correct me if i am wrong.Anonymousnoreply@blogger.com