2012-12-05 38 views
0

我在多个程序中都遇到过这个问题,但我不记得上次如何修复它。在我身体的第二个while循环中,第二个标记值从未读入,原因是某种原因。我一直试图修复它一段时间,以为我可能会看到任何人有任何线索。java中的无限循环,不在哨兵阅读中

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

} 
+0

这是很多代码。你能指出,你不明白代码的行为吗? – jlordo

+0

你有调试吗? –

+0

我把星号放在无限循环旁边,是的,我调试了它 –

回答

0

我觉得这是你的问题,但我可能是错的:

内部while循环,你有这些电话:

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

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

会发生什么情况是,当您下次调用next()时,它将返回新行,而不是CarMake事物。

+0

所以你说要在循环结束时添加carMake = scan.nextLine()? –