2012-12-10 166 views
2

首先這是一個作業問題。我完成了程序,並且運行良好;但我有一個小問題。該程序應該首先在線閱讀一個txt文件,並收集所有50個州及其縮寫。接下來,我必須要求用戶輸入一個有效的州名縮寫。一旦他們這樣做了,我必須在線閱讀另一個txt文件,然後讓兩位參議員回到他們的狀態。無限循環Java

我遇到的問題是我想循環掃描儀,要求用戶輸入一個狀態。如果他們進入不正確的狀態,那麼我想重新問他們。現在,如果我進入合法狀態,程序運行良好。如果我輸入的狀態不正確,程序將循環,並重新要求我進入另一個狀態。如果我進入一個不正確的狀態,然後進入正確的狀態,程序要求我進入另一個狀態,而不是繼續執行程序。請幫忙嗎?

我認爲這個問題是程序循環時,它不讀取:

if(stateAbbreviation.equals(abbreviation)) 
{ 
    state = splitData[0]; 
    abbrev = stateAbbreviation; 
    found = true; 
    break; 
} 

再次,使發現總是= FALSE。

import java.io.IOException; 
import java.net.MalformedURLException; 
import java.net.URL; 
import java.util.ArrayList; 
import java.util.Scanner; 

public class Senator 
{ 
    static String abbrev; 
    static String state; 

    public static void main(String[] args) throws IOException 
    { 
    // define our URL 
    URL serviceURL = new URL("http://i5.nyu.edu/~cmk380/cs101/USStates.txt"); 

    // connect and obtain data from this URL 
    Scanner input = new Scanner(serviceURL.openStream()); 
    Scanner input1 = new Scanner(System.in); 

    while(true) 
    { 
     boolean found = false; 

     //Ask for state 
     System.out.print("Enter state (abbreviation): "); 
     String stateAbbreviation = input1.next(); 

     // read in our data 
     while (input.hasNext()) 
     { 
     // grab a line 
     String data = input.nextLine(); 

     // split up the line based on the position of the commas 
     String[] splitData = data.split(","); 

     // grab out the abbreviation 
     String abbreviation = splitData[1]; 

     if(stateAbbreviation.equals(abbreviation)) 
     { 
      state = splitData[0]; 
      abbrev = stateAbbreviation; 
      found = true; 
      break; 
     } 
     } 

     if (found == true) 
     { 
     // close our URL 
     input.close(); 
     break; 
     } 
    } 

    double numLines = 0;  

    // define our URL 
    URL senatorURL = new URL("http://www.contactingthecongress.org/downloadsContactingCongress.db.txt"); 

    // connect and obtain data from this URL 
    Scanner input2 = new Scanner(senatorURL.openStream()); 

    // read in our data 
    while (input2.hasNext()) 
    { 
     // grab a line 
     String data = input2.nextLine(); 

     if (numLines != 0) 
     { 
     // split up the line based on the position of the commas 
     String[] splitData = data.split("\t"); 


     if(splitData[0].equals(abbrev+"SR")) 
     { 
      System.out.println(""); 
      System.out.println("State: " + state); 
      System.out.println(""); 
      System.out.println("Senior Senator"); 
      System.out.println("Name: " + splitData[1]); 

      //Try catch their information (exception raised if not found) 
      try 
      { 
      System.out.println("Address: " + splitData[3]); 
      } 
      catch (Exception e) 
      { 
      System.out.println("Addrress unavailable."); 
      } 

      try 
      { 
      System.out.println("Phone: " + splitData[4]); 
      } 
      catch (Exception e) 
      { 
      System.out.println("Phone unavailable."); 
      } 

      try 
      { 
      System.out.println("Website: " + splitData[7]); 
      } 
      catch (Exception e) 
      { 
      System.out.println("Website unavailable."); 
      } 
      System.out.println(""); 
     }   

     if(splitData[0].equals(abbrev+"JR")) 
     { 
      System.out.println("Junior Senator"); 
      System.out.println("Name: " + splitData[1]); 

      //Try catch their information (exception raised if not found) 
      try 
      { 
      System.out.println("Address: " + splitData[3]); 
      } 
      catch (Exception e) 
      { 
      System.out.println("Addrress unavailable."); 
      } 

      try 
      { 
      System.out.println("Phone: " + splitData[4]); 
      } 
      catch (Exception e) 
      { 
      System.out.println("Phone unavailable."); 
      } 

      try 
      { 
      System.out.println("Website: " + splitData[7]); 
      } 
      catch (Exception e) 
      { 
      System.out.println("Website unavailable."); 
      } 
      System.out.println(""); 
     } 

     } 

     numLines ++; 
    } 

    // close our URL 
    input2.close(); 
    } 
} 
+0

你說在新的編輯中它總是返回false。你是說現在只返回false還是隻在false和true後返回false? – DrinkJavaCodeJava

+0

檢查while循環中的if語句是否通過將System.out.println(「If executed」);如果沒有,我想我可能會知道你的問題。 – DrinkJavaCodeJava

回答

3

你有兩個while,然後嘗試用休息來擺脫它,但你剛開出內while的。

要脫離while s,您需要更改程序邏輯或使用標籤(是的,Java可以使用標籤,並且一次性打破內部循環和outter循環就是其中一個用途)

+1

我明白了。其中一個問題是,如果我再次通過while循環,程序將不會再讀取文件;它只是在txt文件的底部。所以我在循環中再次連接到了URL。我認爲這是不好的做法,每次都重新連接到網址。謝謝你的幫助。 – JerryCrowley

1

你可以做到這一點的另一種方式是

 public static boolean checkAbbreive(){ 
       while(true) 
       { 
        boolean found = false; 

        //Ask for state 
        System.out.print("Enter state (abbreviation): "); 
        String stateAbbreviation = input1.next(); 

        // read in our data 
        while (input.hasNext()) 
        { 
         // grab a line 
         String data = input.nextLine(); 

         // split up the line based on the position of the commas 
         String[] splitData = data.split(","); 

         // grab out the abbreviation 
         String abbreviation = splitData[1]; 

         if(stateAbbreviation.equals(abbreviation)) 
         { 
          state = splitData[0]; 
          abbrev = stateAbbreviation; 
          found = true; 
          return found; 
         } 

       } 
     if (checkAbbreive() == true) 
      { 
      // close our URL 
      input.close(); 
      break; 
      } 

    } 
} 

聲明同時while循環的功能包裹並有發現後返回發現命令=真

+0

感謝您的幫助。弄清楚了。 – JerryCrowley

0

我不知道,如果這將工作,但試圖擺脫兩個循環。

breakTheWholeWhileLoop: 
while(true) 
{ 
    //For this version, we won't need the boolean found 
    //since we'll break out of everything 

    //Ask for state 
    System.out.print("Enter state (abbreviation): "); 
    String stateAbbreviation = input1.next(); 

    //Read in our data 
    while(input.hasNextLine()) 
    { 
     //... 

     if(stateAbbreviation.equals(abbreviation)) 
     { 
     state = splitData[0]; 
     abbrev = stateAbbreviation; 
     input.close(); 

     //This piece of code will break out of the while(true) loop 
     break breakTheWholeWhileLoop; 
     } 
    } //while input loop 
} //while true loop 

//Do the rest 

希望這會有所幫助。

+0

感謝您的幫助。弄清楚了。 – JerryCrowley