gpt4 book ai didi

java - 代码中的运行时错误 - "NumberFormatException"

转载 作者:行者123 更新时间:2023-12-01 12:08:21 25 4
gpt4 key购买 nike

这是一个展示最便宜一双鞋子的程序。数据来自文本文件。输入鞋子的颜色、制造商和类型后,我收到以下错误消息:

java.lang.NumberFormatException: For input string: "Price"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at Boots.main(Boots.java:47)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:272)

我已经束手无策了。请帮忙!

这是代码。

import java.util.Scanner; 
import java.io.*;
import java.util.StringTokenizer;

public class Boots
{
public static void main(String[] args) throws IOException
{
Scanner keyboard = new Scanner(System.in);

String manufact, color, type;

System.out.print("Enter the boot manufacturer.");
manufact = keyboard.nextLine();
manufact = manufact.replaceAll("\\s+", "");
System.out.print("Enter the type of boot.");
type = keyboard.nextLine();
type = type.replaceAll("\\s+", "");
System.out.print("Enter the boot color.");
color = keyboard.nextLine();
color = color.replaceAll("\\s+", "");

/* Open boots.txt.*/

File inputFile = new File("Boots.txt");
if (!inputFile.exists()) System.out.print("I'm sorry, we are experiencing technical difficulties. Please try again later.");
Scanner warehouse = new Scanner(inputFile);

String[] compData = new String[25];

int price = 0; // cheapest pair of shoes
String finalVendor = "";
String finalType = "";
String finalManufact = "";
String finalColor = ""; //information of cheapest pair of shoes

/* Read through file, and save each line as a compData array, each word tokenized as a compData subarray.*/

do {
String nLine = warehouse.nextLine();
StringTokenizer tokens = new StringTokenizer(nLine);
for (int i = 0; i<5; i++)
{
compData[i] = tokens.nextToken();
}

int priceParsed = Integer.parseInt(compData[4]);
if (compData[1].equalsIgnoreCase(manufact))
if (compData[3].equalsIgnoreCase(color))
if (compData[2].equalsIgnoreCase(type))
if (priceParsed <= price)
{
price = priceParsed;
finalVendor = compData[0]; finalManufact = compData[1]; finalType = compData[2]; finalColor=compData[3];
}
} while (warehouse.hasNext());

if (price<=0)
{System.out.printf("Meet your new shoes!\nThey are %s %s by %s. You can get them for $%.2s at %s.", finalColor,
finalType, finalVendor, finalManufact, price);} else {System.out.print("We have no such merchandise at this time.");};

warehouse.close();
keyboard.close();
}
}

最佳答案

您所要做的就是添加 = "";到字符串变量声明的末尾。

String finalVendor = ""; 
String finalType = "";
String finalManufact = "";
String finalColor = ""; //information of cheapest pair of shoes

这将消除您的错误。如果您在使用 parseInt 时遇到问题,您需要检查您正在查看的数组中的内容,并确保您从分词器中获取了正确的数据。

关于java - 代码中的运行时错误 - "NumberFormatException",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27415199/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com