forum

Home / DeveloperSection / Forums / Taking a string and printing only the digits

Taking a string and printing only the digits

Samuel Fernandes 1841 29-Apr-2015
This is the code that I have so far, It will only print out the digit if it is entered first..How to I get it to print out all digits? and I am getting an error that the c is not initialized
import java.util.*;
 
//Write a program that prompts the user for some text.  Output only the digits in that text.  Hint:  Use a loop and the Character.isDigit method.
public class Q1 
{
     
    public static void main (String[] args)
    {
        String user_input;
        char c;
        Scanner keyboard = new Scanner (System.in);
         
        user_input = keyboard.nextLine();
         
        for( int i=0; i<user_input.length(); i++)
        {
        c = user_input.charAt(i);
        }
         
        if(Character.isDigit(c))
        {
            System.out.println(c);
        }      
          
    }   
}//end code

Updated on 29-Apr-2015

Can you answer this question?


Answer

1 Answers

Liked By