Home > Mobile >  How do I print out information with CSV spreadsheet?
How do I print out information with CSV spreadsheet?

Time:07-29

I'm currently working on a project with CSV. In the task, I am supposed to type a country name in the tester method, and when I call the tester method, it will print the information of the country. For example, "Germany Chemical 32000." However, no matter what country name I put(I'm sure that country exists in the spreadsheet), it always prints out "NOT FOUND," which I don't understand how. I'm guessing the problem is in the if statement of the countryInfo method. However, I can't find the problem probably due to a lack of domain knowledge, so I hope someone can inform me or give me a hint.

public void tester(){
    FileResource fr = new FileResource();
    CSVParser parser = fr.getCSVParser();
    
    String GermanyInfo = countryInfo(parser,"Peru");
    System.out.println(GermanyInfo);
}

public String countryInfo(CSVParser parser, String country){
    String countryInfo = " ";
    for (CSVRecord record : parser){
        String nation = record.get("Country");
        if (nation.contains(country)){
            String countryExport = record.get("Exports");
            String exportValue = record.get("Value (dollars)");
            countryInfo = country   ": "   countryExport   " "   exportValue;
        }else{
        countryInfo = "NOT FOUND";
        }
    }
    return countryInfo;
}

Debug Process: Hey guys, after more testing and trying, I found out the problem is really part of the if statement. My for-each loop is running through the parser, one row at a time. The way I have this written, my if statement is checking to see whether that row contains any matching country name in the Country column, but once it finds it, it just keeps going and doesn't stop because I haven't told it to do so. It would find Germany but then move on to the next rows and bypass it until the end of the file, where it will return "not found." In order for me to fix this, I need to have a return statement following the exportValues = record.get line instead of the end of my method, OR simply type in a line that says "break;" after the money line, which will end the loop and then go to the return statement at the bottom.

CodePudding user response:

If you're sure that the country search in your loop works fine, just add the return statement in the right place. I would suggest to change your method like this:

    public String countryInfo(CSVParser parser, String country) {
    for (CSVRecord record : parser) {
        String nation = record.get("Country");
        if (nation.contains(country)) {
            String countryExport = record.get("Exports");
            String exportValue = record.get("Value (dollars)");
            return country   ": "   countryExport   " "   exportValue;
        }
    }
    return "NOT FOUND";
}

In this case - as soon as the country is found - the method will return information about it. If no country is found - the method will return String "NOT FOUND"

  • Related