FAO java peoples

Chronictank

FH is my second home
Joined
Jan 21, 2004
Messages
10,133
Hi,
Does anyone know how you add 2 literal strings which have numbers

i.e.
variable1 = 1
variable2 = 2

I want to do this:
Variable1 = Variable1+Variable2

and have it output 12 rather than 3

Thanks in advance
 

Ovron

One of Freddy's beloved
Joined
Nov 4, 2004
Messages
471
Not a Java person ;p (C ftw!), but you can use: String.toString().

The String object's function toString() will convert int's, double's, float's and actually any other data type to be presented as a string.

Thus this should work:

String strTest = String.toString(variable1) + String.toString(variable2);

I don't have the JDK installed at the moment, but that should work. Remember your data types; can't assign a string to an integer, which seems what you wanted... I take it variable1 and variable2 are numerical data types.

/Ovron
 

Congax

Fledgling Freddie
Joined
Aug 18, 2004
Messages
3,231
Can't you just enclose the string in double quotes?

fe:

String number = "1";
String number1 = "2";

(number + number1 );


Anywho, I don't really know. this just seems logic to me :p
 

Chronictank

FH is my second home
Joined
Jan 21, 2004
Messages
10,133
Not a Java person ;p (C ftw!), but you can use: String.toString().

The String object's function toString() will convert int's, double's, float's and actually any other data type to be presented as a string.

Thus this should work:

String strTest = String.toString(variable1) + String.toString(variable2);

I don't have the JDK installed at the moment, but that should work. Remember your data types; can't assign a string to an integer, which seems what you wanted... I take it variable1 and variable2 are numerical data types.

/Ovron

yep and i dont know how to change the cast in Java
your code didnt work :<
gave the error
Object Expected

String expiryDate = String.toString(expiryDate) + String.toString(document.cardForm.dayList.selectedIndex);
alert(expiryDate)
 

Ovron

One of Freddy's beloved
Joined
Nov 4, 2004
Messages
471
yep and i dont know how to change the cast in Java
your code didnt work :<
gave the error
Object Expected

String expiryDate = String.toString(expiryDate) + String.toString(document.cardForm.dayList.selectedIndex);
alert(expiryDate)

hmmm just tried it and I can't get it to work either. But, I came up with another way to do it, a bit easier.

int int1 = 13;
int int2 = 70;

String str1 = "" + int1 + int2; //produces "1370"
System.out.println(str1);

I am not sure if that does an actuall type cast, or if it simply ignores the rules of integer handling, and just conc's the two integers with the string.

That produces "1370". I.e. simply add a leading "" before +'ing the numeral data types.

Hope this works for you,

/Ovron
 

Chronictank

FH is my second home
Joined
Jan 21, 2004
Messages
10,133
expiryDay = expiryDay.toString()
expiryMonth = expiryMonth.toString()
expiryDate = expiryDay+expiryMonth


got it :)

Thanks for the help tho ^^ got thinking about your first answer and had a play
rep a piece for the help :cheers:
 

Ovron

One of Freddy's beloved
Joined
Nov 4, 2004
Messages
471
expiryDay = expiryDay.toString()
expiryMonth = expiryMonth.toString()
expiryDate = expiryDay+expiryMonth


got it :)

Thanks for the help tho ^^ got thinking about your first answer and had a play
rep a piece for the help :cheers:

The second way I just posted is a bit easier, and works aswell - but that is the correct way of doing it. Sorry for not getting it right, but atleast it gave you an idea, and you managed ;>. Been over two years since I last touched java. Forgot that the actual datatypes got the conversion functions in em, instead of having to use the "master" object, String.

Anyway, nice that it works. ;>
 

kirennia

Part of the furniture
Joined
Dec 26, 2003
Messages
3,857
Hmmm, I'm getting slowly more and more suprised at how close java and c sharp really are. I've only done c sharp personally and only for a little while too but this looks mighty similar. C sharp would be.

int var1 = 1;
int var2 = 2;
string output;
Convert.ToString(var1, var2);
output = "" + var1 + var2;
Console.WriteLine("{0}", output);
Console.ReadLine();

Probably a quicker way to type it up but anyhoo :D
 

Steffan-

