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);
}
}
}
Here you have your class in java 1.4 compatible format. Changes are:
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);
}
}
}