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.
Well, I'm sorry for the minor confusion I caused but I am very happy you understood the point at the end. Also thanks a lot for the positive feedback, you have no idea how much it means! I am also really thankful you subscribed and hope my future uploads meet your needs.
You have no idea how much it means to hear that! I'm really greatful you took the time to write this comment as I am aware being a teacher can be time consuming. I hope future videos can be as useful.
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
for the filepath, did you drag the tutorial.txt into the project folder or did you get the filepath from where u had it stored in your computer since i keep getting an error?
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!
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)
I would advise looking at random access files (one of my recent videos covers reading from one and will upload one on writing to one using random file access). Hashing is also a way to make searching more efficient.
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?
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?
Right, I am very sorry you are having this issue. Firstly what do you mean by last value? Do you mean the last record or the last field? Secondly, could you send your code to me (using Hastebin(hastebin.com/) or whatever you fancy). Seeing the code should make finding the issue a lot quicker and ty for your kind words, they make my day.
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?
As you read each record in the file, store it into an array of arraylist. After you have read the file, sort each record using the name field. I could cover this in a future tutorial.
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("[, ]"); 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()); } }
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....!!!!
Right, I am very sorry the code isn't working as of now. I will try my best to assist you. So to save time may I see your code? It will assist in finding the cause of the issue you are encountering (hastebin is a great way to send code).
You would need to create a textfield for each field type and one extra for the search term. You can use the .getText() and .setText() methods to get text from a textfields and set the text of tetxfields. You would pass the .getText data into the read record method and return data read from the file and set the textfields to the data read using the .setText() method.
Thanks a lot! As for your question, I shall do my best to word it in an understandable way so if I make no sense don't be afraid to point that out. x.next() just reads data up until a delimiter (which in the case of this video is a comma or a new line) and we assign the data read to a variable. If your ID is on the 3rd column just simply read the 1st 2 columns (using x.next()) and assign them to variables. Set the 3rd x.next() to equal the ID variable. I modified the code inside the while loop to illustrate what I am trying to say. hastebin.com/ejeyoxehoc.vbs I hope this helps.
You can just create an array list and append each line to it to read the whole file and output the records or do whatever you want with them. Also, I'm glad my tutorials are helping you. It makes my day hearing that.
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.
Thanks a lot, your kind words do make my day. You are also correct, I'm not a teacher, I'm a 19 year old student. Some teachers have also told me that they use my tutorials in lessons.
@@maxodidily will if you're thinking about starting a teacher career i think you're half way there. (saying the guy who left the teaching job though :D )
Have an additional string variable for each additional field a record can have. Also have an additional x.next() for each additional field of a record.
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
Hello, so what you want to do is get the user input by using the scanner object and save it as a string. I then pass that string as a parameter for the readRecord method when called. To get user input using the Scanner object you can use the following 2 lines: "Scanner scanner = new Scanner(System.in); String input = scanner.next();" I don't mention how to get userinput in my videos as it is quite simple and many tutorials exist to explain them and my videos aim to cover topics that aren't common on RU-vid and getting an input makes no real difference to the concept I am teaching as it will just impact some values we pass into the methods, maybe in the future I may make some videos on getting user inputs but for now I hope the 2 lines of code and my explanation are enough. Hope this helps!
Thanks a lot for answering the very basic question lol. Also, suppose you searched for 'mary' and there are 2 'mary's in 2 different lines, how can i get both the data as this tutorial shows only the first occurred data? Thanks in advance !
Right, so I will be covering this in around a months time but here is a logical hint, you need to search the whole file and if any record has the name "Mary" or whatever the search term is, store it to an array list. After that you can interact with the records as you please. Also, any question is good to ask, regardless of how simple it is. Hope this hint helps until I release a video on it.
Assuming you have used the code in this video you would pass "5432" into the readRecord method and where we would print all details of the record we find you could just do "JOptionPane.showMessageDialog(null,"The name for the Student with the ID of 5432 is:" + name1);" or something similar to that. If I have assumed incorrectly or just misunderstood your question or just didn't answer it, I am sorry and feel free to ask again and I won't stop helping until your problem is solved.
Yes, you certainly can access arrays from other classes. Firstly you must create an instance of the class with the array . In this example the class with the array is called 'MyClass1'. You will want to type this 'MyClass1 object = new MyClass1();'. This is a constructor method used to create an instance of a class. Where I typed 'object' is what you would call this instance you create. It can be anything you want. Now our array is called 'namesArray'. In the class that is isn't in we can access it by doing object.namesArray[i] and then something to do with the array like usual. In other words, do what you would normally do with the array but put 'object.' before it (or whatever you called the instance of the class). I hope this helps and if you want to know more about classes and using multiple ones I do have a video going over the basics which maybe u will like but up to u. Happy coding.
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
Here is a solution: hastebin.com/wikocujipi.cs However it isn't very different to what my video shows as no matter what you want to do, you will have to read through every field until the search term is found. Storing each field temporarily as variables really doesn't do any harm. May I ask why you made your request?
Hello, I am sorry you are having an issue. I would like to thankyou for sending me the code and after a quick inspection I have already identified one potential issue and that is with your delimiter. This is what you put 'sc.useDelimiter("[| ]");' when you would need to put 'sc.useDelimiter("[, ]"); '. Instead of a comma, you put a '|' character. If this doesn't fix the issue could I have a copy of what 'myFileHarvey.txt' please. (You can just copy the contents of the file and paste it in hastebin).
This is my 'manageFile.java' - hastebin.com/opulagupaz.cs This is my main - hastebin.com/erevagudex.cs This is my 'myFileHarvey.txt' - hastebin.com/yeciwaxoso.rb Sorry for my way of coding.
Firstly, you have no need to apologise for how you code, there are many styles out there and many solutions to solve the same problems. Encountering issues is all part of the fun too and once u overcome them you learn loads and feel great. So I believe I have identified your issue as I tested your file handling code with the file and got it to read a record matching the name the user inputs. You have a funny way of separating fields of a record, you have a field with loads of spaces then a splitter (the '|') followed by some more spaces and so on. No idea why you are doing that but there is nothing wrong with it. This tutorial was made for CSV files hence why it didn't fully work but you just need to do one thing to make it work for what you want. You have the correct delimiter set but it doesn't take into account all the spaces you have. Let me explain. Let's say we want to read the record for 'Harvey'. We input the name and start searching the file. Each sc.next() will read up to the next '|' or the next line. So the 1st thing the file reads is 'Harvey '. That is not a typo, in the file you have loads of spaces between values of fields and the delimiter (aka splitter aka the '|'). So Java will check if 'Harvey' is equal to 'Harvey ', which it isn't as the 2nd string has loads of spaces. This is why it isn't fining the record. So we could just use normal commas or not leave spaces there but there is another way. We can use the .trim() method. This will remove all spaces in a string. An example would be ' ducks '. If we used .trim() on that string it would become 'ducks'. The same will apply to 'Harvey '. .trim() would make it 'Harvey'. This matches what the user would input to find the Harvey record. So hopefully that explanation identifies the problem and explains how to fix it, as for the code I would encourage you to try fixing it yourself as you know what the issue is now but here is the code if you don't feel like it or want to check what you have done: hastebin.com/qonoxewehe.cs I hope this helps and if you have any further questions or need any more help just ask. I am always happy to help.
I am still a student and learning. Haha. This is part of my final requirement to one of my subject. My professor told us to use 2 field separator/structures. So I used fixed-length and a delimiter "|". So, there are 15 characters including the spaces in every field except for the 'age' which has only 5 characters. And yeah finally, my problem has been solved. Thank you!!!!!!!!!!!!
@@PluckMyStrings Here is the tutorial: ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-KI_a39BeCLU.html I am also in the process of remaking older tutorials for September 2021 if you would be interested in seeing another approach.
You can save it in your Java project's folder or even better create a subfolder to store text files for your project (to keep things organised). If you store your textfile "Tutorial.txt" in your Java project folder you just need to refer to the file as "Tutorial.txt" in your code. If you save our file in a subfolder it would be something like "\Tutorial.txt". I sadly can't say where your Java project folders are located as you may not use the default location for them and I can't remember the default location, but they won't be hard to find.
Can you guide me on my TIC TAC TOE PROJECT? We need to make an AI to play TIC tac toe using a training set from the internet. I have 0 knowledge here. My professor haven't taught anything. All he did said is use an id3. Thank you! you are my only hope.
I would happily offer some guidance, but it is slightly concerning that your professor hasn't taught you anything to assist you. Just ask a question and I shall happily respond as best I can, I am however somewhat busy right now but I shall respond as quickly as I can.
Hi, I'm using eclipse, and copy the file to the project folder doesn't seems to work, neither see any option to import a text file to my project, any tips on how to copy or set the file path on the string? I tried that but the \ character can't be used on a string except for a escape sequence