这是我的司机课

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




        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


首先是这里的浇头。您正在尝试将其读取为尺寸,这给了您错误

关于java - 我的代码有什么问题;构造函数和循环?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27472750/

10-09 07:00
查看更多