• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

.concat() with ints

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.


 
Sheriff
Posts: 67682
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67682
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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"?
 
Marshal
Posts: 76854
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 7123
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
lowercase baba
Posts: 13081
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 13081
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 76854
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
When you have exhausted all possibilities, remember this: you haven't - Edison. Tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic