i have write a code for making a string that is being read from CSV file.
i want to make a string from particular location of each and every line.
i have attached a code what i have done.but i want it as it for any number of location.
In dataset.csv file there is rows like
abc,12,male,12345,flu,yes
apm,15,female,32165,fever,no
plz,45,female,32154,flu,no
ert,32,female,32564,fever,yes

If i run following code with identifier =3 and location are 1,2,3 then i need output as
12,male,12345 in first iteration
15,female,32165 in second iteration
and so on.

public static void main(String args[])throws IOException
{

int q,i,loc[];

DataInputStream din = new DataInputStream(System.in);


System.out.println("Enter No. of Idenifier =");
q=Integer.parseInt(din.readLine());
loc= new int[q];

for(i=0;i<q;i++)
{
System.out.println("Enter "+i+"th identifier location :");
loc[i]=Integer.parseInt(din.readLine());
}

BufferedReader CSVFile = new BufferedReader(new FileReader("dataset.csv"));

String dataRow = CSVFile.readLine(); // Read first line.

int dataIndex = 0;
while (dataRow != null)
{
   String[] dataArray = dataRow.split(",");

   for (String item:dataArray) {
    if(item.length()==0)
     System.out.print("no records found..." + "\t");
    else{ continue;}    
   }

String aspStr = null;
for(int j=0;j<q;j++)
{
    aspStr += dataArray[loc[j]]+",";//+dataArray[loc[1]]+","+dataArray[loc[2]];
}

    dataRow = CSVFile.readLine();// Read next line of data.
}

but i got null at the starting of each aspStr output

So please tell me how can i got output as
12,male,12345
15,female,32165
and so on.

please reply.

line 34 you initialise aspStr as null, but then on line 37 you try to append text to it. You can't append anything to a null. Initialise it to an empty String ie "" instead.

commented: really good suggestion +0

Thanks james,

For your kind suggestion.
but there is also one problem.
i have used String aspStr="";
but after in for loop it always prints one extra ","(comma) after each execution of loop.

it gives output as
12,male,12345,
15,female,32165,
One extra comma after every line, so how to remove that or it will not appended when last identifier is appended.

Thank you.

waiting for your reply.

That's a standard problem! Easiest solution is to use substring to take all the characters from the string except the last one.

commented: Solved +0

Thanks a lot james...

My problem is solved.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.