简体   繁体   中英

How do I read multiple file lines and store them into different Objects Using Scanner

I have a file that I am trying to read multiple lines from. But depending if it is a realtor line or a property line, it will store certain things into the line values array. I cant figure out the proper loop to read the lines properly Here is my code for reading the lines

    while (fileScanner.hasNextLine()) {
        String oneLine;
        String[] lineValues = null;
        oneLine = fileScanner.nextLine();

        lineValues = oneLine.split(",");

        if (lineValues[0].contains("REALTOR")) {
            if (lineValues[1].contains("ADD")) {                    
                processRealtorAddition(lineValues);                    
            } else if (lineValues[1].contains("DEL")) {
                realtorDeletion(lineValues);
            }
            else
                break;

        }  if (lineValues[0].contains("PROPERTY")) {
            System.out.println("fsdfsdfsdfdsfdsfsdfsdfds");                
            if (lineValues[1].contains("ADD")) {                    
                processPropertyAddition(lineValues);
                break;
            } else if (lineValues[1].contains("DEL")) {
                propertyDeletion(lineValues);
            }
        }             
    }
}

But when I run that I get this:

The Realtor Object with a license number of MN4564567 has been added fsdfsdfsdfdsfdsfsdfsdfds Realtor Log:

Property Log: Property{mlsNumber=4455667, licenseNumber=MN4564567, streetAdress=4455 This Circle, city=Denver, state=CO, zipCode=80333, bedrooms=1, bathrooms=1.0, sold=false, askingPrice=344555.0} All properties are correct BUILD SUCCESSFUL (total time: 0 seconds)

This is the file I'm trying to read:

REALTOR,ADD,MN4564567,Carla,Combs,444-555-6666,0.014 PROPERTY,ADD,4455667,MN4564567,4455 This Circle,Denver,CO,80333,1,1,N,344555 REALTOR,ADD,RR6655443,Jerry,Smith,555-444-3333,0.013 PROPERTY,ADD,23456789,RR6655443,888 Terry Lane,Longmont,CO,80503,3,2,N,222222 REALTOR,ADD,AB1234567,Matthew,Munez,123-456-7890,0.012 PROPERTY,ADD,1234567,AB1234567,1234 Which Way,Somewhere,CO,82222,3,3,Y,222222 PROPERTY,ADD,2234567,AB1234567,345 Main St,Fort Collins,CO,81333,4,3.5,N,222333 REALTOR,DEL,MN4564567 REALTOR,ADD,XY98765432,Alex,Yung,999-888-7777,0.013 PROPERTY,ADD,9998888,XY98765432,111 Main St,Cheyenne,WY,82222,1,1

,N,199888

I just need it to read the first line get all the right info, then start the while loop all over again then read the second line. Thanks!

This is the static method to process the addition

static void processPropertyAddition(String lineValues[]) {
    Property property = new Property(lineValues);

    boolean value1 = property.verifyMlsNumber();
    boolean value2 = property.verifyState();
    boolean value3 = property.verifyZipCode();

    if (value1 == false) {
        System.out.println("ERROR: invalid MLS number: "
                + property.mlsNumber + "\n");
    } else if (value2 == false) {
        System.out.println("ERROR: Invalid State: "
                + property.state + "\n");
    } else if (value3 == false) {
        System.out.println("ERROR: Invalid zip code: "
                + property.zipCode + "\n");
    }

    boolean value4 = realtorLogImpl.isLicenseUnique(property.getLicenseNumber());

    boolean value5 = propertyLogImpl.isMlsUnique(property.getMlsNumber());
    if (value4 == false && value5 == false) {
        propertyList.add(property);
    } else if (value5 == true && value4 == true) {
        propertyList.add(property);
    } else if (value4 == false && value5 == true) {
        propertyList.add(property);

        System.out.println("The Property with Realtor license number "
                + property.getLicenseNumber() + " and with MLS number"
                + property.getMlsNumber() + " has been added");
    } else if (value4 == true && value5 == false) {
        System.err.println(" Property will not be added due to "
                + "a Realtor license or a MLS number that is not "
                + "unique");
    }
}

This is the method of the PropertyLogImpl to add a property to the property linked list

  public boolean add(Property property) {
    return propertyList.add(property);
}

I have to have a method to add a property, I feel that this method may be incorrect and may be the cause of it

Instead of this

 }  if (lineValues[0].contains("PROPERTY")) {

you probably wanted to write

 } else if (lineValues[0].contains("PROPERTY")) {

Also whenever you use contains you probaly wanted to use equals instead.

And as already mentioned in comments your usage of break statements is suspicious.

If you run into an infinite loop, there is nothing in the code shown so far that would cause this. It is possible the infinite loop comes from here:

realtorLogImpl.isLicenseUnique(property.getLicenseNumber());

It should be really easy to find the cause with a debugger (hint, hint, ...)

break statement breaks out of the for/while loop.

If you remove the break statement after the processPropertyAddition method call, that should fix your bug.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM