Win a copy of Zero to AI - A non-technical, hype-free guide to prospering in the AI era this week in the Artificial Intelligence and Machine Learning forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

How to concatenate string variable consist of words separated by spaces

 
Ranch Hand
Posts: 235
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Java Experts,

I need to concatenate a string variable to form a full path of a new file to be referenced by later reads but failed to form the complete path due to multiple words with space in the string variable. Below is the code snippet which the full path has failed to materialize:



As a result, could someone advice me on how to concatenate multiple strings where the variable string such as country is made up of more than one word separated by space. It is necessary to use double quotes in general to surround them such as the following example so the operating system knows how to put the full path together:

“C:\United States of America\sports.txt”

I am running JDK 7 on Windows XP & 7 platform.

Your assistance would be much appreciated.

Thanks,

Jack
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jack Bush wrote:


Is that your actual code ? The quotes look like the ones you get when you edit using a word processor rather than a text editor. Java doesn't like those quotes.
Replace them with normal quotes like this

and see if that fixes your problem.

Also your variable on line 1 is called county, but on lines 3 and 8 you are using a variable called country.
It's always best if you post the actual code you are using. As this code won't even compile, you couldn't have got the errors from it that you say you did.
 
Jack Bush
Ranch Hand
Posts: 235
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Joanne,

Thanks for pointing this out and the corrected code snippet below is working properly now:


Thanks,

Jack
 
What? What, what, what? What what tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic