首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当我将文本文件附加到代码中时,出现错误消息

当我将文本文件附加到代码中时,出现错误消息
EN

Stack Overflow用户
提问于 2022-05-15 06:02:21
回答 1查看 25关注 0票数 2

我需要创建程序,为用户提供按乡镇名称按字母顺序或按乡镇平方英里按大小顺序显示报告的能力。

但是,当我使用代码运行文本文件时,会收到错误消息。

如果没有文本文件,我的代码就能工作,但是当我尝试将文本文件与代码一起使用时,我会得到这个错误。

代码语言:javascript
复制
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)

我的代码:

代码语言:javascript
复制
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;
    }

}

我试图附加的文本文件

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-05-15 06:22:33

java.lang.NumberFormatException: For input string: ""意味着您正在尝试解析一个不是有效数字的数字。在这种情况下,您正在尝试将空字符串转换为整数。

您正遇到此问题,因为填充、townSize数字在文本文件中有一个或多个空格。仅仅使用split(" ")是不够的,您必须使用split("\\s+)在一个或多个空格上拆分。

布鲁维尔的数字有两个空格,而靛蓝村的数字只有一个空格。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72245877

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档