change the String List code in java 1.4

1.2k Views Asked by At

I need to change the below code in java 1.4, can anyone help me to do so.

the main problem I am getting is to convert the string type of list

line List splitStringList = new ArrayList ()

and line

for (String str: splitStringList) is generating error for java 1.4

public class Demo2ReferDemo1 {
     public static void main (String [] args) {
            String inputStr = "00400 - 00479,00100 - 0022200su,00100 - 00228,00100 - 00228,00400 - 00479,lab661,";

            StringBuffer sb = new StringBuffer (inputStr);
            List<String> splitStringList = new ArrayList<String> ();
            boolean insideDoubleQuotes = false;
            StringBuffer field = new StringBuffer ();

            for (int i=0; i < sb.length(); i++) {
                if (sb.charAt (i) == '"' && !insideDoubleQuotes) {
                    insideDoubleQuotes = true;
                } else if (sb.charAt(i) == '"' && insideDoubleQuotes) {
                    insideDoubleQuotes = false;
                    splitStringList.add (field.toString().trim());
                    field.setLength(0);
                } else if (sb.charAt(i) == ',' && !insideDoubleQuotes) {
                    // ignore the comma after double quotes.
                    if (field.length() > 0) {
                        splitStringList.add (field.toString().trim());
                    }
                    // clear the field for next word
                    field.setLength(0);
                } else {
                    field.append (sb.charAt(i));
                }
            }
            for (String str: splitStringList) {
                System.out.println ("Split fields: "+str);
            }
        }
        }
2

There are 2 best solutions below

0
On BEST ANSWER

Here you have your class in java 1.4 compatible format. Changes are:

  • ArrayList declaration without generics <>
  • The last "for" loop works with an Object variable instead of String variable, because the ArrayList returns an Object reference which later can be converted into String. The class code is as follows: import java.util.ArrayList; import java.util.List; public class Demo2ReferDemo1 { public static void main(String[] args) { String inputStr = "00400 - 00479,00100 - 0022200su,00100 - 00228,00100 - 00228,00400 - 00479,lab661,"; StringBuffer sb = new StringBuffer(inputStr); List splitStringList = new ArrayList(); boolean insideDoubleQuotes = false; StringBuffer field = new StringBuffer(); for (int i = 0; i < sb.length(); i++) { if (sb.charAt(i) == '"' && !insideDoubleQuotes) { insideDoubleQuotes = true; } else if (sb.charAt(i) == '"' && insideDoubleQuotes) { insideDoubleQuotes = false; splitStringList.add(field.toString().trim()); field.setLength(0); } else if (sb.charAt(i) == ',' && !insideDoubleQuotes) { // ignore the comma after double quotes. if (field.length() > 0) { splitStringList.add(field.toString().trim()); } // clear the field for next word field.setLength(0); } else { field.append(sb.charAt(i)); } } for (Object str : splitStringList) { System.out.println("Split fields: " + str); } } }
0
On

JAVA 1.4 does not include generics, so List<String> splitStringList = new ArrayList<String> (); is not valid.

Change it to List splitStringList = new ArrayList ();

You'll also find that

for (String str: splitStringList) {
        System.out.println ("Split fields: "+str);
}

will cause a problem - this syntax wasn;t introduced until Java 5 either, so you'll have to fall back on one of the other loops ( a for loop for example) and cast the List member to String