-3

I am using Java 17, Maven, and i am still learning, my question is why does

String database= ""database"";

output as

'"database"'

i want it to output as "database" but when i do

String database = ""database"" it wont work.

it just outputs it with extra " or ' added. i need the output to just be 'database' or "database" with quotes included but i cannot get it to work.

I have tried to escape the quotes, using methods i have found online, nothing is working. I am doing this for a minecraft plugin config that keeps outputting wrongly, here is an example.

database:
  host: host
  port: 3306
  database: '"database"'
  username: username
  password: password

i need it to be

database:
  host: host
  port: 3306
  database: "database" or 'database'
  username: username
  password: password
khmarbaise
  • 92,914
  • 28
  • 189
  • 235
Mxzzy
  • 1
  • 5
    `String database= ""database"";` will not compile. – f1sh Dec 27 '22 at 17:16
  • 2
    you have to escape the quotes https://stackoverflow.com/a/3844602/8843585 – Ramon Dias Dec 27 '22 at 17:17
  • 1
    This looks rather about YAML than Java. `"'database'"` or `'"database"'` should work. Why do you need the quotes though? – knittl Dec 27 '22 at 17:17
  • Escape with back-slash `String database= "\"database\"";` – K-Galalem Dec 27 '22 at 17:18
  • @K-Galalem then i am getting the output database: '"database"', keep in mind it is outputting to a config.yml file. – Mxzzy Dec 27 '22 at 17:31
  • @knittl just for personal preference, i would like them there. Although when i escape quotes i get the output database: '"database"', keep in mind it is outputting to a YAML file but from one of my java classes as it is saving the default for my config. – Mxzzy Dec 27 '22 at 17:32
  • @Mxzzy I'm not sure I understand: "for personal preference" – does the value require quotes or not? If quotes can't be there, you can't put them there – they will break stuff. If they must be there, then it's not personal preference. How does your YAML and your Java program come together? How are you using the value from the YAML file? – knittl Dec 27 '22 at 17:44
  • @knittl i want the '' there to connect the value if there is any spaces, it works because i have manually edited the config and used them, the java program is a minecraft plugin that connects to mysql, the database connection string is in this config and i want to have quotes surrounding the last 3 values as a user has requested that. – Mxzzy Dec 27 '22 at 17:55
  • `'"database"'` will not work, but use ` as delimiter (as used in phpmyadmin for example) can work too – Elikill58 Dec 27 '22 at 18:04

1 Answers1

1

Is it Java code though? if you want to print quotes in Java, use escape character:

String javaCodePrintQuotes = "\"database\"";
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
Aitsuken
  • 126
  • 10