Java 什么';我的代码有问题;构造函数和循环?

Java 什么';我的代码有问题;构造函数和循环?,java,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 = ""; Str

这是我的驾驶课

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

必须更改订单

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

这是因为您的文件首先包含浇头的名称。因此,您必须首先阅读
infiletoping

更改后的输出将为

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

首先是这里的浇头。您试图将其读取为一个大小,这会给您带来错误

看起来您在输入信息之前,它会询问您。这真的是你想要的吗?你在被要求之前键入了“sloppyJoe 15.30”。对不起,我刚刚更新了它。
在PizzaMatch.main(PizzaMatch.java:42)
PizzaMatch?inFileSize=infiereReader.nextInt()中哪一行是#42;能给我们pizza.txt文件吗?我想看看它怎么样format@Newbie一定要接受这个答案是正确的。否则其他人会认为你的问题仍然没有得到回答。请参阅此链接-
cheese 12 12.50