Why doesn't my program return the file line I searched for inside a txt file?

Viewed 27

Hi this has me stumped I'm trying to search my file for a selected number and then display that number with the rest of the line, the problem is that my program seems to return the first line of contents when I search for it, but not second or third lines when I type in to search for them

public static String searchProject(String searchTerm, String filepath) {
        String findString = "";
        boolean found = false;
        String projectNumber = "";
        String projectName = "";
        String typeProject = "";
        String address = "";
        String erf = "";
        String fee = "";
        String paid = "";
        String deadline = "";
        String finalised = "";
        String architectName = "";
        String architectTel = "";
        String architectEmail = "";
        String architectAddress = "";
        String contractorName = "";
        String contractorTel = "";
        String contractorEmail = "";
        String contractorAddress = "";
        String customerName = "";
        String customerTel = "";
        String customerEmail = "";
        String customerAddress = "";

        try {
            Scanner userInput = new Scanner(System.in);
            userInput = new Scanner(new File(filepath));
            userInput.useDelimiter("[,\n]");

            while (userInput.hasNext() && !found) {
                projectNumber = userInput.next();
                projectName = userInput.next();
                typeProject = userInput.next();
                address = userInput.next();
                erf = userInput.next();
                fee = userInput.next();
                paid = userInput.next();
                deadline = userInput.next();
                finalised = userInput.next();
                architectName = userInput.next();
                architectTel = userInput.next();
                architectEmail = userInput.next();
                architectAddress = userInput.next();
                contractorName = userInput.next();
                contractorTel = userInput.next();
                contractorEmail = userInput.next();
                contractorAddress = userInput.next();
                customerName = userInput.next();
                customerTel = userInput.next();
                customerEmail = userInput.next();
                customerAddress = userInput.next();

                if (projectNumber.equals(searchTerm)) {
                    findString = "\nProject Number: " + projectNumber + "\nProject Name: " + projectName + "\nType Project: " + typeProject + "\nAddress: "
                            + address + "\nERF number: " + erf + "\nTotal fee: R" + fee + "\nPaid to date: R" + paid + "\nProject Deadline: "
                            + deadline + "\nIs the project finalised: " + finalised + "\nArchitect Name: " + architectName + "\nArchitect Telephone: " + architectTel + "\nArchitect Email: "
                            + architectEmail + "\nArchitect Address: " + architectAddress + "\nContractor Name: " + contractorName + "\nContractor Telephone: " + contractorTel + "\nContractor Email: "
                            + contractorEmail + "\nContractor Address: " + contractorAddress + "\nCustomer Name: " + customerName + "\nCustomer Telephone: " + customerTel + "\nCustomer Email: "
                            + customerEmail + "\nCustomer Address: " + customerAddress;
                    System.out.println(findString);
                }
            }
        } catch (Exception e) {

        }
        return findString;
    }

my txt file contents

1,House mack,house,123 mack road,155,100000,40000,2022-09-14,no,Bill,123456,bill@gmail.com,12 Bill road,Jack,456789,jack@gmail.com,23 Jack road,John,789632,john@gmail.com,34 John road,
2,House John,house,123 John road,183,160000,50000,2022-09-10,yes,Bill,123456,bill@gmail.com,12 Bill road,Jack,456789,jack@gmail.com,23 Jack road,John,789632,john@gmail.com,34 John road,
3,House bill,house,123 bill road,193,160000,50000,2022-09-10,yes,Bill,123456,bill@gmail.com,12 Bill road,Jack,456789,jack@gmail.com,23 Jack road,John,789632,john@gmail.com,34 John road,

the output I am looking for

Please enter the project number to find the task: 1

Project Number: 1
Project Name: House mack
Type Project: house
Address: 123 mack road
ERF number: 155
Total fee: R100000
Paid to date: R40000
Project Deadline: 2022-09-14
Is the project finalised: no
Architect Name: Bill
Architect Telephone: 123456
Architect Email: bill@gmail.com
Architect Address: 12 Bill road
Contractor Name: Jack
Contractor Telephone: 456789
Contractor Email: jack@gmail.com
Contractor Address: 23 Jack road
Customer Name: John
Customer Telephone: 789632
Customer Email: john@gmail.com
Customer Address: 34 John road

when I type in 2 or 3 to find the project my program displays nothing where is the problem and how can I fix it?

1 Answers

The problem is that your text file contains one column more than your parsing code expects (it contains a , after the last data field which makes it a total of 22 columns of which you read only 21).

Due to this mismatch, the reading of the "second" project starts with the empty last field of the first line instead of the project number in the second line.

To check this you could write out the projectNumber just after you have read it:

            projectNumber = userInput.next();
            System.out.println("projectNumber=" + projectNumber);

One way to fix this would be to add an extra userInput.nextLine(); after you have read all the fields of a row:

            customerAddress = userInput.next();
            userInput.nextLine();

But IMHO it would be better to rewrite your code to read line by line and split each line separately into the columns. By reading the whole file as a sequence of columns you ignore the structure that your data has - and that structure could help you find mistakes in the data and / or in the code.


Additional note: why do you create a new Scanner(System.in); if you never use it? I'm talking about those two lines:

    Scanner userInput = new Scanner(System.in);
    userInput = new Scanner(new File(filepath));

You should replace those two lines with a single line

    Scanner userInput = new Scanner(new File(filepath));
Related