2014-03-29 89 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