1

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.

2 Answers 2

1

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;
      }
   }
}
Sign up to request clarification or add additional context in comments.

3 Comments

btw, what if theres a space in between some of the commas like this: 132435465, 3578,McIvar, Alan 543247531,2854,Jones, Alan
Yeah I saw that in my original running but chose to ignore it which probably wasn't helpful to you. You can resolve this by changing the Person constructor to use the trim() method. I will edit my response to include it.
I updated the example. See in the Person constructor how it calls this.studentNumber = csvValues[0].trim() and so on? That will remove the leading and trailing whitespace from each comma-separated string from the file. I tested it and it worked well.
0

Here an idea how you could achieve this: Create a "student" class:

class student {
  private String lastname;
  private String firstname;
  private String studentId;
  private String pin;

  // Getter and Setter methods

  public static createNewStudent(String line) {
    // split here the line and save the fields in the member variables
  }

  public boolean checkPinCode(String pin) {
    return this.pin.equals(pin);
  }
}

In your loop you can create student objects and add them to a Hashtable. The key is the studentId and the value is the student object. You can retrieve a student object from the hashtable with the entered key and check if the pin passes.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.