0

The goal of my code is to ask for a user input, the user specifies this input and my code is to search and find out which row the user input is located.

Once located, I will then have code to extract that row into something (string array?) so I can then edit part of it's contents.

My problem is that I'm not sure which code/methods/classes to use in order to do this.

I have my code set up right now so that it reads the .CSV file and outputs it into a String array of type list. I then iterate through the array and find out if the user input that was specified is located within the file.

At the moment, my code only says if it is found, but I am unsure as to how to extract that specific row, and I was hoping you guys could help me out.

Here's my code at the click of a button:

try{

        String strSearch = searchSerialField.getText();

        CSVReader reader = new CSVReader(new FileReader("test.txt"), ',');
        List<String[]> myEntries = reader.readAll();
        reader.close();

        //Write to existing file
        //CSVWriter writer = new CSVWriter(new FileWriter("test.txt"), ',',CSVWriter.NO_QUOTE_CHARACTER, "\r\n");

        //Iterate through my array to find the row the user input is located on
        for (String[] line : myEntries)
        {
            ArrayList<String> newLine = new ArrayList<String>();
            for (String word : line)
            {
                if (word.contains(strSearch))
                {
                    //Need a method so I can find out what row my item is on
                    System.out.println("Found - Your item is on row: ...");

                    //Code to extract row into something (String array?) so I can edit it's contents

                    //I then need to write the edited row back to it's original row

                }else
                {
                    System.out.println("Not found");
                }
            }
        }

   }catch (IOException e)
        {
            System.out.println(e);
        }

I've tried my best but I'm now stumped as to how I'm meant to extract the row, how can I go about doing this?

Thanks for the help.

juiceb0xk
  • 949
  • 3
  • 19
  • 46

3 Answers3

0

What you need is just not a nested loop, just a Arrays.toString method.

import java.io.FileReader;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

import com.opencsv.CSVReader;


public class Test {
    public static void main(String[] arg){
        search();
    }
    public static void search(){
        try{

            String strSearch = "banana";

            CSVReader reader = new CSVReader(new FileReader("d:\\textfile.txt"), ',');
            List<String[]> myEntries = reader.readAll();
            System.out.println(myEntries.size());
            reader.close();

            //Write to existing file
            //CSVWriter writer = new CSVWriter(new FileWriter("test.txt"), ',',CSVWriter.NO_QUOTE_CHARACTER, "\r\n");

            //Iterate through my array to find the row the user input is located on
            int i=1;
            for (String[] line : myEntries)
            {

                String textLine = Arrays.toString(line).replaceAll("\\[|\\]","");
                System.out.println(textLine);
                  if (textLine.contains(strSearch))
                    {
                        //Need a method so I can find out what row my item is on
                        System.out.println("Found - Your item is on row: ...:"+i);

                        //Code to extract row into something (String array?) so I can edit it's contents

                        //I then need to write the edited row back to it's original row

                    }else
                    {
                        System.out.println("Not found");
                    }
                  i++;
            }

       }catch (IOException e)
            {
                System.out.println(e);
            }
    }
}
zawhtut
  • 8,335
  • 5
  • 52
  • 76
  • 1
    This does not answer my question. How do I find out what row a certain value is on? In my .CSV file, 'banana' is located on the 3rd row. What code should I be implementing in order to find out what row it's on? – juiceb0xk Nov 11 '16 at 03:53
  • 1
    Unbelievable I got down voted for this. Kudo to stackoverflow. For your question, I updated my answer. Just need to add index. – zawhtut Nov 11 '16 at 04:10
  • Great, that answered part of my question. Now, to extract that line into something so I can edit the accompanying contents of it. i.e. `banana,store`. I'd like to only edit the location so I can end up with `banana,home`. – juiceb0xk Nov 11 '16 at 04:35
  • Sorry, it's a pain to answer down voted post. Do it by yrself. no offense. – zawhtut Nov 11 '16 at 04:39
  • I didn't downvote it, but okay. Thanks for your help. – juiceb0xk Nov 11 '16 at 04:40
0

you can achieve that by simply counting the indices.

    String strSearch = searchSerialField.getText();
    CSVReader reader = new CSVReader(new FileReader("test.txt"), ',');
    List<String[]> myEntries = reader.readAll();
    reader.close();

    for (int row = 0; row<myEntries.size(); row++){
        for (int column = 0; column< myEntries.get(row).length; column++ ){
            if(myEntries.get(row)[column].trim().equalsIgnoreCase(strSearch)){
                System.out.println("found - your item is on row " +row + ", column "+ column);
            }              
        }
    }
Eritrean
  • 15,851
  • 3
  • 22
  • 28
-1
public static String[] getSpecificRowFromCsv(String filePath, int rowNum) {
    List<String[]> fileData = null;
    CSVReader reader = null;
    try {
        reader = new CSVReader(new FileReader(filePath));
        fileData = reader.readAll();
        reader.close();
    } catch (Exception e) {
        e.printStackTrace();
    }             

    return fileData.get(rowNum - 1);
}
Daniel Puiu
  • 962
  • 6
  • 21
  • 29