0

How would I solve this problem?

I am storing a name and surname in Strings as well as some other information for a user.

The user can give input in form of a string and a output must be delivered in form of a string.

I want to take this for e.g. :

"My name is $name$ and my last name is $surname$, I was born on $date$ and I am now $age$ years old"

The string above is provided by the user (User can place the variables in the order they want them).

I want to convert the variables ($someVar$) that the user provided and turn them into variables that are stored in my program. The variables orders can change, and some may not be used by the user.

The user literally enters $placeholder$ (The user would type out $name$ if they want the name to be returned to them in form of a string)

This is how I decided to solve the problem:

String input = "My name is $name$ and my last name is $surname$, I was born on $date$ and I am now $age$ years old";

input.replace("$name$", "Chris").replace("$surname$", "Smith").replace("$date$", "1962").replace("$age$", "42");

I don't think this is the best method but it should work Can it be improved?

Thanks!

ILoveAMac
  • 14
  • 2
  • Use a `java.util.Scanner` object, initialize it with `System.in`, and then use it to get user input, assigning the information obtained into variables. This is all well explained in easy to find tutorials or in most any intro to Java textbook. Good luck! – Hovercraft Full Of Eels Jun 17 '17 at 19:17
  • 1
    If I understand correctly, the user will enter the exact string that you have shown in your question and you want to replace anything surrounded with `$`s with an appropriate value. Is that correct? You should learn a little about so-called "string parsing". In this case, `String.replace()` might be what you need. Also, where do you get these values from? Are they also entered by the user? – Code-Apprentice Jun 17 '17 at 19:25
  • 1
    Which part of this problem are you having difficulty with? Do you need to know how to allow the user to enter data? Or do you need help with replacing the placeholders with a valid value? – Code-Apprentice Jun 17 '17 at 19:27
  • No, that is only an e.g. the order of the sentence could change or some variables may not be used. I am aware of the String.replace() method however it will be of no use as the order of the sentence is changed and when some variables are left out – ILoveAMac Jun 17 '17 at 19:29
  • Does the user literally enter `$surname$` or actually real values like "Louis", "42" and "1984-01-01"? If the user really enters the sentence with those placeholders, should your program replace them with real values? Please clarify that and edit your question. (And as always on this site you should show your attempt, no matter how bad it is.) For the former case please provide three, four other sentences with different grammar. – try-catch-finally Jun 17 '17 at 20:16
  • 1
    `String.replace` replaces the substring no matter where in the string it appears, thus the order of the sentence doesn't matter. – Bernhard Barker Jun 17 '17 at 20:18
  • You have various string templating solution in the duplicate. –  Jun 17 '17 at 21:33

0 Answers0