How can i get the name of the class
String.class.getName() returns java.lang.String
I am only interested in getting last part ie only String
Any Api can do that?
Class.getSimpleName()
The below both ways works fine.
System.out.println("The Class Name is: " + this.getClass().getName());
System.out.println("The simple Class Name is: " + this.getClass().getSimpleName());
Output as below:
The Class Name is: package.Student
The simple Class Name is: Student
or programmaticaly
String s = String.class.getName();
s = s.substring(s.lastIndexOf('.') + 1);
You can use following simple technique for print log with class name.
private String TAG = MainActivity.class.getSimpleName();
Suppose we have to check coming variable value in method then we can use log like bellow :
private void printVariable(){
Log.e(TAG, "printVariable: ");
}
Importance of this line is that, we can check method name along with class name. To write this type of log.
write :- loge and Enter.
will print on console
E/MainActivity: printVariable:
Social.class.getSimpleName()
getSimpleName() :
Returns the simple name of the underlying class as given in the source code. Returns an empty string if the underlying class is anonymous.
The simple name of an array is the simple name of the component type with "[]" appended. In particular the simple name of an array whose component type is anonymous is "[]".
Here is the Groovy way of accessing object properties:
this.class.simpleName # returns the simple name of the current class
Get simple name instead of path.
String onlyClassName = this.getLocalClassName();
call above method in onCreate
Related
I am a beginner at Java. Pardon me if its too stupid a question.
I am fetching a string input from the user in a variable comparisonEntity. if the input is "xyz" then I have to compare all the instances based on this attribute. (the same name attribute exists in class) If the input is "abc" then I have to compare all the instances based on this attribute.
Also I dont want to write redundant code in if and else sections.
So is there something like
objectname."comparisonEntity"
"comparisonEntity" will be replaced by the attribute name of the same value.
If I understand your problem, I think you can achieve this by using reflection.
So you have an instance of your class, and also an input string:
Test instance = new Test();
String input = "nameOfSomeField";
You can obtain a Field that corresponds to the inputted string:
try {
Field field = instance.getClass().getField(input);
} catch (NoSuchFieldException ex) {
// Field doesn't exist
}
And then you can do things like get the value of such field:
Object value = field.get(instance);
Notice that field.get() returns an Object. You need to cast it.
Should be a relatively easy question, but as I am a newbie to java, I dont know the answer!
I have the following code:
String FTSE = "http://www.bloomberg.com/quote/UKX:IND/members";
doc = Jsoup.connect(FTSE).get();
Elements trs = doc.select("tr:has(a[href='/quote/III:LN'])");
Elements values = trs.select("td.value");
link = values.get(0);
System.out.println("text : " + link.text());
However, there are red squiggly lines in eclipse under the word 'link' in the penultimate and final line, and when I hover over it, it says this- link cannot be resolved to a variable.
How do I fix this?
Cheers
You are trying to assign values.get(0) to a variable link, which, as Eclipse thinks, was not defined or defined elsewhere. There are two problems and two possible solutions:
You never defined link. Define it and assign it the type of what values.get returns:
SomeType link = values.get(0);
You defined link in another method. Since it's not in the same scope, you must define it for the global scope and then use it with this keyword:
this.link = values.get(0);
It seems that link is not defined. Try:
YourClass link = (YourClass)values.get(0);
Instead of YourClass use the class that values.get returns.
How can I simply get the email address after use MimeUtility.decodeText() from javax.mail.internet ?
After that operation I have following String:
Foo Bar <foo.bar#abc.en>
I can do that by methods from String class but I'm interested in existed predefinied method for this ?
It's very easy to define your method. To get the String you only need one line of code:
String email = stringToDecode.split("<")[1].substring(0, stringToDecode.split("<")[1].length() - 1);
Just call new InternetAddress(addr).getAddress(). In fact, you don't even need to call MimeUtility.decodeText() first.
I am working with a java generated dynamic webpage, and I'm printing links from a query to a JDO. But I don't understand how can I get and use the parameter from the url.
My html objects are printed like this
print = print + "Nome:<a href='displayFotos?album="+results.get(i).nome+ "'>"+
results.get(i).nome+ "</a></br>";
The results in having for example:
Nome:<a href='displayFotos?album=album1'>album1</a>
So, in my head when clicked it should be calling the address of the dynamic webpage album like this and should get the parameter. In this case it would be the album1.
else if (address.indexOf("/dinamicas/album") != -1) {
String album = param1;
System.out.println("did it work? "+album);
}
And I have in the beginning of the class a general parameter that I use to get text from html forms.
String param1 = req.getParameter("param1");
I understand this might be an easy question but I'm not getting there by myself.
Nome:<a href='displayFotos?album=album1'>album1</a>
Here, you're using a parameter name of album.
However, you're attempting to get it by a parameter name of param1. This does obviously not match.
String param1 = req.getParameter("param1");
You need to use the same parameter name as is been definied in the request.
String album = req.getParameter("album");
// ...
What I want to do is to get a specific text from strings.xml dynamically. I think it will involve to access an object variable dynamically.
There will be a function like:
public void getDynamicString(int level) {
text.setText(R.string.levelText_+level);
}
And in strings.xml there will be <string name="levelText_5">Text level 5</string>
I would rather not create a list with all the text resources. Can one do this in Java/Android.
Use the method getIdentifier(name, defType, defPackage) of the Resources class to get the id of a resource by name. Then you can do a normal getString(id) from the same class.
EDIT: a bit of Googling revealed this: this. You can find sample usage there.
Try: getResources().getString(R.id.stringId).
You should look at using getIdentifier(String, String, String) of the Resources class.
All you have to do is call
this.getString(R.string.levelText_5)
If your in an area of the program in which you have access to a Context or Application, such as a ListAdapter call:
context.getString(R.string.levelText_5)
or
application.getString(R.string.levelText_5)
if you have no access to the context or application then call:
getResources().getString(R.String.levelText_5);
To do it dynamically call:
String name = "levelText_"+level;
int id = getIdentifier(name, "string", "com.test.mypackage");
getResources().getString(id);
I had the same problem and I fixed it using this
okey , whenever you want to access a string from strings.xml dynamically and what i mean by that is to avoid using getResources().getString(R.id.stringId) ,you create a string in which you can manipulate dynamically however you want in our case uriq ("stupid variable name") and then you create resource object which is in my example level_res and initialize it then you use this method called getIdentifier() which accepts your dynamic string as a parameter ,now u simply pass your ressource to the method getstring(mysttring)
String uriq="level"+level_num;
level_res=getResources();
int mystring=getResources().getIdentifier(uriq,"string",getPackageName());
String level=level_res.getString(mystring);