Convert a string in code in Java [duplicate] - java

This question already has answers here:
Run piece of code contained in a String
(9 answers)
Closed 5 years ago.
I'm trying to convert a string in code in Java, but i have no idea how to do it or if it is possible.
This is my Java code (Measure is an other class I have created)
String str= "Measure m = new Measure(10,1);";
Is it possible to run the code in the string?

No I dont think that is a good practice, you don't need to do it that way,
just instantiate outside of the string, it will be fine and good practice.
Measure m = new Measure(10,1);

Related

How do I make this char replacement work? [duplicate]

This question already has answers here:
Java String replace not working [duplicate]
(6 answers)
Closed 9 years ago.
I am a little bit confused at the moment. I tried that:
String test = "KP 175.105";
test.replace("KP", "");
System.out.println(test);
and got:
KP 175.105
However, I want:
175.105
What's wrong with my code?
You did not assign it to test. Strings are immutable.
test = test.replace("KP", "");
You need to assign it back to test.
Strings are immutable so you need to assign your test reference to the result of String.replace:
test = test.replace("KP", "");
String is immutable in java so you have to do
test =test.replace("KP", "");

alternative for strings combing strings that isnt "+" [duplicate]

This question already has answers here:
String variable interpolation Java [duplicate]
(5 answers)
Closed 1 year ago.
Is there any way to join a string and another variable together, that is also a string, without using the "+" operator. Specifically one similar to this one that is done in C#.
string str = $"Hello {userName}. Today is {date}.";
If there is any way to achieve a similar or same outcome in Java please let me know.
See https://docs.oracle.com/javase/7/docs/api/java/lang/String.html
String.format("Hello %s. Today is %s.", userName, date);

Why should java variable names start with lowercase characters? [duplicate]

This question already has answers here:
camel case method names
(12 answers)
Closed 7 years ago.
I do not understand what the difference between
int Hello ;
and
int hello ;
is .
Does it make a big difference if i use upper case Characters ?
That's because of Java Convention!
Actually, you can write a program like the way you're imagining but, you won't be following any pattern.......If you become a real programmer someday, you'll understand that patterns exist to make things better and easier.....

Why does String.replace not work? [duplicate]

This question already has answers here:
Java String replace not working [duplicate]
(6 answers)
Closed 9 years ago.
I am a little bit confused at the moment. I tried that:
String test = "KP 175.105";
test.replace("KP", "");
System.out.println(test);
and got:
KP 175.105
However, I want:
175.105
What's wrong with my code?
You did not assign it to test. Strings are immutable.
test = test.replace("KP", "");
You need to assign it back to test.
Strings are immutable so you need to assign your test reference to the result of String.replace:
test = test.replace("KP", "");
String is immutable in java so you have to do
test =test.replace("KP", "");

How to convert string to math equation? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Java: Parse a mathematical expression given as a string and return a number
Hello there,
I would like to ask you, if exist some way how to convert string "1+2+3" to math equation? Exist for this purpose some function or method in Java?
Thanks for hints.
It's not part of the standard API.
But I implemented it in my project, you can have a look here.
https://github.com/MarkyVasconcelos/Towel/wiki/Expression
This would necessarily depend on the implementation of the Equation class you're using. Check its API.

Categories