• Post Reply Bookmark Topic Watch Topic
  • New Topic

java String concatenations  RSS feed

 
adebari olalekan
Ranch Hand
Posts: 61
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i am to concatenate a string literal received from stdin to another string i created , but when i use the "+ "operator, the value of the string variable was not outputed , or got cut off when it gets to a blank space character.

this is my code.

to test it the inputed string should be more than one word sepearated by white space character to see the cut off. thanks.
 
Liutauras Vilda
Sheriff
Posts: 4928
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For me this code works in the way you expecting it to work.

Maybe you could copy/paste output here so we could better understand what you mean?
 
Liutauras Vilda
Sheriff
Posts: 4928
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe you are typing in full name?

For that you need to read in not just a next token, but full line. Read the API by looking Scanner class and existing methods and see if you can find for yourself first.
 
adebari olalekan
Ranch Hand
Posts: 61
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:For me this code works in the way you expecting it to work.

Maybe you could copy/paste output here so we could better understand what you mean?


I have read the API and try to find what the matter is, but nothing.  The program does not return any errors ; what happens is that the string that is inputed got cut off, assume you give the input of the stdin to be ;" Olalekan Adebari ", what I receive as the output is only the first name but the second name has been cut off.
 
Liutauras Vilda
Sheriff
Posts: 4928
334
BSD
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know...

Have you read why it is like that and what you need to do carefully? I'm very surprised you didn't find what you need after I mentioned to you what you need to do.

Please try harder - you need to learn how to navigate in Java API site. Took me around 3 seconds to find.
 
adebari olalekan
Ranch Hand
Posts: 61
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The next available method to use is nextLine() but it still return the same thing , either it failed to out put the value of the variable or it cut off some portion if it.

Seriously i haven't seen this type of stuff before , as I have used scanner to read numerous strings from Stdin, I would have used buffered reader which is more efficient when reading strings but , I wanted to use scanner
 
Liutauras Vilda
Sheriff
Posts: 4928
334
BSD
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
adebari olalekan wrote:The next available method to use is nextLine() but it still return the same thing , either it failed to out put the value of the variable or it cut off some portion if it.

Probably you won't be able to solve that part yourself, Scanner has its own tricks when you call nextSomething then nextLine (the one you need).

In the meantime, just replace line 16th with next 2 lines (and look for information why that happens on this forum, you could find quite a lot info about that) :
 
Liutauras Vilda
Sheriff
Posts: 4928
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And well done finding yourself a correct method to read in next line 
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!