String firstname , middlename , lastname ;
firstname = fn.getText().substring(0,1);
middlename = mn.getText().substring(0,1);
lastname = ln.getText();
String shortname = shortname.concat(lastname); // error is in this line, shortname.concat
shortname = shortname.concat(" ");
shortname = shortname.concat(firstname);
shortname = shortname.concat(".");
shortname = shortname.concat("middlename");
shortname = shortname.concat(".");
shrt.setText(shortname);
No other lines have any error. just shortname variable not initialised.
Note: EASY SOLUTIONS PLEASE. I AM IN CLASS 11. DOING THIS JAVA OUT OF TEXT BOOK.
The right-hand expression in
String shortname = shortname.concat(lastname);
will be evaluated before the assignment, so when you try to do
shortname.concat(lastname)
shortname at that moment is not initialized. To fix this you must initialize it with an empty string ("") before you use it:
String shortname = "";
shortname = shortname.concat(...);
Edit:
As #BrianRoach commented, is not necessary to concatenate it, since you are just concatenating a empty string ("") with another String. Just do:
String shortname = lastname;
You are trying to use shortname even before declaring it. You need to declare it and initialize a variable before using it. The use of shortname on the RHS means that you're trying to use it even before it has been declared on the LHS. Declare and initialize it first, and then use it.
String shortname = ""; // blank string, for initializing it
shortname = shortname.concat(lastname);
As #Brian has commented, if its going to be a concat with a blank string, you might as well just directly assign the value to it. And that way you need not have 2 statements.
String shortname = lastname; // Declaration and initialization, done!
Use:
String shortname = "";
shortname = shortname.concat(lastname);
Initilize shortname first
Actulally shortname doesnt contain anything to concat with.
So provide some value for it,
String shortname="";
shortname = shortname.concat(lastname);
Edit:
as #BrianRoach commented in #Christians answer,we should initilize shortname as
String shortname=lastname;
to value the purpose of concatenation.
Related
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
I have this ste.getClassName() which return a String like this pack.age.Foo.
ste is StackTraceElement.
How I could get only Foo? Or the only way is to do a method which extract Foo from that string?
There isn't a built in method for that. You could break up the string like #Naya and #Daniel Perez suggested, or let Class to the heavy lifting for you:
String simpleName = Class.forName(ste.getClassName()).getSimpleName();
String fullClassName = stackTraceElement.getClassName();
String simpleClassName = fullClassName.substring(fullClassName.lastIndexOf('.') + 1);
String[] parts = ste.getClassName.split(".");
parts[2] will be the Foo value.
.split allows you to choose a value for which the string will be divided into an array depending on the position of the divider.
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);
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);
the code is
Field field = st.class.getField("g_"+selectedGroup);
st is my class, and g_+"selectedgroup" is in the st class as String array
how to get that string array?
I need something: String sa[]= field.getStringArray[]; but only getInt, getBoolean there is :(
how to?
Try this,
Field field = ST.class.getField("g_"+selectedGroup);
String[] sa = (String[])field.get(stInstance);
Where stInstance is an instance of ST class.
You just use get.
field.get(instance);
If it's a static field, instance can be null (or really anything).