Alors d'abord, concentrons-nous sur votre Country Class. Le problème de base est que vous souhaitez créer des objets Pays où chacun représente un pays. Pour cela, vous voulez un constructeur dans le pays de classe qui prend une chaîne en tant que paramètre, de sorte que dans votre programme principal, vous pouvez faire:
Country ger = new Country("Germany");
Country usa = new Country("USA");
Mais pour ce faire, vous devez d'abord d'assurer le pays de classe a une une variable d'instance qui peut stocker le pays pour chaque objet. La classe Country doit donc avoir au moins les éléments suivants:
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;
}
}
Maintenant, nous sommes prêts à créer des objets Pays représentant différents pays. Étant donné que vous souhaitez entrer les pays à partir d'un fichier texte (.txt), vous devrez créer des outils pour traiter le fichier en question. Je suppose que le fichier texte n'a que des noms de pays séparés par une nouvelle ligne, pour plus de simplicité. Vous pouvez étendre votre pays de classe pour inclure une des méthodes principales, ainsi que des importations concernées:
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]);
}
}
Le countryObject tableau doit maintenant être rempli avec des objets de pays, où chacun est un pays que vous avez saisi dans votre fichier texte. J'espère que cela aide, faites-moi savoir si quelque chose ne vous est pas clair ou si j'ai mal compris la question. Merci!
Semble que vous voulez probablement un 'Map' - en avez-vous vraiment besoin en tant que * variables * séparées? (Notez que c'est la variable qui a un nom, pas l'instance.) –
Pourquoi ne pas utiliser une carte? –