Create variable execution time Java [closed] - java

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Is possible to do in Java something like this? :
System.out.... -> ask for a string
Store this string
Create a variable with the name of this string
Thank you very much!

No. You cannot create a variable, at execution time, with a particular name.
However, you can use a Map, which probably does what you want. The get method will look up an entry, and the put method will set an entry. Example:
Map<String, Integer> myMapOfThings = new HashMap<>();
String nameOfThing = /* ... get a string from the user somehow here ... */;
myMapOfThings.put(nameOfThing, 2);
System.out.println(myMapOfThings.get(nameOfThing)); // prints 2

Related

Use the value that a function returns as the name of a new Array [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Good I am trying to create a function that generates new Array, and trying to use a getter method that returns a name for example ArrayD5, ArrayD10 and use these as the name of the new arrays to generate.
I have tried to do this:
int length = 5;
String (SeatIbiza.nombres(length))[] = new String[length];
String nombres (int length) {
return "ArrayD"+length;
}
If you are looking for a way to declare something like e.g.
String "ArrayD" + 2
so that you have a variable
String ArrayD2
then this is not possible in Java. Dynamic naming of variables is not supported.
I don't think if that's possible. The variable names are required to be declared during compile time in JAVA and dynamic naming isn't supported... yet.
Correct me if I'm wrong as I'm still learning about JAVA

Need to add a default value in a string inside a method in java [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
Suppose we have a string BE231231.
I need an output like this : BE0231231
need to append zero at a this fixed location in java inside a method
public String appendAtIndex(String base, String toAppend, int index){
return base.substring(0,index) + toAppend + base.substring(index);
}
You can implement the logic for restrictions on index accordingly so that base.substring does not give errors.

Is there any way to auto generate objects from a class? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I have a class with 5 variables, and 3 out of the 5 will be the same for every new object. Is there a way to create, say, 100 objects, from a string file containing 100 items?
Yeah of course can you do that. I wont write code for you, but the following steps should help to come to a solution yourself.
Make a constructor delegation, one which accepts 5 values. And another which accepts just the 2 which are not changing. This constructor then overloads the other with the default values. this(3,2,"a", input1, input2)
Initialize the 3 variables which are not changing directly in the field declaration: private int initialSetVariable1 = 3;

java string add comma become array [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
String items = "bookcupdoll";
I want to add a comma in items variable like below:
book,cup,doll
How should I make the string become an array?
If you are asking how to convert a string to an array...
There is a handy method called "split(String separator)"
If the string looks like : String items = "book,cup,doll";
you can use it like this
String[] array = items.split(",");
And you will end up with an array of size 3 with the 3 different elements.

Java- About strings [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I am doing some validation using simple Java, I am coding as below
if (something.equals("ABC"))
expectedvalue = "TEST1";
else if(something.equals("DEF"))
expectedvalue = "TEST5";
// and so on....
My issue here is that, if something.equals("ABC"), expectedvalue can be any of these two values -> "TEST1" or "TEST2"
During validation, in the output, I do not get error if I give "TEST1" for expectedvalue. It should do the same even if I give "TEST2". How to do that?
You have to have a Set of allowed values.
Set<String> allowed = new HashSet<String>();
allowed.put("TEST1");
allowed.put("TEST2");
//and then you can use this set
if (allowed.contains(yourValueToCheck)) {
// do what you need
Also note that it is safer to compare strings like this:
if ("ABC".equals(something)) // to protect yourself from NullPointerExceptions
Perhaps you could change your test to test with an OR statment?
if(expectedvalue.equals("TEST1") || expectedvalue.equals("TEST2"))
.....

Categories

Resources