How can I use name of string instead of content of string? - java

Normally, I use
String[] arrayName = getResources().getStringArray(R.array.name_array);
So are there any ways to use
String[] arrayName = getResources().getStringArray(R.array.NAME);
with NAME is string (String NAME = "name_array";)?

Android replace string name to integer id after compile, but you place id to varialbe
int NAME = R.array.name_array;
String[] arrayName = getResources().getStringArray(NAME);

Going by what is in the official documentation, this is an acceptable way of referencing your array. However, I need to point out an error in your flow:
NAME must be an int. Therefore, the value should be R.id.name_array (this would return an int). Also, remove the double quotes around name_array.
You can do the referencing this way:
int NAME = R.array.name_array; // use a variable to store the int reference
String[] arrayName = getResources().getStringArray(NAME); //fetch

Related

String.replace() isn't working like I expect

I am not understanding how to use the String.replace() method. Here is the code:
CharSequence oldNumber = "0";
CharSequence newNumber = "1";
String example = "folderName_0";
System.out.println("example = " + example);
example.replace(oldNumber, newNumber);
System.out.println("example.replace(oldNumber, newNumber);");
System.out.println("example = " + example);
And it's outputting:
example = folderName_0
example.replace(oldNumber, newNumber);
example = folderName_0 // <=== How do I make this folderName_1???
The replace method isn't changing the contents of your string; Strings are immutable. It's returning a new string that contains the changed contents, but you've ignored the returned value. Change
example.replace(oldNumber, newNumber);
with
example = example.replace(oldNumber, newNumber);
Strings are immutable. You need to re-assign the returned value of replace to the variable:
example = example.replace(oldNumber, newNumber);
String is a immutable object, when you are trying to change your string with the help of this code - example.replace(oldNumber,newNumber); it changed your string but it will be a new string and you are not holding that new string into any variable. Either you can hold this new string into a new variable, if you want to use your old string value later in your code like -
String changedValue = example.replace(oldNumber,newNumber);
or you can store in the existing string if you are not going to use your old string value later like -
example = example.replace(oldNumber,newNumber);

replace String with value of properties file in java

I have a code to replace stream of string. I need to search a specific string that is defined in the key of properties file
String result="";
int i=0;
while (i<listToken.size()){
result = listToken.get(i);
while (enuKey.hasMoreElements()) {
String key = (String)enuKey.nextElement();
// String value = propertiesSlang.getProperty(key);
if (listToken.get(i).equals(key)){
String value = propertiesSlang.getProperty(key);
listToken.get(i).replace(listToken.get(i), value);
System.out.print("detected");
}
}
i++;
}
But it doesn't replace word. How I can replace words using properties.
It's because you forgot to assign the result, using the method set():
listToken.set(i, propertiesSlang.getProperty(key)));
assuming listToken implements AbstractList
Why complicate things with replace(). As far as I understand your code you can simply do -
String value = propertiesSlang.getProperty(key);
listToken.set(i, value);
I see you have modified your code again to
listToken.get(i).replace(listToken.get(i), value);
Just so that you know String class is immutable. So operations like replace() or substring() will give you a new String and not modify the original one. Get the new String and set it in your list listToken.

How do I convert a string to an new object's name?

I'd like to write a program which creates a set of objects in a loop....
(i.e.)
String newFirm = "empty";
for(int i=0; i<30; i++)
{
newFirm = "firm" + i;
firm newFirm = new firm();
}
and then of course I would need something like
stringToObject = "firm" + x;
stringToObject.type = "service";
stringToObject.size = 10000;
Obviously this code is fictional, but It expresses how I'd ideally create and call for objects. The nature of this program is such that the final number of firms (or other objects) are not known at the time of compiling.
Is there a method by which I can convert a given string into the name of an object (either to call or to create) as well as creating objects "on the fly"?
Sounds like a job for an ArrayList.
ArrayList<Firm> myList = new ArrayList<Firm>();
And in your loop,
Firm firm = new Firm();
firm.type = "service";
myList.add(firm);
And to get it,
Firm f = myList.get(index);
convert a given string into the name of an object
Your need is to refer an object with the string in your hand. I'll suggest Hashmap<String,Object>
Eg:- you have a String,
String name="object_name";
And your class is Firm. Now,
Hashmap<String,Firm> objs=new Hashmap<String,Firm>();// note:your for loop comes after this line
Firm new_firm=new Firm();
new_firm.type = "service";
new_firm.size = 10000;
objs.put(name,new_firm);
Now you can refer your object with the string in your hand as
objs.get("object_name");

Change an array to a string without creating a string

I wish to accomplish:
String []beef = new String[3];
beef[0] = "Water";
beef[1] = "Chicken";
beef[2] = "Paper";
String empo = Arrays.toString(beef);
if (empo.isEmpty()){
empo = "Nothing";
System.out.println(empo);
}else{
System.out.println(empo);
}
without having to create the string.
Something like:
String []beef = new String[3];
beef[0] = "Water";
beef[1] = "Chicken";
beef[2] = "Paper";
Arrays.toString(beef); //change beef to just a plain string
if(beef.isEmpty()||beef==""){
no = "Nothing";
System.out.println(beef);
How would one go about doing this?
You can't.
Java is a strongly and statically typed language. That means you have to tell it what type a thing will be when you declare it (strong typing), and you can't ever change it's type after that (static typing).
You will just have to create a new String.
You can create substrings with the same backing memory as the original string, but you can't create a string with the same backing memory as an array of strings. They're not stored in the same order so it's impossible to view the same memory both ways.

Java use String value as Variable

I have a method with a Parameter Country. This Parameter only Contains an abbreviation of the Country. In the method i want to print the full name of the Country without switch case or something, but with predefined Strings
final String VA="Vatikan";
String country="VA";
system.out.println(country);
//Is it possible that it Prints Vatikan now?
//I know not with that code but is there a possibillity to do that.
No, but you could use a map to acheve the result you want. Specifically, to return the full name of the abbreviated country name:
String va="Vatikan";
String country="VA";
Map<String, String> abbreviationMap = new HashMap<String, String>();
abbreviationMap.put(country, va);
System.out.println(abbreviationMap.get(country)); //prints "Vatikan"
This will assign it properly:
final String VA="Vatikan";
String country=VA;
System.out.println(country);
The String variable country will be pointed to whatever the variable VA is pointed to; because VA cannot change (it's final), country will point to "Vatikan".
What you are doing is that assigning a string "VA" to country, but you want to treat it as a variable, so remove the quotes("").
final String VA="Vatikan";
String country=VA;
System.out.println(country);

Categories

Resources