multiple column in the file
the file record have 4 field and storing the records in string[]
record=bufferedReader.readLine();
String[] str = record.split("");
System.out.println(str[2]);
These code will print the 3 column of the file but if we want to read each element of str[2]; then how to do that
[330 byte] By [
seemapa] at [2007-11-26 13:01:39]

> the file record have 4 field and storing the records
> in string[]
> record=bufferedReader.readLine();
> String[] str = record.split("");
> System.out.println(str[2]);
>
>
> These code will print the 3 column of the file but if
> we want to read each element of str[2]; then how to
> do that
Since I don't know what you mean by 'each element of str[2]' because I don't know what a line of your file looks like this is just about impossible to answer!
String[] str = record.split("");str is the array of all recordsstr[2] is holding all the records of the 3 column means if the third column is price then str[2] ={"56","345","57","78", .........}now i want to read all the elemnts of str[2] one by one
String [] str2 = str[2].split(",");~Tim
String s2 = "{\"56\",\"345\",\"57\",\"78\"}";
System.out.println(s2);
String[] splitS2 = s2.substring(2, s2.length()-2).split("\",\"");
for (String s : splitS2)
{
System.out.println(s);
}
or an alternative to get splitS2String[] splitS2 = s2.replaceAll("\\{\"|\"}","").split("\",\"");
Message was edited by:
sabre150
Thx all .Yes u guys are right but the main problem is
when first time we split the records of the file then we know the delimiters used in the file second time if we want to split then we don't know the delimter because its java who is storing in array and all elemnt data type is String but which delimiter it is using atleast i do not know.
If anybody have idea of delimiter used in this case pls provide the information.
When you call split, everything from the start to the delimiter is stored in the array. Then delimiter itself is not. So, :
public static void main(String[] args) {
String test = "\"Larry\",\"Moe\",\"Curly\"\t1234,5432,97854";
String [] arr = test.split("\t");//Split the test string at any tab
for(String s : arr)
{
System.out.println(s);
}
String [][] arr2 = new String[arr.length][];
for(int i = 0; i< arr.length; i++){
arr2[i] = arr[i].split(","); //loop thru the array created by the first split, and split it at any comma
for(int j = 0; j<arr2[i].length; j++)
{
System.out.println("\t" + arr2[i][j]);
}
}
}
Of course, this will only work if you know the format of the data, but since you need the format to start with, then you should easily be able to figure it out.
~Tim>