d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Java Help! > Separating Numbers
12Next
Add Reply New Topic New Poll
Member
Posts: 2,431
Joined: Dec 27 2009
Gold: 1,356.00
Mar 27 2014 01:43pm
i have to enter in a five digit number
12345
and have it make it say
1 2 3 4 5
and separate all the numbers.

how would i do that?
Member
Posts: 2,757
Joined: Nov 26 2007
Gold: 1,214.81
Mar 27 2014 02:04pm
Code
String a = "12345";
String b = "";
for ( int i = 0; i < a.length( ); i++ ) {
b += a.charAt( i );
if ( i + 1 <= a.length( ) ) {
b += " ";
}
}


or just
Code
String a = "12345";
a = a.replace( "", " " );


This post was edited by labatymo on Mar 27 2014 02:06pm
Member
Posts: 2,431
Joined: Dec 27 2009
Gold: 1,356.00
Mar 28 2014 11:47am
Quote (labatymo @ Mar 27 2014 01:04pm)
Code
String a = "12345";
    String b = "";
    for ( int i = 0; i < a.length( ); i++ ) {
      b += a.charAt( i );
      if ( i + 1 <= a.length( ) ) {
        b += " ";
      }
    }


or just
Code
String a = "12345";
    a = a.replace( "", " " );


thanks
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Mar 30 2014 06:07pm
Quote (labatymo @ Mar 27 2014 03:04pm)

Code
String a = "12345";
    a = a.replace( "", " " );


I did not know this worked like this. I find this really cool. Thanks :)
Member
Posts: 2,431
Joined: Dec 27 2009
Gold: 1,356.00
Apr 15 2014 08:53am
Quote (labatymo @ Mar 27 2014 01:04pm)
Code
String a = "12345";
    String b = "";
    for ( int i = 0; i < a.length( ); i++ ) {
      b += a.charAt( i );
      if ( i + 1 <= a.length( ) ) {
        b += " ";
      }
    }


or just
Code
String a = "12345";
    a = a.replace( "", " " );


with the top[ one its giving me an error sign saying "i cannot be resolved into a variable"
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Apr 15 2014 09:46am
Seems to me you got yourself one of them thar syntax errors all thekids been talktalking about. My guess is a stray semi colon at the end of the for loop statement closing it off, so the block underneath has no definition of i.


Of course, ny pointless speculation would be unnecessary if you posted your code.
Member
Posts: 2,431
Joined: Dec 27 2009
Gold: 1,356.00
Apr 15 2014 09:59am
Quote (Minkomonster @ Apr 15 2014 08:46am)
Seems to me you got yourself one of them thar syntax errors all thekids been talktalking about. My guess is a stray semi colon at the end of the for loop statement closing it off, so the block underneath has no definition of i.


Of course, ny pointless speculation would be unnecessary if you posted your code.


how do i post up my code like he did?
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Apr 15 2014 10:36am
Quote (GrafEmpire @ Apr 15 2014 10:59am)
how do i post up my code like he did?


Copy and paste. There's a button on the reply toolbar that wraps text in code blocks
Member
Posts: 2,431
Joined: Dec 27 2009
Gold: 1,356.00
Apr 16 2014 02:17pm
[CODE/**
*
*/

/**
* @author Dylan.Pettigrew
*
*/
public class seperating {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int i;

String a = "12345";
String b = "";

for ( int 1 = 0; i < a.length(); i++)
b += a.charAt( i );
if ( 1 + 1 <= a.length( ) )
b += " ";


}

}
][/CODE]
Member
Posts: 2,431
Joined: Dec 27 2009
Gold: 1,356.00
Apr 16 2014 02:18pm
Quote (GrafEmpire @ Apr 16 2014 01:17pm)
[CODE/**
*
*/

/**
* @author Dylan.Pettigrew
*
*/
public class seperating {

/**
  * @param args
  */
public static void main(String[] args) {
  // TODO Auto-generated method stub
  int i;
 
  String a = "12345";
  String b = "";
 
  for ( int 1 = 0; i < a.length(); i++)
  b += a.charAt( i );
  if ( 1 + 1 <= a.length( ) )
  b += " ";
 
 
}

}
][/CODE]


im just learning how to do this and i probably messed this up.
but im looking for constructive criticism
Go Back To Programming & Development Topic List
12Next
Add Reply New Topic New Poll