2012-12-05 72 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()? –