复制
收藏
提问
简洁

读取文件时发生错误:For input string: "编号 名称 价格 数量"以下是全部代码package com.qf.b_homework; import java.io.*; import java.util.Scanner; class Fruits { private int number; private String name; private double price; private int amount; public int getNumber() { return number; } public void setNumber(int number) { this.number = number; } public String getName() { return name; } public void setName(String name) { this.name = name; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public int getAmount() { return amount; } public void setAmount(int amount) { this.amount = amount; } public Fruits() { } public Fruits(int number, String name, double price, int amount) { this.number = number; this.name = name; this.price = price; this.amount = amount; } } public class FruitsManageSystem { static Scanner sc = new Scanner(System.in); public static void main(String[] args) throws IOException { System.out.println("欢迎进入水果管理系统"); while (true) { System.out.println("请选择角色:1.顾客 2.管理员 3.退出"); int c = sc.nextInt(); switch (c) { case 1: customerPage(); break; case 2: adminPage(); break; case 3: System.out.println("感谢使用,已退出!"); return; default: System.out.println("输入错误,请重新输入!!"); break; } } } private static void customerPage() { } public static void adminPage() throws IOException { System.out.println("请输入管理员账号:"); String adminUsername = sc.next(); System.out.println("请输入管理员密码:"); String adminPassword = sc.next(); if (adminUsername.equals("admin") && adminPassword.equals("123123")) { System.out.println("登录成功"); while (true) { System.out.println("请选择操作:"); System.out.println("1. 查看水果信息"); System.out.println("2. 添加新水果"); System.out.println("3. 修改水果信息"); System.out.println("4. 删除水果"); System.out.println("5. 返回上一级菜单"); int choice = sc.nextInt(); switch (choice) { case 1: displayFruits(); break; case 2: addFruit(); break; case 3: modifyFruit(); break; case 4: deleteFruit(); break; case 5: return; // 返回上一级菜单 default: System.out.println("无效的选择,请重新输入"); break; } } } else { System.out.println("登录失败,账号或密码错误"); } } public static void displayFruits() throws IOException { FileInputStream fis = new FileInputStream("D:/aaa/fruits.txt"); BufferedInputStream bis = new BufferedInputStream(fis); byte[] buf = new byte[4 * 1024]; int len; while ((len = bis.read(buf)) != -1) { System.out.println(new String(buf, 0, len)); } bis.close(); fis.close(); } public static void addFruit() throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.println("请输入要添加的水果信息:"); System.out.println("编号:"); int number = Integer.parseInt(br.readLine()); System.out.println("名称:"); String name = br.readLine(); System.out.println("单价:"); double price = Double.parseDouble(br.readLine()); System.out.println("数量:"); int amount = Integer.parseInt(br.readLine()); Fruits newFruit = new Fruits(number, name, price, amount); if (isFruitNumberExist(number)) { System.out.println("编号已存在,添加失败"); return; } // 将新水果信息写入文件 try (BufferedWriter bw = new BufferedWriter(new FileWriter("D:/aaa/fruits.txt", true))) { bw.newLine();

ea6fd21bf
1个月前
全网
文档
学术
百科
知识库
时间不限
自动
回答简洁模式
深入研究一些

看起来你遇到了一个读取文件时的错误,错误信息是 "For input string: '编号 价格 数量'"。这个错误通常发生在尝试将一个非数字的字符串转换为数字时。在你的代码中,这个错误可能是由于读取文件时,文件的第一行是标题行,包含了非数字的字符串,而你的代码尝试将这些字符串转换为数字。

要解决这个问题,你可以在读取文件时跳过第一行,或者在将字符串转换为数字之前检查字符串是否只包含数字。下面是一些可能的解决方案:

  1. 跳过第一行:在 displayFruits 方法中,你可以在读取文件之前先读取并丢弃第一行。
public static void displayFruits() throws IOException {
    FileInputStream fis = new FileInputStream("D:/aaa/fruits.txt");
    BufferedInputStream bis = new BufferedInputStream(fis);
    BufferedReader br = new BufferedReader(new InputStreamReader(bis));
    String line;
    // 跳过第一行
    br.readLine();
    while ((line = br.readLine()) != null) {
        System.out.println(line);
    }
    br.close();
    fis.close();
}
  1. 检查字符串是否只包含数字:在将字符串转换为数字之前,你可以添加一个检查,确保字符串只包含数字。
public static boolean isNumeric(String str) {
    try {
        double d = Double.parseDouble(str);
    } catch (NumberFormatException | NullPointerException e) {
        return false;
    }
    return true;
}

// 在 addFruit 方法中使用 isNumeric 方法
double price = isNumeric(br.readLine()) ? Double.parseDouble(br.readLine()) : 0;
  1. 使用正则表达式:如果你想要更精确地检查字符串是否只包含数字和可能的小数点,你可以使用正则表达式。
public static boolean isNumeric(String str) {
    return str.matches("-?\\d+(\\.\\d+)?");  // 匹配整数或小数
}

// 使用 isNumeric 方法同上

请注意,这些解决方案假设你的文件格式是固定的,并且每行都包含四个字段:编号、名称、价格和数量。如果文件格式可能会变化,你可能需要更复杂的逻辑来处理不同的行格式。

你觉得结果怎么样?
Java读取文件时的常见错误
Java文件读取异常处理
Java中BufferedInputStream的使用
Java中FileInputStream的使用
Java中IOException的处理方法
Java中Scanner类的nextInt()方法使用 Java文件写入常见问题

以上内容由AI搜集生成,仅供参考

在线客服