0

I want to create some instances of class, just like:

Country ger = new Country();
Country usa = new Country();
...

and so on. Because I want to do this for a huge number of objects, I'd rather want to create those instances with instance names from a text file where those country tags are listed and iterate through this list. I am familiar with the Java reflection concept, however I DO NOT WANT to create a single class for each object as I just want to cut those declarations short.

Is there any way to do that?

Thanks in advance

user2651050
  • 73
  • 1
  • 1
  • 7
  • 4
    Sounds like you probably want a `Map` - do you really need them as separate *variables*? (Note that it's the variable that has a name, not the instance.) – Jon Skeet Mar 21 '15 at 12:26

2 Answers2

1

Add a namevariable to Country and initialize it via the constructor. Or use a map.

1

So first let's focus on your Country Class. The root problem is that you'd like to make Country objects where each one represents one country. For this, you would want a constructor in class Country that takes a String as parameter, so that in your Main program you can do:

Country ger = new Country("Germany");
Country usa = new Country("USA");

But to do this, you first need to ensure the class Country has a an instance variable that can store the country for each object. So the Country class would need to have at least the following:

public class Country {

    //Each object holds a variable called country, which is the name of the country
    private String country;

    //This constructor takes the name you enter in brackets and sets country to this name
    public Country(String name) {
        country = name;
    }
}

Now we're ready to make Country objects that represent different countries. Since you want to enter the countries from a text file (.txt), you will need to make some tools to deal with the file in question. I'll be assuming the text file has only country names separated by a new line, for simplicity. You can extend your class Country to include a main methods as well as some relevant imports:

 import java.io.*;
 import java.util.Scanner;     

 public class Country {

    //Each object holds a variable called country, which is the name of the country
    private String country;

    //This constructor takes the name you enter in brackets and sets country to this name
    public Country(String name) {
        country = name;
    }

    public static void main(String[] args) {
        //This represents the file you want to access
        File countryFile = new File("path-of-text-file-goes-here");
        Scanner input = null;  //this tool will allow you to read from the file

        try {
            //we must initialize the file read tool in a try block
            input = new Scanner( new FileInputStream(countryFile) );
        }
        catch (FileNotFoundException e) {
            System.out.println("Error accessing the file. Exiting...");
            System.exit(0);
        }

        //initialize an empty String and we will put all the countries in the file
        //into our program by first putting it in a String separated by spaces
        String inputText = "";
        while (input.hasNext())  //tests to see whether input has a word left
            inputText += input.next() + " "; //if so, add it to the String and add a space

        //Here we are creating an array of Strings, where each indice is one word from inputText
        // the split(" ") method returns an array of characters separated by spaces
        String[] inputTextArray = inputText.split(" ");

        //initialize a Country array to the length of words in inputText
        Country[] countryObject = new Country[inputTextArray.length];

        //for each word in inputTextArray, initialize each indice in countryObject
        //with a new Country("each-indice-of-inputTextArray-will-be-placed-here-one-by-one")
        for (int i = 0; i < countryObject.length; i++)
            countryObject[i] = new Country(inputTextArray[i]);

    }
}

The array countryObject should now be populated with Country objects, where each one is a country you typed in your text file. Hope this helps, let me know if something is unclear to you or if I misunderstood the question. Thanks!

APengue
  • 138
  • 6