trying to convert an int into string

String M;

if (milli2 == 1000)

{

// String M = new Integer(milli2).toString();

String a = milli2 + "";

String a = "M";

}

else if (milli2 == 2000)

{

//String (M + M) = new Integer(milli2).toString();

String a = milli2 + "";

String a = "MM";

}

else if (milli2 == 3000)

{

//String (M + M + M) = new Integer(milli2).toString();

String a = milli2 + "";

String a = "MMM";

}

else

System.out.println("here is your number " + a);

I have tried a few different ways to do this, none are working. Essentially milli2 has previously been initialized, it gets a number. Depending on the number I want it to get a M, MM or MMM.

Thanks in advance

Will

[781 byte] By [grjmmra] at [2007-11-26 18:46:53]
# 1
static int parseInt(String s)
qUesT_foR_knOwLeDgea at 2007-7-9 6:20:49 > top of Java-index,Java Essentials,New To Java...
# 2

Consider using a switch statement

//...

String a;

switch(milli2)

{

case 1000: a = "M"; break;

case 2000: a = "MM"; break;

case 3000: a = "MMM"; break;

default: a = "DEFAULT_VALUE"; break;

}

System.out.println("here is your number " + a);

//...

#

duckbilla at 2007-7-9 6:20:49 > top of Java-index,Java Essentials,New To Java...
# 3
Thanks, I had written a switch statement earlier but I made a small error. So not only does the program run but I learned to correctly write a switch line also!Will
grjmmr1a at 2007-7-9 6:20:49 > top of Java-index,Java Essentials,New To Java...
# 4

You could just use Integer.toString(int n)

or maybe even toString() alone will work for you.

int i;

String n;

String n = i.toString();

I'm pretty sure Java already has pre-built number-to-String conversions. There should be no reason to code your own.

Edit: N/m me ... helps if I read the entire question first.

JJCoolBa at 2007-7-9 6:20:49 > top of Java-index,Java Essentials,New To Java...
# 5

> Thanks, I had written a switch statement earlier but

> I made a small error. So not only does the program

> run but I learned to correctly write a switch line

> also!

>

> Will

You're welcome. It is important to notice how I declared the String variable _outside_ the switch-block, so as to make it visible to the System.out.println() call.

duckbilla at 2007-7-9 6:20:49 > top of Java-index,Java Essentials,New To Java...