hieukhtn
asked on
How can I get all the string in a java file??
How can I get all the string in a java file.
Example:
we have a java class file like this:
-test.java -------------------------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------
public class test{
public static void correctString(String sourceFile, String desFile){
String str1 = "Dang Nhon Hoa";
String str2 = "Nguyen Quang Minh";
String str3 = "Test1" + "test2" + "test3";
System.out.print(str3 + str1 + str2 + "test");
}
}
-------------------------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ------
We have to write a method that read the java file above then return the expected String array content values like this:
returnArrayValues = {"Dang Nhon Hoa", "Nguyen Quang Minh", "Test1", "test2", "test3", "test"};
This mean that all values that considered as String in java will be cacth to this list.
Thanks for your help.
Example:
we have a java class file like this:
-test.java --------------------------
public class test{
public static void correctString(String sourceFile, String desFile){
String str1 = "Dang Nhon Hoa";
String str2 = "Nguyen Quang Minh";
String str3 = "Test1" + "test2" + "test3";
System.out.print(str3 + str1 + str2 + "test");
}
}
--------------------------
We have to write a method that read the java file above then return the expected String array content values like this:
returnArrayValues = {"Dang Nhon Hoa", "Nguyen Quang Minh", "Test1", "test2", "test3", "test"};
This mean that all values that considered as String in java will be cacth to this list.
Thanks for your help.
Hi,
A better implementation of describe
public static String[] describe(Object obj) {
String[] sa = null;
ArrayList sb = null;
if (obj != null) {
Field[] fields = obj.getClass().getFields() ;
int length = fields.length;
sb = new ArrayList(length);
for (int i = 0; i < length; i++) {
try {
if (fields[i].getClass().getN ame().equa ls(
"java.lang.String")) {
sb.add(fields[i].get(obj)) ;
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
if (sb != null) {
sa = (String[]) sb.toArray(sa);
}
return sa;
}
A better implementation of describe
public static String[] describe(Object obj) {
String[] sa = null;
ArrayList sb = null;
if (obj != null) {
Field[] fields = obj.getClass().getFields()
int length = fields.length;
sb = new ArrayList(length);
for (int i = 0; i < length; i++) {
try {
if (fields[i].getClass().getN
"java.lang.String")) {
sb.add(fields[i].get(obj))
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
if (sb != null) {
sa = (String[]) sb.toArray(sa);
}
return sa;
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
@CEHJ: that good but when the file have some string like this:
String str = "unde\146ine\u0064";
then the final result is: "undefine\u0064";
this is not exactly equal to the source String.
Please correct this.
Thanks
String str = "unde\146ine\u0064";
then the final result is: "undefine\u0064";
this is not exactly equal to the source String.
Please correct this.
Thanks
ASKER
Oh! Sorry for the final result I posted above.
The result of your solution is "undefineu0064"
And it still not equal to the source file.
The result of your solution is "undefineu0064"
And it still not equal to the source file.
Not sure if that escapement can be avoided
ASKER
Please show me the better solution for that case.
Thanks.
Thanks.
Did you try any of the earlier solutions?
Hi hieukhtn,
After reading the previous posts I am unsure of your exact requirements -
1. Do you want to be able to return a String Array containing the values of all String objects in a Java Class OR
2. Do you want a String Array of all Words in ANY source file ?
StringTonenizer is not recommended
http://java.sun.com/j2se/1.4.2/docs/api/java/util/StringTokenizer.html
Must you insist with a similar requirement use String.split() instead
After reading the previous posts I am unsure of your exact requirements -
1. Do you want to be able to return a String Array containing the values of all String objects in a Java Class OR
2. Do you want a String Array of all Words in ANY source file ?
StringTonenizer is not recommended
http://java.sun.com/j2se/1.4.2/docs/api/java/util/StringTokenizer.html
Must you insist with a similar requirement use String.split() instead
Sorry - my code of course uses StreamTokenizer, not StringTokenizer
Sorry CEHJ,
It admit it was my oversight
It admit it was my oversight
ASKER
@Jaax: Yes! I want to get String Array of all Words in ANY source file.
@CEHJ: I'll try with StreamTokenizer
@CEHJ: I'll try with StreamTokenizer
:-)
Instead of reading the java source file per se, if the class file is availeble then the following utility method would be of help
....
YourCustomClass yourCustClass = new YourCustomClass ();
String str = describe(yourCustClass);
if(str != null && str.length > 0){
for(int i=0; i < str.length; i++){
System.out.println("String
}
}
...
public static String[] describe(Object obj) {
String[] sb = null;
if (obj != null) {
Field[] fields = obj.getClass().getFields()
int length = fields.length;
sb = new String[length];
for (int i = 0; i < length; i++) {
try {
if (fields[i].getClass().getN
"java.lang.String")) {
sb[i] = (String) fields[i].get(obj);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
return sb;
}