在java中无限循环,而不是在sentinel中读取

时间:2012-12-05 16:46:13

标签: java while-loop infinite-loop

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

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

}

1 个答案:

答案 0 :(得分:0)

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

在你的while循环中,你有这些调用:

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

问题是最后一次通话(nextDouble)不会吃换行符。因此,要解决此问题,您应该在两个循环结束时添加额外的nextLine()调用。

下一次调用next()时会发生什么情况,它会返回换行符,而不是CarMake-thing。