English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

How do I write an application that prompts the user to input a five digit number, and prints the digits separated from each other by three spaces each? (For example, the user enters "61345" and the program prints "6 1 3 4 5").

Here is the initial program skeleton to work from:

import java.util.Scanner;

public class SpaceNumber {
public static void main(String args[]) {

// get a number from user - store in variable called number
Scanner input = new Scanner(System.in);
System.out.print( "Enter a positive, 5-digit integer: " );
int number = input.nextInt();

// Notes: (1) above code assumes user actually enters an integer;
// (2) code below assumes the integer has 5 digits and is > 0.


//YOUR SOLUTION HERE


}
}

I'd really appreciate the help. I'm just starting java and the textbook doesn't clearly specify how to complete this problem.

2006-10-16 09:44:48 · 5 answers · asked by boatboatboat 1 in Computers & Internet Programming & Design

5 answers

Your problem isn't a java problem. You're being asked to solve a problem. What your instructor wants you to do is to figure out how to break down a number into digits. You can do this using integer division and some subtraction. So, isn't 61345 / 10000 == 6? Once you get the 6, you can repeat the process with 1345 to get the 1, and so on. Give it a shot.

2006-10-20 07:46:35 · answer #1 · answered by arbeit 4 · 0 0

Look into
java.lang.StringBuffer
for modifiable string and in-place insertion of space and convertion to regular string
java.lang.Integer
for turning an integer to a string

I'm not writing the entire program for you
StringBuffer buffer = new StringBuffer(new Integer(number).toString());
buffer.insert(4, " ");
System.out.println(buffer.toString());

2006-10-16 16:58:20 · answer #2 · answered by Andy T 7 · 0 0

Go to sun.com or mozilla.com,plenty of Open Source Java writeups there. Firefox also has a nice repository.

2006-10-16 16:56:16 · answer #3 · answered by Anonymous · 0 0

dude try to use arraysto store each input and print each value of array without using prinln , e.g using system.out.print(array[1]+" "+array[2]+" " + ...araay[5])"

2006-10-16 16:51:06 · answer #4 · answered by Talha R 2 · 0 0

try this:
public class SpaceNumber
{
public static void main (String argv[])
{
try
{
System.out.println (
new java.io.BufferedReader(
new java.io.InputStreamReader (
System.in)).readLine ().
replaceAll ("(\\d)", "$1   "));
}
catch (Exception e) {e.printStackTrace ();}
}
}

2006-10-16 17:24:32 · answer #5 · answered by n0body 4 · 0 0

fedest.com, questions and answers