Reading from a CSV File and Searching for a Record in Java



Reading from a CSV File and Searching for a Record in Java

Hello guys, I’m back with another file handling video and today we will be killing 2 birds with one stone by allowing the user to search for a record via reading the file, if it is found then tell the user the record details, if not found then tell them the record can’t be found. This is in my opinion the hardest thing to dofor the As Practical exam but watch this video and practice in a couple of scenarios and you will be a pro. Thanks for being a great audience and we will see you next time!

Easter holidays are over but I will try to upload as often as possible. Why not subscribe to keep notified when I upload?

Social media:
Instagram:
Twitter:

Nguồn: https://baggersoriginals.com/

Xem thêm bài viết khác: https://baggersoriginals.com/cong-nghe/

Article Categories:
Công Nghệ

Comments

  • hello Max! I tried your code. I also edited a little in it. (statement below)

    if(ID.equals(searchItem) || name.equals(searchItem) || age.equals(searchItem))

    i also have this one for my txt file:

    2311,Spencer,39

    2435,Ron,25

    5474,Nico,20

    but upon searching for the age "39", it can't find a match not until I tried 20. It only detects the age of the last entry.

    What could be wrong?

    Spencer Arsenal July 2, 2020 6:04 pm Reply
  • Thanks alot for the help your videos have really helped simply the file.io concept !!

    cletus vaz July 2, 2020 6:04 pm Reply
  • Hi, do you know how we would do this in JavaFX? Thanks.

    Joe Sylvester July 2, 2020 6:04 pm Reply
  • thank you so much! this really helped out a lot

    Caine July 2, 2020 6:04 pm Reply
  • shouldnt the !found be equal to true?

    Muhammad Arslan Bhatti July 2, 2020 6:04 pm Reply
  • Is there any way to make this function more efficient if you are only checking for one value like in this example. Instead of going through each item, just check if one item in a row is equal. In other words do you have to write x.next(); to go through each value or could you skip some of the values? How would that impact the performance?
    (I have to search through a large database with thousands of rows and around ten or so columns)

    JONASXD360 July 2, 2020 6:04 pm Reply
  • Hello! How to make this into dynamic? I mean the user will input the search term into the console then it will display the result.

    Balut Pinoy TV July 2, 2020 6:04 pm Reply
  • Hello! How to make this into dynamic? I mean the user will input the search term into the console then it will display the result.

    Balut Pinoy TV July 2, 2020 6:04 pm Reply
  • Hi Max, I have been using your tutorial as base for creating a login page. I am having 1 problem. when the data in the file is read, it only gets accurate answer for the final record. for the previous records, it says the 3 column data is inaccurate "password incorrect", which means it is looking at the wrong column but for the final record it works fine.

    this is called in Main class where main method exists. through EventQueue.

    Data in txt file
    123,Zabi,password
    124,Fazal,letmein
    125,Zahir,pass
    126,Amir,news

    import java.awt.Component;

    import java.awt.FlowLayout;

    import java.awt.GridLayout;

    import java.awt.event.ActionEvent;

    import java.awt.event.ActionListener;

    import java.io.File;

    import java.util.Scanner;

    import javax.swing.ImageIcon;

    import javax.swing.JButton;

    import javax.swing.JFrame;

    import javax.swing.JLabel;

    import javax.swing.JOptionPane;

    import javax.swing.JPanel;

    import javax.swing.JTextField;

    public class MainFrame {

    static JFrame frame;

    static JPanel jpHold;

    final static String file = "users.txt";

    private static String searchUserID;

    private static String searchPassword;

    private static String userId;

    private static String empName;

    private static String password;

    private static Scanner x;

    public void search() {

    //search(searchUserID,searchPassword,file);

    }

    public static Component hold() {

    jpHold = new JPanel();

    jpHold.setBounds(50, 50, 300, 300);

    jpHold.setLayout(new GridLayout(3,1,5,5));

    JTextField jtfUserName = new JTextField();

    jtfUserName.setBounds(50, 50, 200, 50);

    //jtfUserName.setText("");

    JTextField jtfPassword = new JTextField();

    jtfPassword.setBounds(50, 255, 200, 50);

    JButton login = new JButton("Log in");

    login.setBounds(210, 50, 100, 50);

    login.addActionListener(new ActionListener() {

    @Override

    public void actionPerformed(ActionEvent e) {

    boolean found = false;

    searchUserID = jtfUserName.getText();

    searchPassword = jtfPassword.getText();

    try

    {

    x = new Scanner(new File(file));

    x.useDelimiter("[,n]");

    while(x.hasNext() && !found)

    {

    userId = x.next();

    empName = x.next();

    password = x.next();

    if(userId.equals(searchUserID))

    {

    found = true;

    }

    }

    if(found && password.equals(searchPassword))

    {

    JOptionPane.showMessageDialog(frame, "Log in successful");

    }

    if(found && password != searchPassword)

    {

    JOptionPane.showMessageDialog(frame, "Password is not correct");

    }

    else

    {

    JOptionPane.showMessageDialog(frame, "Log in failed");

    }

    }

    catch(Exception e1)

    {

    }

    }

    });

    jpHold.add(jtfUserName);

    jpHold.add(jtfPassword);

    jpHold.add(login);

    return jpHold;

    }

    public MainFrame() {

    initialise();

    }

    public void initialise() {

    frame = new JFrame();

    frame.setVisible(true);

    frame.setResizable(false);

    frame.setSize(400, 400);

    frame.setLayout(new FlowLayout());

    frame.setContentPane(new JLabel(new ImageIcon("")));

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    frame.add(hold());

    }

    }

    Zabi Sidiqkhil July 2, 2020 6:04 pm Reply
  • Thank you so much I implemented this for a button and it works so nice. the buttons reads, retrieves , adds to JTable and writes to another file

    Zabi Sidiqkhil July 2, 2020 6:04 pm Reply
  • Hey, I was wondering if it was possible to save the information in the CSV file into an array, and output it in order of age, so the youngest one would get outputted first with its ID and name, then the next and so on?

    Raphael Granado July 2, 2020 6:04 pm Reply
  • Kindly let me know if I have to find several matching records then what changes needs to be done within the program.

    harsh sinha July 2, 2020 6:04 pm Reply
  • Thumbs up guy was here, splendid clean explantation, nice job.
    buy reading the comment here i should understand that your not a teacher and that shocks me because you reaaly sound like one.

    alain soral July 2, 2020 6:04 pm Reply
  • Nice! got mine to work!

    Cecilia Barber July 2, 2020 6:04 pm Reply
  • I can't do the import method things.

    What are the imports that I need for this code?

    jongmagee July 2, 2020 6:04 pm Reply
  • Sir, do you have any tutorials that shows all of the record in a single file instead of 1 record ? BTW your tutorials really helps me.

    Razor Abaquita July 2, 2020 6:04 pm Reply
  • Great Video! Just a quick question, what if the CSV file has headers, how do I parse through it without considering the catagories within the file? Thank You!

    Aadil Mufti July 2, 2020 6:04 pm Reply
  • How do i use this search method on a gui ? I would like to search for an ID using the txtfield and display the data on the other txtfields

    SayRun Plays July 2, 2020 6:04 pm Reply
  • Doesn't look like the code does anything when run. Nothing is calling readRecord() from main()

    Alex Wang July 2, 2020 6:04 pm Reply
  • When I run it I just get "Process finished with exit code 0"

    Alex Wang July 2, 2020 6:04 pm Reply
  • Instead of writing a string in the searchterm what if i were to take that as user input? I am using system.in to take the input but idk what to do with it. Thanks

    Sam Khisa July 2, 2020 6:04 pm Reply
  • hi!! is there any way to read only id part of each row, and whenever the id matches to user given id then read their corresponding name and age and printing them. Thanks in advance

    Aayush Agrawal July 2, 2020 6:04 pm Reply
  • very good video

    raghavendra reddy July 2, 2020 6:04 pm Reply
  • Where do I save my notepad named "Tutotial.txt"? in what directory?

    Ardy Ubanos July 2, 2020 6:04 pm Reply
  • I am a teacher and you file handling videos really helped me to teach my student.

    Abhishek Bhardwaj July 2, 2020 6:04 pm Reply
  • Thanks,….

    Abhishek Bhardwaj July 2, 2020 6:04 pm Reply
  • hey can you tell me how to use CSV parser ? for editing the csv files

    Adithya Kowshik July 2, 2020 6:04 pm Reply
  • great tutorial and detailed explanation for each line. I t helps me understand what everything is doing and what I need to do in a project ive got.
    What did confuse me a little was when you explained the conditions of the while loop and said that !found means 'is found', but I eventually knew what you meant was that the while loop will continue until 'found' is found to be true. thank you again, glad I found your tutorials, Ive subscribed on just this vid, cheers mate.

    Alex Eaton July 2, 2020 6:04 pm Reply
  • I've tried implementing into a gui and doesnt seem to do what i want it to do? Could you help if you can?

    Papa Gobseck July 2, 2020 6:04 pm Reply
  • what if we want to search for the third element in the row and my program is only working on first row of file on further rows it is giving no such element found exception plss help….!!!!

    Jyoti Rao July 2, 2020 6:04 pm Reply
  • Great Work!

    Ishan Varshney July 2, 2020 6:04 pm Reply
  • A very good video. However what if my id was in the 3rd column and then display the row, what do we do then?

    Ayan Choudhury July 2, 2020 6:04 pm Reply
  • Awesome tutorial sir!! thanks!

    Ayush July 2, 2020 6:04 pm Reply
  • Hello. I followed your tutorial, but why is it not working for me? I use FIRST NAME as the one to be searched. But it always says "RECORD NOT FOUND".

    Harvey Quinto July 2, 2020 6:04 pm Reply
  • Great clear video!
    If I wanted to add a list of items and move items from a file would this be the right approach? or would xml using JSP be better?
    Thanks

    Tony Edwards July 2, 2020 6:04 pm Reply
  • Hi, great and simple tutorial, but I have one issue with my read method. For some reason it doesn't read the last value in the text. I followed your tutorial properly, but I just can't seem to find whats causing the problem. Maybe you have any ideas?

    Justinas Daukša July 2, 2020 6:04 pm Reply
  • M
    Could anyone help ? instead of using array, I wanna use multiple CSV files to join, search , delete, insert in two files in java .. thanks

    Matab Asiri July 2, 2020 6:04 pm Reply
  • Hi
    Suppose i want to ask a question like print name of student with id "5432"? How to go about it

    Murtaza Haji July 2, 2020 6:04 pm Reply

Leave a Reply

Your email address will not be published. Required fields are marked *