Error message when I attach a textfile to my code

I need to create program to offer the user the ability to produce the report as show about in alphabetical order by
township name or in size order by township square mile.

But I get error message when I run the text file with the code.

Without the text file, my code works, but when I try to use the text file with the code, I get this error.

Exception in thread "main" java.lang.NumberFormatException: For input string: ""
   at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:67)
   at java.base/java.lang.Integer.parseInt(Integer.java:678)
   at java.base/java.lang.Integer.parseInt(Integer.java:786)
   at Main.main(Mice.java:76)

My code:

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;


// create mice class

class Mice {

    private final double micepopulation;
    private final int sizetown;
    private final String town;


    // =-=-=-=-=-=-=-=-=--==-=


    public Mice(double population, int sizetown, String town) {

        this.town = town;
        this.micepopulation = population;
        this.sizetown = sizetown;
    }


// --==--=-=-=-=-=-=-=-=-=--=

    public String miceelseif() {
        if (micepopulation > 75) {
            return "Blue";
        } else if (micepopulation > 65) {
            return "Green";
        } else if (micepopulation > 50) {
            return "Yellow";
        } else if (micepopulation > 35) {
            return "Orange";
        } else {
            return "Red";
        }

        // -=-=-=-=-=-=-=-=-=-=-=-=-
    }
    public String getTOWN() {
        return town;
    }

    public String toString() {
        return String.format("%-25s%-20.2f%-20d%-20s", town, micepopulation, sizetown, miceelseif());
    }
}


public class Main {
    public static void main(String[] args) {
        int numRecords = getNumRecords();
        String[] township = new String[numRecords];
        double[] population = new double[numRecords];
        int[] townsize = new int[numRecords];

        try {
            Scanner fileScanner = new Scanner(new File("Micepopulation.txt"));
            int index = 0;
            while (fileScanner.hasNextLine()) {
                township[index] = fileScanner.nextLine();
                String[] popTwonSizeContents = fileScanner.nextLine().trim().split("");
                population[index] = Double.parseDouble(popTwonSizeContents[0]);
                townsize[index] = Integer.parseInt(popTwonSizeContents[1]);
                // increment the index
                index++;
            }
            fileScanner.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        Mice[] micePop = new Mice[numRecords];

        for (int index = 0; index < micePop.length; index++) {
            micePop[index] = new Mice(population[index], townsize[index], township[index]);
        }

        Scanner scanner = new Scanner(System.in);
        int choice;
        do {
            System.out.println("1: Mice by Town");
            System.out.println("2: Mice by size");
            System.out.println("3- Town name");
            System.out.println("0- Exit");
            System.out.print(" Please enter your choice: ");
            choice = scanner.nextInt();
            scanner.nextLine();

            switch (choice) {
                case 0 -> System.out.println("thank you, have a good day");
                case 1, 2 -> bytown(micePop);
                case 3 -> {
                    System.out.print("please enter town ");
                    String town = scanner.nextLine();
                    int foundIndex = townLookUp(micePop, town);
                    if (foundIndex == -1) {
                        System.out.println("no town");
                    } else {
                        System.out.printf("%-25s%-20s%-20s%-20s\n", "Town", "Mice Population", "Town Size",
                                "Alerts");
                        System.out.println(micePop[foundIndex].toString());
                    }
                }
                default -> System.out.println("Invalid choice");
            }
            System.out.println();
        } while (choice != 0);
        scanner.close();
    }

    // ==-=-=-===--==-=-=-=-=-=-=-=-=-=--==-





    private static void bytown(Mice[] micePop) {
        for (int index = 0; index < micePop.length; index++) {
            for (int innerIndex = 0; innerIndex < micePop.length - index - 1; innerIndex++) {
                if (micePop[innerIndex].getTOWN().compareTo(micePop[innerIndex + 1].getTOWN()) > 0) {
                    Mice temp = micePop[innerIndex];
                    micePop[innerIndex] = micePop[innerIndex + 1];
                    micePop[innerIndex + 1] = temp;
                }
            }
        }
        System.out.println("\nREPORT BY TOWN");
        printMicePopulation(micePop);
    }

    // -=-===-=-=-=--==-=-=--==-=-


    // =-=--=-==-=-=-=--==-=-=-=-=-=-=--==-=-=--
    private static int townLookUp(Mice[] micePop, String township) {
        for (int index = 0; index < micePop.length; index++) {
            if (micePop[index].getTOWN().equalsIgnoreCase(township)) {
                return index;
            }
        }
        return -1;
    }

    // -=-==-=-=-=--==--==-=-==-=-=-=-=-=-=--==-=-=-----=

    private static void printMicePopulation(Mice[] micePop) {
        System.out.printf("%-25s%-20s%-20s%-20s\n", "Town", "Mice Population", "Town Size", "Threat Alert");
        for (Mice mice : micePop) System.out.println(mice.toString());

    }

    // -=-==--==--==-==-=-=-===-=-=-=-=-=-=-=-

    public static int getNumRecords() {
        try {
            Scanner scanner = new Scanner(new File("Micepopulation.txt"));
            int numRecords = 0;

            while (scanner.hasNextLine()) {
                numRecords++;
                scanner.nextLine();
            }
            scanner.close();
            return numRecords / 2;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return 0;
    }

}

Text File I’m trying to attach

City of Red
70.81  2137
Boro of Orange
101.77 71
Yellow City
83.13  1034
Green Town
54.79  1819
Blueville
45.71  1514
Indigo Village
4.15 1442
Violeton 
119.27 2225
Redburg
7.46 977
Orange Park
16.72  133
Yellow Falls
94.5  4556
Green Haven
326.12 1105242
Blue City
44.69 1979
Indigo Township
113.56 365
Violet Point
35.27 4161

>Solution :

java.lang.NumberFormatException: For input string: "" means you are trying to parse a number that isn’t a valid number. In your case, you are trying to convert an empty string to an integer.

You are running into this problem because population and townSize numbers have one or more spaces in your text file. Just using split(" ") will not be sufficient, you will have to use split("\\s+) to split on one or more spaces.

Blueville’s numbers have two spaces, whereas Indigo Village’s numbers have one space.

Leave a Reply