0

我在多个程序中都遇到过这个问题,但我不记得上次我是如何解决的。在我体内的第二个 while 循环中,由于某种原因,第二个标记值从未被读入。我一直在尝试修复它一段时间,想我可能会看看是否有人有任何线索。

    import java.text.DecimalFormat; // imports the decimal format
    public class Car {

    // Makes three instance variables.
    private String make;
    private int year;
    private double price;
    // Makes the an object that formats doubles.
    public static DecimalFormat twoDecPl = new DecimalFormat("$0.00");

    // Constructor that assigns the instance variables
    // to the values that the user made.
    public Car(String carMake,int carYear, double carPrice)
    {
        make = carMake;
        year = carYear;
        price = carPrice;
    }
    // Retrieves variable make.
    public String getMake()
    {
        return make;
    }
    // Retrieves variable year.
    public int getYear()
    {
        return year;
    }
    // Retrieves variable price.
    public double getPrice()
    {
        return price;
    }
    // Checks if two objects are equal.
    public boolean equals(Car c1, Car c2)
    {
        boolean b = false;
        if(c1.getMake().equals(c2.getMake()) && c1.getPrice() == c2.getPrice() &&
        c1.getYear() == c2.getYear())
        {
            b = true;
            return b;
        }
        else
        {
            return b;
        }
    }
    // Turns the object into a readable string.
    public String toString()
    {
        return "Description of car:" +      
        "\n    Make : " + make +
        "\n    Year : " + year +
        "\n    Price: " + twoDecPl.format(price);
    }
}

    import java.util.Scanner; // imports a scanner
public class CarSearch {

    public static void main(String[] args)
    {
        // initializes all variables
        Scanner scan = new Scanner(System.in);
        final int SIZE_ARR = 30;
        Car[] carArr = new Car[SIZE_ARR];
        final String SENT = "EndDatabase";
        String carMake = "";
        int carYear = 0;
        double carPrice = 0;
        int count = 0;
        int pos = 0;
        final String SECSENT = "EndSearchKeys";
        final boolean DEBUG_SW = true;

        // Loop that goes through the first list of values.
        // It then stores the values in an array, then uses the
        // values to make an object.
        while(scan.hasNext())
        {

            if(scan.hasNext())
            {
                 carMake = scan.next();
            }
            else
            {
                System.out.println("ERROR - not a String");
                System.exit(0);
            }
            if(carMake.equals(SENT))
            {
                break;
            }
            if(scan.hasNextInt())
            {
                carYear = scan.nextInt();
            }
            else
            {
                System.out.println("ERROR - not an int" + count);
                System.exit(0);
            }
            if(scan.hasNextDouble())
            {
                carPrice = scan.nextDouble();
            }
            else
            {
                System.out.println("ERROR - not a double");
                System.exit(0);
            }
            Car car1 = new Car(carMake, carYear, carPrice);
            carArr[count] = car1;
            count++;
        }
        // Calls the method debugSwitch to show the debug information.
        debugSwitch(carArr, DEBUG_SW, count);
        // Calls the method printData to print the database.
        printData(carArr, count);
        // Loops through the second group of values and stores them in key.
        // Then, it searches for a match in the database.
        **while(scan.hasNext())**
        {
            if(scan.hasNext())
            {
                 carMake = scan.next();
            }
            else
            {
                System.out.println("ERROR - not a String");
                System.exit(0);
            }
            if(carMake.equals(SECSENT))
            {
                break;
            }
            if(scan.hasNextInt())
            {
                carYear = scan.nextInt();
            }
            else
            {
                System.out.println("ERROR - not an int" + count);
                System.exit(0);
            }
            if(scan.hasNextDouble())
            {
                carPrice = scan.nextDouble();
            }
            else
            {
                System.out.println("ERROR - not a double");
                System.exit(0);
            }
            Car key = new Car(carMake, carYear, carPrice);
            // Stores the output of seqSearch in pos.

            // If the debug switch is on, then it prints these statements.
            if(DEBUG_SW == true)
            {   
                System.out.println("Search, make = " + key.getMake());
                System.out.println("Search, year = " + key.getYear());
                System.out.println("Search, price = " + key.getPrice());
            }   
            System.out.println("key =");
            System.out.println(key);
            pos = seqSearch(carArr, count, key);
            if(pos != -1)
            {
                System.out.println("This vehicle was found at index = " + pos);
            }
            else
            {
                System.out.println("This vehicle was not found in the database.");
            }

        }

    }
    // This method prints the database of cars.
    private static void printData(Car[] carArr, int count)
    {
        for(int i = 0; i < count; i++)
        {
            System.out.println("Description of car:");
            System.out.println(carArr[i]);

        }
    }
    // Searches for a match in the database.
    private static int seqSearch(Car[] carArr, int count, Car key)
    {
        for(int i = 0; i < count; i++)
        {
            boolean b = key.equals(key, carArr[i]);
            if(b == true)
            {
                return i;
            }   

        }
        return -1;
    }
    // Prints debug statements if DEBUG_SW is set to true.
    public static void debugSwitch(Car[] carArr, boolean DEBUG_SW, int count)
    {
        if(DEBUG_SW == true)
        {
            for(int i = 0; i < count; i++)
            {
                System.out.println("DB make = " + carArr[i].getMake());
                System.out.println("DB year = " + carArr[i].getYear());
                System.out.println("DB price = " + carArr[i].getPrice());
            }   
        }
    }

}
4

1 回答 1

0

我认为这是你的问题,但我可能错了:

在您的 while 循环中,您有以下调用:

  • next()
  • nextInt()
  • nextDouble()

问题是最后一次调用 ( nextDouble) 不会吃掉换行符。nextLine()所以要解决这个问题,你应该在两个循环的末尾添加一个额外的调用。

发生的情况是,下次您调用 next() 时,它将返回换行符,而不是 CarMake-thing。

于 2012-12-05T16:55:05.873 回答