Skip to main content
gamedev.net
🔒 Locked

java beginner

Started by grjmmr Feb 16, 2007 at 6:01 PM 7 replies 1.7k views
Original Post
grjmmr
grjmmr
I am learning Java I wrote something like this String m = "M"; int thou; if (milli2 == 1000) { thou = m; } else if (milli2 == 2000) { thou = m + m ; } else if (milli2 == 3000) { thou = m + m + m; } else System.out.println("here is your number " + milli2); Essentially earlier I initialized milli2 and it will get a number that is 1000, 2000 or 3000. I need to change from int to string and I have been trying to concatanate. Any ideas how to convert these ints to strings and then print them out. Thanks Brett
dave
dave
Google
Raghar
Raghar
int is a primitive, String is an immutable object. Mixing these two is not recommended as primitives are for speed.

Read documentation for StringBuilder.
Sneftel
Sneftel
Moved to For Beginners.
Sausie
Sausie
I dont see what you are trying to do...

You're testing milli2 for truth and then u assign a String to an integer.

Why dont you use the concat() method thats already in the String object?

Here's the entry in the documentation:

String concat(String str)
Concatenates the specified string to the end of this string.

Don't forget a String is an object and not a primitive datatype like int.
wijnand
wijnand
Quote:
Original post by grjmmr
I am learning Java

I wrote something like this

String m = "M";
int thou;


if (milli2 == 1000)
{
thou = m;
}
else if (milli2 == 2000)
{
thou = m + m ;
}
else if (milli2 == 3000)
{
thou = m + m + m;
}

else



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

Essentially earlier I initialized milli2 and it will get a number that is 1000, 2000 or 3000. I need to change from int to string and I have been trying to concatanate. Any ideas how to convert these ints to strings and then print them out.

Thanks

Brett


parseintegertostring use that function call in JAva, I completly forgot the exact syntax and name but i know there is a function to change a integer to a string and viceversa. but what exactly is the goal of this program? and maybe you can use Switch looks a lot cleaner then a few if/else statements plus it gives a default variable you can fill in incase someone just writes garbage :)
LemonSHARK
LemonSHARK
An int is a primative, but an Integer is an object.
A String is an object too.

To convert from a string to an integer:

String a = "1000";
int b = 5;
System.println("b is currently " + b);
b = Integer.parseInt(a);
System.println("B is now " + b);

To convert from an int to a string:

int c = 1000;
Integer d = c;
String e = "one thousand";
System.out.println("e is currently '" + e + "'");
e = d.toString();
System.out.println("e is now '" + e + "'");


If you write:

thou = m;

You'll get a type mismatch. If you are not already using it, download eclipse for free and it'll be very helpful at finding problems like this.
Zahlman
Zahlman
Are you trying to convert to Roman numerals or something?

Pay close attention to the type of your variable 'thou': it currently is 'int'. Concatenating Strings with the '+' operator *does* work like that in Java, but the result of concatenating two Strings is another String (logically enough), and you can't assign the String back to an int typed variable. You want 'String thou;' instead.

(Also, I think you mean 'mille', with an e for the variable name - the Latin for "thousand". 'milli' is the SI (metric system of measurements) prefix for "one thousandth".)
Clawer
Clawer
Quote:
Original post by grjmmr

I need to change from int to string and I have been trying to concatanate. Any ideas how to convert these ints to strings and then print them out.

Thanks

Brett


int a = 5; // the integerString myNiceString; // the String// we assign to myNiceString a formatted new String with the integer insidemyNiceString = String.format( "%d", a );


hope this helps! :)

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.