这是一个显示最便宜的鞋子的程序。数据来自文本文件。在我输入鞋子的颜色,制造商和类型后,我收到以下错误消息:
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();
}
}
答案 0 :(得分:1)
你所要做的就是添加=&#34;&#34 ;;到String变量声明的末尾。
String finalVendor = "";
String finalType = "";
String finalManufact = "";
String finalColor = ""; //information of cheapest pair of shoes
这将消除您的错误。 如果你遇到parseInt问题,你需要检查你正在查看的数组中的内容,并确保从令牌器中获取正确的数据。