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

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变量声明的末尾。

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


这将摆脱您的错误。
如果您对parseInt遇到问题,则需要检查正在查看的数组中的内容,并确保从令牌生成器中获取正确的数据。

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

10-11 22:32
查看更多