#City Database from csv

1 messages ยท Page 1 of 1 (latest)

boreal skiff
#

Im getting some errors i have tried to fix them please have a look.

sleek tartanBOT
#

<@&987246399047479336> please have a look, thanks.

#

Here is an AI assisted attempt to answer your question ๐Ÿค–. Maybe it helps! In any case, a human is on the way ๐Ÿ‘. To continue talking to the AI, you can use </chatgpt:1108714622413963314>.

#
TJ-Bot
Im getting some errors i have tried to fix them please have a look.

It would be helpful if you could provide more specific details about the errors you are encountering in order for us to assist you better. However, based on the information provided, here are some common issues and potential solutions you can try:

  1. Make sure that you have correctly imported the necessary libraries for reading CSV files in Java, such as OpenCSV or Apache Commons CSV.

  2. Check that the file path to your CSV file is correct and accessible from your Java program. You can use absolute or relative paths depending on your setup.

  3. Verify that the structure of your CSV file matches the way you are trying to read its data in your Java code. Ensure that each line in the CSV corresponds to the expected format.

  4. Handle exceptions properly when reading from the CSV file, such as FileNotFoundException or IOException, to prevent runtime errors.

  5. Consider using a debugger or print statements to trace where exactly the errors occur in your code and provide more specific information for troubleshooting.

If you continue to encounter issues, please share any error messages or code snippets related to reading from the CSV file so we can provide more targeted assistance.

boreal skiff
sleek tartanBOT
boreal skiff
#

the errors

radiant valley
#

You can wrap it in a try/catch to check the invalid value.

boreal skiff
vestal acorn
#

Does the CSV have a heade row pehaps?

boreal skiff
#

yes it does

#

thats where my thoughts went to

#
private void readCitiesFromFile(String fileName) throws IOException {
    int lineNumber = 0;
    try (BufferedReader br = new BufferedReader(new FileReader(fileName))) {
        String line;
        while ((line = br.readLine()) != null) {
            lineNumber++;
            if (lineNumber == 1) {
                continue;
            }
            String[] values = line.split(",");
            String name = values[0];
            String country = values[1];
            boolean isCapital = Boolean.parseBoolean(values[2]);
            int population = Integer.parseInt(values[3]);

            City city = new City(name, country, isCapital, population);
            citiesByCountry.computeIfAbsent(country, k -> new ArrayList<>()).add(city);
        }
    }
}
sleek tartanBOT
# boreal skiff ```java private void readCitiesFromFile(String fileName) throws IOException { ...

Detected code, here are some useful tools:

Formatted code
private void readCitiesFromFile(String fileName) throws IOException {
  int lineNumber = 0;
  try (BufferedReader br = new BufferedReader(new FileReader(fileName))) {
    String line;
    while ((line = br.readLine()) != null ) {
      lineNumber++;
      if (lineNumber == 1) {
        continue ;
      }
      String[] values = line.split(",");
      String name = values[0] ;
      String country = values[1] ;
      boolean isCapital = Boolean.parseBoolean(values[2] );
      int population = Integer.parseInt(values[3] );
      City city = new City(name, country, isCapital, population);
      citiesByCountry.computeIfAbsent(country, k -> new ArrayList<>()).add(city);
    }
  }
}
boreal skiff
#

fixed it