I want help in writing a digit display(3 digit number) in java (using sub strings).
Example 524 if user inputs then computer has to display
5
2
4.

Recommended Answers

All 9 Replies

is 524 n int or a Strng ?
If it is a String

String str = "524";
for(int i = 0; i < str.length(); ++i)
  System.out.printl(str.charAt(i));

I want help in writing a digit display(3 digit number) in java (using sub strings).
Example 524 if user inputs then computer has to display
5
2
4.

import java.util.*;

public class InputSub
{
	public static void main(String[] args)
	{
		Scanner sc = new Scanner(System.in);
		System.out.print("Number Input: ");
		String userInput= sc.nextLine();
		System.out.println();
		System.out.println("Output separated");
		for(int i = 0; i < userInput.length();i++)
		{
			System.out.println(userInput.charAt(i));
		}

	}


}

I tried but it does not give the output.

What output does it give? Did you enter anything?

Gentlemen, or ladies, as appropriate, let's please try to be a little subtle here. Here's a New Year's resolution for you: in the coming year, I will help people find a solution, not give them their solution.

Now, javap - what have you tried so far, and what has been the result?

I tried but it does not give the output.

hey man, Im posting the code again.I dont know why it wouldnt work on yours try looking at the end of your for loop you might accidentaly typed a semicolon on your loop. example: for(int i= 0; i < userInput.length();i++); <-------- see the semi-colon in the end?, thats wrong, that would not execute any codes under the for loop. just copy and paste the code that I wrote, then save it as the same class name "InputSub". I dont know why it shouldnt work.. or you can paste parts of this code into your codes.

import java.util.*;

public class InputSub
{
	public static void main(String[] args)
	{
		Scanner sc = new Scanner(System.in);
		System.out.print("Number Input: ");
		String userInput= sc.nextLine();
		System.out.println();
		System.out.println("Output separated");
		for(int i = 0; i < userInput.length();i++)
		{
			System.out.println(userInput.charAt(i));
		}

	}


}

JKP - Please keep in mind that "code" is a mass noun. It's never plural.

Saying "it won't execute any codes" is like saying "I slipped and fell into a puddle, and when I got up, I was covered with muds".

Thanks a lot. I got it.

JKP - Please keep in mind that "code" is a mass noun. It's never plural.

Saying "it won't execute any codes" is like saying "I slipped and fell into a puddle, and when I got up, I was covered with muds".

Thanks for the info jon.. Im not really that good in english so I thank you for correcting me. This site is so great. Not only you learn how to write java " code",you'll also learn some proper english. Thank you again. Code is a mass noun, never plural, got it.

I hope the code that I wrote helped our buddy javap solve his prob.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.