Link to home
Start Free TrialLog in
Avatar of hieukhtn
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.
Avatar of Jaax
Jaax
Flag of India image

Hi,
 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 Value["+i+"] : "+str[i]);
     }
 }
...

  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().getName().equals(
                            "java.lang.String")) {
                        sb[i] = (String) fields[i].get(obj);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        return sb;
    }

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().getName().equals(
                            "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
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of hieukhtn
hieukhtn

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
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.
Not sure if that escapement can be avoided
Please show me the better solution for that case.
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
Sorry - my code of course uses StreamTokenizer, not StringTokenizer
Sorry CEHJ,
 It admit it was my oversight
@Jaax: Yes! I want to get String Array of all Words in ANY source file.

@CEHJ: I'll try with StreamTokenizer
:-)