-2

I am developing java Swing based desktop application without using database (file based application). whenever i am trying to extract the specified row wise data, the constraints were not working as expected.

i am reading the entire text file line by line using scanner, but i am struggling to give the constraint for search functionality.

Could you please give me some suggestions on how to extract row wise data using column as a constraint..

e.g:

SL.No|Name|Salary  
1|ABC|1000  
2|DEF|2000  
3|GHI|1500

note: this is my file structure, Could you please give me some suggestions on how to extract row wise data using column as a constraint..(like Name)

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
kishoor
  • 1
  • 1
  • 1
    Cannot understand your question. Please provide more details. BTW: StackOverflow is not do-my-job-please portal. Try to do the task by yourself, and when you have a concrete question, ask them here. – Sergiy Medvynskyy Apr 27 '18 at 09:06
  • Assuming the first row is the row headers. Read the first row. [Split](https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#split(java.lang.String)) it by delimiter (`row.split("|")` in your case) find the column name in the resulting array. Remember somewhere the index of that column name. Read the rest of the file. Split the rows by the same delimiter. The data you look for will be on the indexth position in the arrays. – MatheM Apr 27 '18 at 10:07
  • *"I am developing java **Swing** based desktop application"* Sure, but the Swing part is irrelevant to *"How to extract the row data-wise data with column constraint from a text file in java"* - which is a task that could be worked out in a command line application. So .. do that, then figure out later how to dovetail that into the GUI application. Tag removed! – Andrew Thompson Apr 27 '18 at 10:14
  • Basically you cannot select just some column from file, its a simply one long string, what can you to do is to read first row as the header, split by delimiter- in array find the position of the given column and then read this position in each else rows. The most of the code is eg. there https://codereview.stackexchange.com/questions/15062/reading-a-line-from-a-text-file-and-splitting-its-contents – xxxvodnikxxx Apr 27 '18 at 10:45

1 Answers1

0

First create wrapper class for data

public class Data
{

    private int serialNo;
    private String name;
    private double salary;


    public int getSerialNo()
    {
        return this.serialNo;
    }
    public void setSerialNo(int serialNo)
    {
        this.serialNo = serialNo;
    }
    public String getName()
    {
        return this.name;
    }
    public void setName(String name)
    {
        this.name = name;
    }
    public double getSalary()
    {
        return this.salary;
    }
    public void setSalary(double salary)
    {
        this.salary = salary;
    }


}

Create separate class for fetching data from file

public class Parser
{

    private File file;
    private BufferedReader bufferedReader;

    public Parser(File file)
    {
        this.file = file;
    }

    public ArrayList<Data> parse() throws IOException
    {

        ArrayList<Data> dataList = new ArrayList<>();

        bufferedReader = new BufferedReader(new FileReader(file));

        //skip first line as it contains colums
        bufferedReader.readLine();

        String line = "";

        while((line = bufferedReader.readLine()) !=null)
        {

            String[] tokens = line.split("\\|");

            Data data = new Data();
            data.setSerialNo(Integer.valueOf(tokens[0]));
            data.setName(tokens[1]);
            data.setSalary(Double.valueOf(tokens[2]));
            dataList.add(data);


        }


        return dataList;

    }


}

Finally Use like this

Parser parser = new Parser(new File("data.txt"));

ArrayList<Data> dataList = parser.parse();

for(Data data : dataList)
 System.out.println("Ser="+ data.getSerialNo() + " Name=" + data.getName() + " Salary=" + data.getSalary());

Console output

Ser=1 Name=ABC Salary=1000.0
Ser=2 Name=DEF Salary=2000.0
Ser=3 Name=GHI Salary=1500.0
Umer Farooq
  • 762
  • 1
  • 8
  • 17