2014-03-29 122 views
0

我正在使用while循環創建一個數組。 (爲什麼我這樣創建一個數組,請參考https://www.cia.gov/library/publications/the-world-factbook/rankorder/rawdata_2151.txt)雖然在while循環中創建了我的數組(data),但我無法在while循環之外訪問它。我希望能夠這樣做,以便用戶可以輸入一個國家的名稱,例如印度,並獲得該國的移動用戶數量。在循環之外訪問循環創建的數組

String address = "https://www.cia.gov/library/publications/the-world-factbook/rankorder/rawdata_2151.txt"; 
URL pageLocation = new URL(address); 
Scanner in1 = new Scanner(pageLocation.openStream()); 
Scanner in = new Scanner(System.in); 
String line; 
System.out.print("Please enter the name of the country you would like to see the mobile users for: "); 
String country = in.next(); 
while (in1.hasNextLine()){ 
line = in1.nextLine(); 
String[] data = line.split("\t"); 
if (data[1].contains(country) == true){ 
    System.out.println("Country name: " + data[1]); 
    System.out.println("Mobile phone subscribers: " + data[2]); 
    return; 
} 
else{ 
    System.out.println("No country found with that name!"); 
    return; 
    } 
    } 

輸入工作,如果它是內循環,但因爲它是第一個國家在列表中只會與中國合作。我明白爲什麼它不能正常工作,我認爲除了將if語句放在循環之外之外,我不確定如何解決它,但是如果我這樣做了,則語句無法訪問我的數組。有什麼建議麼?

回答

3

問題就在這裏:

if (data[1].contains(country) == true){ 
    System.out.println("Country name: " + data[1]); 
    System.out.println("Mobile phone subscribers: " + data[2]); 
    return; 
} else { 
    System.out.println("No country found with that name!"); 
    return; //<-----ISSUE 
} 

return就是所謂的else子句在其終止程序。它真正需要做的是迭代循環的第二次運行。

刪除else-statment中的return

下面是修改後的代碼:

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

public class TestClass { 
    public static void main(String[] args) throws IOException { 
     String address = "https://www.cia.gov/library/publications/the-world-factbook/rankorder/rawdata_2151.txt"; 
     URL pageLocation = new URL(address); 
     Scanner in1 = new Scanner(pageLocation.openStream()); 
     Scanner in = new Scanner(System.in); 
     String line; 
     System.out 
       .print("Please enter the name of the country you would like to see the mobile users for: "); 
     String country = in.next(); 

     while (in1.hasNextLine()) { 
      line = in1.nextLine(); 
      String[] data = line.split("\t"); 

      if (data[1].contains(country) == true) { 
       System.out.println("Country name: " + data[1]); 
       System.out.println("Mobile phone subscribers: " + data[2]); 
       return; //<--- will exit after printing^
      } 
     } 
     System.out.println("No country found with that name!"); 
    } 
} 

下面是一個運行示例:{input} India

Please enter the name of the country you would like to see the mobile users for: India 
Country name: India 
Mobile phone subscribers:  893,862,000 
+0

33秒相同的邏輯,你會成爲答案。非常感謝你,我不敢相信我沒有想到這一點。 – usernolongerregistered

+0

@Agony沒問題。花了我不到一分鐘的時間來找到我的'調試器'問題。如果你打算在將來編程更多,我強烈建議你學會使用一個。 – Tdorno

+0

我一定要看看它。我一直坐在這裏看大約3個小時。我幾乎禿頂了我拔出的頭髮:P – usernolongerregistered

0

嘗試把

String[] data; 

您的循環之前。這將使其範圍大於循環。

0

聲明以外的數據 「而」 但裏面進行分配。

String address = "https://www.cia.gov/library/publications/the-world-  factbook/rankorder/rawdata_2151.txt"; 
URL pageLocation = new URL(address); 
Scanner in1 = new Scanner(pageLocation.openStream()); 
Scanner in = new Scanner(System.in); 
String line; 
System.out.print("Please enter the name of the country you would like to see the mobile users for: "); 
String country = in.next(); 
String[] data; 
while (in1.hasNextLine()){ 
    line = in1.nextLine(); 
    data = line.split("\t"); 
    if (data[1].contains(country) == true){ 
    System.out.println("Country name: " + data[1]); 
    System.out.println("Mobile phone subscribers: " + data[2]); 
    return; 
    } else{ 
    System.out.println("No country found with that name!"); 
    return; 
    } 
} 
Objects.toString(data); // now its visible 
1

您無法迭代到第二行,因爲您在第一次迭代後返回,而不管它是否找到該國。

我建議從else條件中刪除return語句。

我還使用了一個boolean變量,一旦找到國家就會設置該變量,並且No country found消息只有在該國家不在列表中時纔會顯示。

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

public class CountryName { 
public static void main(final String[] args) throws IOException { 
    final String address = "https://www.cia.gov/library/publications/the-world-factbook/rankorder/rawdata_2151.txt"; 
    final URL pageLocation = new URL(address); 
    final Scanner in1 = new Scanner(pageLocation.openStream()); 
    final Scanner in = new Scanner(System.in); 
    boolean found = false; 
    String line; 
    System.out 
      .print("Please enter the name of the country you would like to see the mobile users for: "); 
    final String country = in.next(); 
    while (in1.hasNextLine()) { 
     line = in1.nextLine(); 
     final String[] data = line.split("\t"); 
     if (data[1].contains(country) == true) { 
      System.out.println("Country name: " + data[1]); 
      System.out.println("Mobile phone subscribers: " + data[2]); 
      found = true; 
      return; 
     } 
    } 
    if (!found) { 
     System.out.println("No Country Found"); 
    } 
in.close(); 
in1.close(); 
} 

}

在其他的注意,如果你想使用集合你的程序將變得更加簡潔易讀。這是與HashMap

import java.io.IOException; 
import java.net.URL; 
import java.util.HashMap; 
import java.util.Map; 
import java.util.Scanner; 
public class CountryName { 
    public static void main(final String[] args) throws IOException { 
     final String address = "https://www.cia.gov/library/publications/the-world-factbook/rankorder/rawdata_2151.txt"; 
     final URL pageLocation = new URL(address); 
     final Scanner in1 = new Scanner(pageLocation.openStream()); 
     final Scanner in = new Scanner(System.in); 
     final Map<String, String> countryMap = new HashMap<String, String>(); 
     while (in1.hasNextLine()) { 
      final String[] line = in1.nextLine().split("\t"); 
      countryMap.put(line[1], line[2]); 
     } 
     System.out.print("Please enter the name of the country you would like to see the mobile users for: "); 
     final String country = in.next(); 
     if (countryMap.containsKey(country)) { 
      System.out.println("Country Name: " + country); 
      System.out.println("Mobile phone subscribers: "+ countryMap.get(country)); 
     } else { 
      System.out.println("No Country found with that name"); 
     } 
     in.close(); 
     in1.close(); 
    } 
} 
+0

有點晚了,但這是正確的。謝謝! – usernolongerregistered

+0

@Agony歡迎您 – Ashish