🔒 Locked
java beginner
Advertisement
Original Post
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
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.
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.
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 :)
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.
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.
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".)
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".)
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.
Advertisement