d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > [java] Basic Question
Add Reply New Topic New Poll
Member
Posts: 69,869
Joined: Aug 26 2003
Gold: 32,179.00
Sep 8 2012 04:24am
I want to make a prog like this:

<Ask the user to write, let's say 5 digits. For example "12345">

<Print it out with 3 spaces between each digit like "1 2 3 4 5">


So I how can I split the 5 digits to get 5 different variabels?

I'm pretty new in java, but have to start somewhere ;)
Thx in advance :)
Member
Posts: 13,630
Joined: Dec 4 2009
Gold: 0.00
Sep 8 2012 09:11am
You could modify the input a little. You can require that the digits are delimited. That way, you can replace the delimiter with three spaces.

I'm not sure you can iterate through strings, though, so you wouldn't be able to just add the spaces on each iteration instead of delimiting the string. Look up the string formatting functions. You might find what you need.
Member
Posts: 69,869
Joined: Aug 26 2003
Gold: 32,179.00
Sep 8 2012 09:31am
I finally did it without using any String methods :)

I used the modular (%) to split them :banana:

Thx for suggestiosn anyway :thumbsup:
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Sep 8 2012 10:36am
imo i'd treat it as a string instead of a number.

convert the string input to a char array, then loop through the char array printing it and 3 spaces
Member
Posts: 69,869
Joined: Aug 26 2003
Gold: 32,179.00
Sep 8 2012 11:15am
Quote (carteblanche @ 8 Sep 2012 18:36)
imo i'd treat it as a string instead of a number.

convert the string input to a char array, then loop through the char array printing it and 3 spaces


yes this is the easiest solution.

but in this task I'm not allowed to do this as a String :)

This post was edited by B-BoyX on Sep 8 2012 11:16am
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll