Separate strings and associate them with the text file in java

advertisements

I have a text file which contains a student number (9 digits), pin (4 digits), first name, last name. Which looks like this:

456864324,4965,Eves,Dalton

457642455,2164,Jagger,Michael

132435465, 3578,McIvar, Alan

543247531,2854,Jones, Alan

The student enters its student number and then pin. The program matches his input to the text file and checks if it matches or not.

So far I've separated the text line by line and stored it into an ArrayList and then thought about splitting it with ",". I've also thought about using Maps but cannot figure out how I will store the names with it as well.

        String studentdb = sn_field.getText(); //get student number from input
        String pindb = pin_field.getText(); //get pin from input

         try {
                File f = new File("file name");
                Scanner sc = new Scanner(f);

                ArrayList<String> number= new ArrayList<String>();
                ArrayList<String> pswd = new ArrayList<String>();

                while(sc.hasNextLine()){
                    String line = sc.nextLine();
                   // = line.split("\n");

                    String sn = line;
                    people.add(sn);

                }

            //if(people.contains(studentdb)){

            //System.out.println("pass");}

            } catch (FileNotFoundException f) {         

                System.out.print("file not found");

            }

All in all if the student number and pin both are wrong, it should give an error, if both are correct and match, it passes. Any help would be appreciated as I'm just a beginner at Java.


I was able to process your file with the following example. Thanks for the problem as it provided a fun playground for some of the new features in Java 8 that I'm still getting familiar with . . .

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Optional;
import java.util.Scanner;

public class StudentInformationMatcher
{
   private static final Path FILE_PATH = Paths.get("C:\\projects\\playground\\src\\main\\resources\\studentinfo.txt");

   public static void main(String[] args) throws IOException
   {
      Scanner scanner = new Scanner(System.in);

      System.out.print("Please enter your student number: ");
      String studentNumber = scanner.next();

      System.out.print("Please enter your pin: ");
      String pin = scanner.next();

      Optional<Person> matchingPersonRecord =
            Files.lines(FILE_PATH)
                  .map(line -> line.split(","))
                  .map(csvValues -> new Person(csvValues))
                  .filter(person -> person.getStudentNumber().equals(studentNumber) && person.getPin().equals(pin))
                  .findFirst();

      if (matchingPersonRecord.isPresent())
      {
         Person matchingPerson = matchingPersonRecord.get();
         System.out.println("Hello " + matchingPerson.getFirstName() + " " + matchingPerson.getLastName());
      }
      else
      {
         System.out.println("No matching record found");
      }
   }

   private static class Person
   {
      private final String studentNumber;
      private final String pin;
      private final String lastName;
      private final String firstName;

      private Person(String[] csvValues)
      {
         this.studentNumber = csvValues[0].trim();
         this.pin           = csvValues[1].trim();
         this.lastName      = csvValues[2].trim();
         this.firstName     = csvValues[3].trim();
      }

      private String getStudentNumber()
      {
         return studentNumber;
      }

      private String getPin()
      {
         return pin;
      }

      private String getLastName()
      {
         return lastName;
      }

      private String getFirstName()
      {
         return firstName;
      }
   }
}