我的代码出了什么问题;构造函数和循环?

时间:2014-12-14 18:47:15

标签: java

这是我的驱动程序类

import java.io.File;
import java.io.IOException;
import java.util.Scanner;


public class PizzaMatch {

    @SuppressWarnings("resource")
    public static void main(String[] args) throws IOException {

        String answer = "";
        String yes = "Y";
        String no = "N";
        String topping = "";
        int size = 0;
        double cost = 0;
        int iteration = 0;
        int inFileSize = 0;
        double inFileCost = 0;
        String inFileTopping = "";

        Scanner reader = new Scanner(System.in);
        Scanner inFileReader = new Scanner(new File("C:/Users/Frosty Snow/Downloads/pizza.txt"));

        System.out.println("Would you like to enter a Pizza? (Y or N)");
        answer = reader.next();

        if(answer.equalsIgnoreCase(no)) {
            System.exit(1);
        }

        System.out.println("Input a Pizza topping, size, and cost:");
        topping = reader.next();
        size = reader.nextInt();
        cost = reader.nextDouble();

        Pizza Order = new Pizza (size, topping, cost);

        while(inFileReader.hasNextLine())
        {
            iteration ++;
            inFileSize = inFileReader.nextInt();
            inFileTopping = inFileReader.next();
            inFileCost = inFileReader.nextDouble();

            Pizza inFileOrder = new Pizza(inFileSize, inFileTopping, inFileCost);

            if(Order.equals(inFileOrder))
            {
                System.out.println("That pizza is # " + iteration + " in the file.");
                break;
            }
        }

    }

}

这是我的资源类

public class Pizza {

   private int size;
   private String topping;
   private double cost;


   public Pizza()
   {
      size = 10;
      topping = "cheese";
      cost = 9.00;
   }

   public Pizza(int s, String t, double c)
   {
      size = s;
      topping = t;
      cost = c;   }

   public int getSize() {
      return size;
   }

   public void setSize(int s) {
      s = size;
   }

   public String getTopping(){
      return topping;
   }

   public void setTopping(String t){
      topping = t;
   }

   public void setCost(double c) {
      cost = c;
   }   

   public double getCost(double c){
      return cost;
   }
   public boolean equals(Object obj)
   {
      if(!(obj instanceof Pizza))
         throw new IllegalArgumentException();

      Pizza temp = (Pizza) obj;

      if (this.size == temp.size && this.topping.equals(temp.topping) && this.cost == temp.cost)
         return true;
      else
         return false;
   }

   public String toString()
   {
      return String.format("%d inch %s pizza will cost $%,.2f\n", size, topping, cost);

   }
}

这些是我的错误

Would you like to enter a Pizza? (Y or N)
sloppyJoe 15 15.30
Input a Pizza topping, size, and cost:
Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Unknown Source)
    at java.util.Scanner.next(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at PizzaMatch.main(PizzaMatch.java:34)

Would you like to enter a Pizza? (Y or N)
Y
Input a Pizza topping, size, and cost:
sloppyJoe 15 15.30
Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Unknown Source)
    at java.util.Scanner.next(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at PizzaMatch.main(PizzaMatch.java:42)

我认为这个问题与while语句有关,请阅读下一行。还有什么我可以放在它的位置吗?如果还有其他任何可能搞砸我的事情,请通知我。

pizza.txt

cheese 12 12.50
sausage 15 21.89
peppers 10 15.05
pineapple 40 33.00
pepperoni 11 10.22
olive 9 8.99
xcheese 13 9.66
supreme 13 15.22
mushroom 17 16.34
bbqchicken 14 20.13
pepperoni 10 11.70
sausage 12 11.89
peppers 12 15.05
pineapple 14 13.50
squirrel 12 12.24
pickle 12 7.99
hawaiian 10 13.00
pepperoni 11 10.22
meat 13 9.66
sloppyJoe 15 15.30
dessert 14 17.60
bigBubba 16 25.00
steakLovers 23 30.77
bison 10 11.70
secretMeat 12 11.99
peppers 12 14.00
pineNeedle 13 13.50
sweetTart 12 12.24
tofu 7 8.99

1 个答案:

答案 0 :(得分:2)

必须更改订购

        inFileSize = inFileReader.nextInt();
        inFileTopping = inFileReader.next();
        inFileCost = inFileReader.nextDouble();

        inFileTopping = inFileReader.next();
        inFileSize = inFileReader.nextInt();
        inFileCost = inFileReader.nextDouble();

这是因为您的文件首先包含浇头的名称。所以你必须先阅读inFileTopping

更改后的输出将为

Would you like to enter a Pizza? (Y or N)
Y       
Input a Pizza topping, size, and cost:
cheese 12 12.50
That pizza is # 1 in the file.

了解更多详情

文件的第一行

cheese 12 12.50

这里首先是顶部。您试图将其读作大小,这会给您错误