简体   繁体   中英

Runtime Error in code - “NumberFormatException”

This is a program to display the cheapest pair of shoes. The data is from a text file. After I put in the color, manufacturer, and type of shoe, I get the following error message:

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)

I am at my wits' end. Please help!

Here's the code.

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(); 
  }
  }

All you have to do is add = ""; to the end of the String variable declaration.

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

this will get rid of your errors. If you are having a problem with parseInt you need to check what is in the array you are looking at and make sure you are getting the right data from your tokenizer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM