가장 싼 신발을 표시하는 프로그램입니다. 데이터는 텍스트 파일에서 가져온 것입니다. 색상, 제조업체 및 구두 유형을 입력하면 다음과 같은 오류 메시지가 나타납니다.코드의 런타임 오류 - "NumberFormatException"
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();
}
}
'if (priceParsed <= price)'가 절대로 맞지 않을 가능성이 있습니까? 이 경우 귀하는 그러한 오류를 받게됩니다. 당신은 그것을 초기화 할 수 있습니다. comData [i]가 결코 null이 아니므로'system.out.printf'는 조건이 참인지를 알려줍니다. – saikumarm
if 문 중 하나가 실패하면 해당 변수에 값을 제공하지 않고 인쇄 할 줄을 찾으면 null이됩니다. 그것들을 null로 시작하는 대신에'string finalVendor = "";'를 수행하면됩니다. – csmckelvey
토큰 화 후에 배열 내부에있는 내용을 확인 했습니까? –