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
Anonymous User
29-Apr-2015Are you sure it only prints digits that appear first though? It looks to me like the local variable 'c' will only ever contain the final character from the String