Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

.concat() with ints  RSS feed

 
Kyle Harris
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
Im trying to get a handle on concatenation. .concat()
Ive created two versions, one to concatenate string info and another int.
But when i try and get it working i get "int cannot be dereferenced"
But i have used int data, so im confused

much appreciate to those who can explain where im going wrong +/- how to get this working.


 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66204
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
concat is a method of the String class. It cannot be used with any other type.

ints aren't even objects -- they are primitives, and as such, have no methods at all.

I'm curious: what would you expect the concatenation of two number to be?
 
Kyle Harris
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Thanks for adding tags, im new at this

Bear Bibeault wrote:concat is a method of the String class. It cannot be used with any other type.

ints aren't even objects -- they are primitives, and as such, have no methods at all.

I'm curious: what would you expect the concatenation of two number to be?


unsure about how concat works i tried both int and strings. Which i think is a sensible thing to do as I can now discount int. Learn by doing.

i would expect the concatenation of two numbers to be them next to each other, as opposed to added together.

So to concatenate two numbers id need to turn them into strings.



But why doesn't my string concatenation work?






 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66204
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kyle Harris wrote:i would expect the concatenation of two numbers to be them next to each other, as opposed to added together.

That's not really any sensible mathematical operation on numbers is it? That only makes sense for strings.

So to concatenate two numbers id need to turn them into strings.

Right, but you will end up with a string. If you need a number back, you'll need to convert again.

But why doesn't my string concatenation work?

Don't know. What do you mean by "doesn't work"?
 
Campbell Ritchie
Marshal
Posts: 55717
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Have you come across the String#valueOf() methods?
 
Kyle Harris
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:
Kyle Harris wrote:i would expect the concatenation of two numbers to be them next to each other, as opposed to added together.

That's not really any sensible mathematical operation on numbers is it? That only makes sense for strings.

Its sensible if you are unaware whether it works for strings and or integer, process of elimination.

So to concatenate two numbers id need to turn them into strings.

Right, but you will end up with a string. If you need a number back, you'll need to convert again.

This is useful, thanks.

But why doesn't my string concatenation work?

Don't know. What do you mean by "doesn't work"?


If you don't know then why comment? It's not helpful.
You know what does not work means, your just being sarcastic.
You are supposed to be a friendly place for people learning to program.

Campbell Ritchie
Have you come across the String#valueOf() methods?


No, but ive looked it up now and it might be useful.
thanks Campbell Ritchie
 
Knute Snortum
Sheriff
Posts: 4073
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you don't know then why comment? It's not helpful.
You know what does not work means, your just being sarcastic.
You are supposed to be a friendly place for people learning to program.

I'm pretty sure that Bear is not being sarcastic. He (and I) would like to know the details of the problem (TellTheDetails <- that's a link). Obviously, concatenate "works" in the sense that it's used by Java programmers all the time. How are you using it? How is it failing?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"It doesn't work" could mean:
It doesn't compile.
It doesn't run.
It runs, but gives me output I wasn't expecting.
It starts then throws an exception
or probably a bunch of other things.

If you are asking someone for help, you should strive to make it as easy as possible for them to help you. If you give them as much info as possible, they have to spend less time figuring out what you mean, and can focus in on the actual issues.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now that I have a minute to look, I see at least two problems that most likely won't let this compile. These two methods:


and

Both methods declare that they return somethings - the first an int, the second a String - but neither actually has a return statement.
 
Campbell Ritchie
Marshal
Posts: 55717
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can see another error in the first method; the concat() method returns a String and you cannot convert that to an int.

I think we might have to go back to square 1. Please tell us again what you mean by concatenating two integers? Is it like the old joke about
Four and four make forty‑four.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!