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.
Java

Avatar of undefined
Last Comment
CEHJ
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;
    }

Avatar of Jaax
Jaax
Flag of India image

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

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
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
Avatar of hieukhtn
hieukhtn

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

Not sure if that escapement can be avoided
Avatar of hieukhtn
hieukhtn

ASKER

Please show me the better solution for that case.
Thanks.
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Did you try any of the earlier solutions?
Avatar of Jaax
Jaax
Flag of India image

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
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Sorry - my code of course uses StreamTokenizer, not StringTokenizer
Avatar of Jaax
Jaax
Flag of India image

Sorry CEHJ,
 It admit it was my oversight
Avatar of hieukhtn
hieukhtn

ASKER

@Jaax: Yes! I want to get String Array of all Words in ANY source file.

@CEHJ: I'll try with StreamTokenizer
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

:-)
Java
Java

Java is a platform-independent, object-oriented programming language and run-time environment, designed to have as few implementation dependencies as possible such that developers can write one set of code across all platforms using libraries. Most devices will not run Java natively, and require a run-time component to be installed in order to execute a Java program.

102K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo