0

I am creating an array using a while loop. (For reasons why I am creating an array this way, go to https://www.cia.gov/library/publications/the-world-factbook/rankorder/rawdata_2151.txt) Though once my array (data) is created inside the while loop, I cannot access it outside of the while loop. I was hoping to make it so the user could put in the name of a country, say India, and get the number of mobile users in that country.

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

The input works if it is inside the loop, but will only work with China because it is the first country in the list. I understand why it is not working correctly, thought I'm unsure how to fix it other than putting the if statement outside of the loop, but if I do that, the statement cannot reach my array. Any suggestions?

4 Answers 4

3

The issue is here :

 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
 }

When return is called in your else clause it terminates the program. What it really needs to do is iterate through the second run of the loop.

Remove the return in your else-statment.

Here's the revised code:

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!");
    }
}

Here's a sample run: {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
Sign up to request clarification or add additional context in comments.

3 Comments

33 seconds and you'll be the answer. Thank you so much for that, I can't believe I didn't think of that.
@Agony Not a problem. Took me less than a minute to find the issue with my debugger. If you're planning on programming some more in the future I highly recommend you learn to use one.
I will definitely have to look into it. I've been sitting here for about 3 hours looking at it. I'm nearly bald with the hair I've pulled out :P
1

You are not able to iterate to second line because you are returning the while after first iteration whether it found the country or not.

I would suggest to remove the return statement from the else condition.

I have also used a boolean variable found which will be set once the country is found and No country found message will be appear only if that country is not in list.

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

}

On the other note, if you wants to use collections your program will become more concise and easy to read. Here is the same logic with 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();
    }
}

Comments

0

Try putting

String[] data;

before your loop. That will make its scope greater than just the loop.

Comments

0

Declare data outside "while" but assign it inside.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.