0

I am trying to segregate my data into multiple array list, so that I can use them later-on in my code. But I am not able to put my data in array list.

My code is about segregating the data into three array list of different Subjects (Example:Physics,chemistry) as per various filters, which you will find in my code.

Input data file:

1|150|20150328|20150406|Physics|1600|1600|2|68|92
2|152|20150328|20150406|Physics|1600|1500|2|68|89
3|153|20150328|20150406|Physics|1600|1500|2|68|60
4|155|20150328|20150406|Physics|1600|1600|2|68|72
5|161|20150328|20150406|Chemistry|1600|1600|2|68|77

Here's my code:

Public Class filter{
  public static void main(String args[])

BufferedReader in= null;
BufferedWriter out= null;
String in_line;
String PrevRollNo= "";
int PrevDate= 0;

ArrayList<transaction> PhysicsList= new ArrayList<transaction>();
ArrayList<transaction> scList= new ArrayList<transaction>();
ArrayList<transaction> Chemistry= new ArrayList<transaction>();

try{
in = new BufferedReader(new FileReader(Path for input file));
File out_file= new File(Path for output file);
if(!out_file.exists())
{
(!out_file.createNewFile();
}

 FileWriter fw=new FileWriter(out_file);
out= new BufferedWriter(fw);

while ((in_line=in.readline())!=null)
{
Transaction transact=new Transaction(in_line);

if(transact.RollNo.equals(PrevRollNo))
{
if(transact.subject.equals("Physics")&& transact.Prs_Date= PrevDate
{
PhysicsList.add(transact);
}
else if(transact.subject.equals("Physics")&&transact.wk_date != PrevDate}

Iterator<Transaction> it;
if(!transact.RoomNo.equals("102")&&!transact.lcl_RoomNo.equals("102");

{
it= scList.iterator();
while(it.hasnext())
{
Transaction sc= it.next();

if(sc.lcl_RoomNo.equals(transact.RoomNo) && sc.l1 equals(tansact.l1) && sc.l2 equals(transact.l2)

if(sc.marks==transact.marks)
{
transact.srsfound= true;
}
else
{
System.out.print.ln( "not found");
}
scList.remove(sc))
out.write(in_line);
break;
}}}}

Static Class Transaction
{
Public String RollNo, Subject, RoomNo, lcl_RoomNo, l1, l2;

Public int wk_date, prs_date;
Public double marks , amt;
Public boolean srcfound, tgtfound;

Public Transaction(String in_line)
{
String [] SplitData= in_line.split("\\|");
RollNo = SplitData[1];
Subject = SplitData[4]
RoomNo = SplitData[5];
lcl_RoomNo = SplitData[6];
l1 = SplitData[7];
l2 = SplitData[8];
wk_date = SplitData[3];
prs_date = SplitData[2];

marks = Double.parsedouble(SplitData[9]);
amt = Double.parsedouble(SplitData[]);
srcfound = false;
tgtfound = false;
}

Kindly help with your expertise.

2 Answers 2

1

Use Java 8 NIO and Streams. It will ease the job.

Files.lines(Paths.get("fileName.txt")).map(line -> {
    String[] tokens = line.split("|");
    //tokens contains individual elements of each line. Add your grouping logic on tokens array
}
Sign up to request clarification or add additional context in comments.

3 Comments

can you please suggest changes in the present code. I am not familiar with tokens. Thanks
@sareen, tokens is just an array of Strings. This array contains the individual words in front of the delimiter |
@K139...I am facing another issue with this, you see that I am using transact.Prs_Date= PrevDate I have given int PrevDate= 0; & PrevDate = transact.prs_date. It doesnt work for the first row and the last row. Can you hlp me out in this ?
0

I agree with the other answer in some ways. NIO should be used, it makes it a lot easier. However, I would avoid streams and instead use the readAllLines method like so:

try{
    List<String> filecontents = new String(Files.readAllLines(file.toPath()); //file is the object to read from.
    for(int i = 0; i < filecontents.size(); i++){
        String line = lines.get(i);
        //New code starts here
        if(!line.contains("|") continue; //Ignore that line
        //New code ends here
        String[] array = line.split("|");
        ArrayList<String> list = new ArrayList<String>();
        for(int a = 0; a < array.length; a++){
            String part = array[a];
            list.add(part);
        }
        Transaction t = new Transaction(line);
        if(line.contains("Physics") PlysicsList.add(t);
        else if(line.contains("Chemistry") Chemistry.add(t);
        else{ //Do nothing}
    }
}catch(IOException e){
    e.printStackTrace();
}

EDIT: I added a check in there. The reason the first and last lines may not be working is if the lines that are being parsed are not being parsed properly. See if this fixes your issue

5 Comments

can you please explain on (file.toPath()) thing. and how to use it ? Thanks
The readAllLines method is Files.readAllLines(Path p). The java.io.File class contains a toPath(); method, which converts a File to a Path
Thanks alot for this. I am facing another issue with this, you see that I am using transact.Prs_Date= PrevDate I have given int PrevDate= 0; & PrevDate = transact.prs_date. It doesnt work for the first row and the last row. Can you hlp me out in this ?
Try putting a | at the start of the first entry and and the end of the last entry. Does that fix your problem?
No, I cant change the data file,I need some altercation in my codes... please suggest something

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.