Print Only Numbers From Java Strings

I have a question in Java-string,
String s="adf5hgjyu1frdsff6vgyu7bnjh9asd3"; like
How to print only numbers in that string

Showing Answers 1 - 12 of 12 Answers

Amarendra

  • Oct 6th, 2015
 

Code
  1.  

  2. public static void main(String[] args) {

  3.                 String s = "adf5hgjyu1frdsff6vgyu7bnjh9asd3";

  4.                 char[] sarr = s.toCharArray();

  5.                 for (int i = 0; i < sarr.length; i++) {

  6.                         if(sarr[i]/1>=48 && sarr[i]/1<=57){

  7.                                 System.out.print(sarr[i]+", ");

  8.                         }else{

  9.                                 //System.out.print(sarr[i]+", ");

  10.                         }

  11.                 }

  12.         }

  Was this answer useful?  Yes

amrendra

  • Oct 6th, 2015
 

Check with ascii codes

Code
  1. public static void main(String[] args) {

  2.                 String s = "adf5hgjyu1frdsff6vgyu7bnjh9asd3";

  3.                 char[] sarr = s.toCharArray();

  4.                 for (int i = 0; i < sarr.length; i++) {

  5.                         if(sarr[i]/1>=48 && sarr[i]/1<=57){

  6.                                 System.out.print(sarr[i]+", ");

  7.                         }else{

  8.                                 //System.out.print(sarr[i]+", ");

  9.                         }

  10.                 }

  11.         }

  Was this answer useful?  Yes

rawatfeb

  • Feb 23rd, 2016
 

private static void NumericsFromStringTest() {
String alphaNumericString = "h8ll0";
char[] carray = alphaNumericString.toCharArray();
for (char c : carray) {
if (Character.isDigit(c)) {
System.out.println(c);
}
}
}

Code
  1.         private static void NumericsFromStringTest() {

  2.                 String alphaNumericString = "h8ll0";

  3.                 char[] carray = alphaNumericString.toCharArray();

  4.  

  5.                 for (char c : carray) {

  6.  

  7.                         if (Character.isDigit(c)) {

  8.  

  9.                                 System.out.println(c);

  10.                         }

  11.  

  12.                 }

  13.  

  14.         }

  Was this answer useful?  Yes

Vincent

  • Jan 11th, 2018
 

Run someCode() method from main()

Code
  1.  

  2.  

  3. public static void someCode(){

  4.                

  5.                 String s="adf5hgjyu1frdsff6vgyu7bnjh9asd3";

  6.                 int length=s.length();

  7.                 char ch;int n;

  8.                 for(int i=0;i<length;i++){

  9.                         ch=s.charAt(i);

  10.                         if(Character.isDigit(ch)){

  11.                                 n=Character.getNumericValue(ch);

  12.                                 System.out.println("the number is "+n+" and it is "+checkPrime(n));

  13.                         }

  14.                        

  15.                 }

  16.                

  17.         }

  18.  

  19.         public static String checkPrime(int n){

  20.                

  21.                 if(n==1||n==2||n==3){return "Prime";}

  22.                 for(int i=2;i<n;i++){

  23.                         if(n%i==0)

  24.                                 return "Not Prime";

  25.                 }

  26.                 return "Prime";

  27.         }

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions