---
title: "Taking a string and printing only the digits"  
description: "Taking a string and printing only the digits"  
author: "Samuel Fernandes"  
published: 2015-04-29  
updated: 2015-04-29  
canonical: https://www.mindstick.com/forum/23160/taking-a-string-and-printing-only-the-digits  
category: "java"  
tags: ["java"]  
reading_time: 2 minutes  

---

# Taking a string and printing only the digits

This is the [code](https://yourviews.mindstick.com/view/85458/alan-turing-the-mastermind-behind-cracking-the-enigma-code-during-world-war-ii) that I have so far, It will only [print](https://www.mindstick.com/blog/301752/types-of-3d-printing-technology) out the digit if it is entered first..How to I get it to print out all digits? and I am [getting an error](https://answers.mindstick.com/qa/95244/why-do-you-keep-getting-an-error-message-ox800ccc0b) that the c is not initialized\
import java.util.*; //Write a [program](https://www.mindstick.com/blog/12337/scaling-up-your-mentorship-program) that prompts the [user](https://www.mindstick.com/articles/13001/multi-statement-table-valued-user-defined-function-in-sql-server) for some text. [Output](https://www.mindstick.com/interview/34427/explain-the-output-in-angular) 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
```

## Replies

### Reply by Anonymous User

You print statement does not appear in your for-loop. This means that there will only ever be at-most one print statement executed. If you want to print out every digit then you need to move the print statement into the for-loop.\
\
Are 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\

```
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 = 0;        Scanner keyboard = new Scanner (System.in);        System.out.println("Enter some text:");        user_input = keyboard.nextLine();                 System.out.println("Here are the digits in that text: ");        for( int i=0; i<user_input.length(); i++)        {        c = user_input.charAt(i);                 if(Character.isDigit(c))        {            System.out.print(c);        }              }              }  
```


---

Original Source: https://www.mindstick.com/forum/23160/taking-a-string-and-printing-only-the-digits

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
