Help in writing a java application.

Status
Not open for further replies.

maroon1

Banned
Messages
3,479
Our programming teacher asked as to write a Java application for a problem in our book, but I didn't know how to write it. Please can someone help

this is the problem
Write an application that inputs one number consisting of five digits from the user, separates the number into its individual digits and prints the digits separated from one another by three spaces each. For example, if the user types in the number 42339, the program should print
PHP:
4   2   3   3   9
 
So what do you want help with? We're not going to do your homework for you. Start coding and when you run into a specific problem, post your code.
 
jaeusm said:
So what do you want help with? We're not going to do your homework for you. Start coding and when you run into a specific problem, post your code.

I'm noob in programming.

My problem is that I don't know how to separate the digits by three spaces

I have wrote this program


import java.util.Scanner;

public class Digits

{ public static void main(String args[])

{

Scanner input=new Scanner(System.in);



int number;



number=input.nextInt();



System.out.printf("%d",number);

}
}

but this one ^^ doesn't display the digits separated by three spaces
 
You're on the right track, but you need to add a couple things. First, it would make your life easier to read in user input as a string rather than an integer. So, just use scanner.next() method, which returns a string. At that point, you now have a string representation of the number the user entered. Using a loop, print each character of the string with three spaces concatenated to it.
Code:
System.out.print(character + "   ");
 
Status
Not open for further replies.
Back
Top Bottom