Fledgling Freddie
Joined
Jan 22, 2004
Messages
644
Hmmm, I'm getting slowly more and more suprised at how close java and c sharp really are. I've only done c sharp personally and only for a little while too but this looks mighty similar. C sharp would be.

int var1 = 1;
int var2 = 2;
string output;
Convert.ToString(var1, var2);
output = "" + var1 + var2;
Console.WriteLine("{0}", output);
Console.ReadLine();

Probably a quicker way to type it up but anyhoo :D

C# and java are very similiar in ways, however the {0}, paramter doesn't work in java sadly.

The way I would do this in java would be:

int var1 = x;
int var2 = y;
int result = x+y;
System.out.Println("The result for "+x+" + "+y+" is: "+result);
 

Ovron

One of Freddy's beloved
Joined
Nov 4, 2004
Messages
471
C# and java are very similiar in ways, however the {0}, paramter doesn't work in java sadly.

The way I would do this in java would be:

int var1 = x;
int var2 = y;
int result = x+y;
System.out.Println("The result for "+x+" + "+y+" is: "+result);

He didn't want to sum em, he wanted to conc em together. I.e. var1 = 5, var2 = 10, var1 + var2 should give 510, not 15.
 

kirennia

Part of the furniture
Joined
Dec 26, 2003
Messages
3,857
C# and java are very similiar in ways, however the {0}, paramter doesn't work in java sadly.

The way I would do this in java would be:

int var1 = x;
int var2 = y;
int result = x+y;
System.out.Println("The result for "+x+" + "+y+" is: "+result);

Some useful bits to know when I get around to it next year :) I'm guessing the main principles are the same it's just mainly the code and data types etc to learn? e.g.

Console.WriteLine..... = System.out.Println.....
 

Jeremiah

Fledgling Freddie
Joined
Aug 10, 2004
Messages
1,131
expiryDay = expiryDay.toString()
expiryMonth = expiryMonth.toString()
expiryDate = expiryDay+expiryMonth


got it :)

Thanks for the help tho ^^ got thinking about your first answer and had a play
rep a piece for the help :cheers:

That would give you a complie error since you would have already declared expiryday and expiryMonth to be an int (I would assume :)) and they cant be both an int and a String :)
 

raid

Can't get enough of FH
Joined
Dec 22, 2003
Messages
1,368
Some useful bits to know when I get around to it next year :) I'm guessing the main principles are the same it's just mainly the code and data types etc to learn? e.g.

Console.WriteLine..... = System.out.Println.....

Yep, most of the commonly used languages have very similiar feature set. The real challenge in programming, especially OO programming, is to model and solve the real world problem. Language syntax can be learned fast, and once you know one language well the others are fairly easy to learn because you know what to look for.
 

Steffan-

Fledgling Freddie
Joined
Jan 22, 2004
Messages
644
He didn't want to sum em, he wanted to conc em together. I.e. var1 = 5, var2 = 10, var1 + var2 should give 510, not 15.

Oh, scrap the:

int result = x+y;

then, and instead just do:

System.Out.Println("bla bla"+x+""+y);

PS. Kirennia, you're right the main principles are nearly the same, and yes, System.Out.Println = Console.Write :)
 

Ovron

One of Freddy's beloved
Joined
Nov 4, 2004
Messages
471
Oh, scrap the:

int result = x+y;

then, and instead just do:

System.Out.Println("bla bla"+x+""+y);

PS. Kirennia, you're right the main principles are nearly the same, and yes, System.Out.Println = Console.Write :)

That would just feed the data to standard out - he doesn't want that. He wants to save it in a variable ;).
 

Ovron

One of Freddy's beloved
Joined
Nov 4, 2004
Messages
471
Yep, most of the commonly used languages have very similiar feature set. The real challenge in programming, especially OO programming, is to model and solve the real world problem. Language syntax can be learned fast, and once you know one language well the others are fairly easy to learn because you know what to look for.

Indeed, programming is not knowing a language. However, platforms such as dotNET "require" a bit more "language specific" things than for instance C. That is, if you wish to use the built-in functions for hell-alot. Same goes for java which is even more strict.

C all the way \o/.

ps: down with OO! boooo! ;d
 

Users who are viewing this thread

Top Bottom