Remplir un tableau (java)

Fermé
BDD - Modifié par BDD le 7/05/2014 à 18:37
KX Messages postés 16734 Date d'inscription samedi 31 mai 2008 Statut Modérateur Dernière intervention 24 avril 2024 - 8 mai 2014 à 09:43
Bonsoir !
J'ai un problème avec le remplissage de tableaux en java.
J'aimerais bien déclarer 4 tableaux au début de la classe, puis ensuite par des méthodes les remplir et que chaque méthode aie ses propres composantes.

Voici mon code mais qui ne se compile pas : (le message est illegal start of expression) !
Je ne sais pas si on peut faire de cette manière pour arrivé au résultat désiré ci-dessus, si vous avez une solution à ce problème, je suis preneur.

public class BDD{
    
    String[] tab1;
    String[] tab2;
    String[] tab3;
    String[] tab4;
    
    public BDD(){
    }
    
    public void L1P1(){
            tab1 = {"the hardware","a computer","a laptop","a screen","a monitor","a keyboard","a key","a mouse","a mouse pad","a printer"};    
            tab2 = {"le matériel informatique","un ordinateur","un ordinateur portable","un écran","un moniteur","un clavier","une touche","une souris","un tapis de souris","une imprimante"};  
            tab3 = {"le matériel","un ordinateur de bureau","un pc portable","/","/","/","/","une souris d'ordinateur","un tapis de souris","/"};
            tab4 = {"hardware","un pc","un portable","/","/","/","/","/","/","/"};
        }
    }
    
    public void L1P2(){
        tab1 = {"a window","the password","a file","a database","a shortcut","the trash","(to) install","(to) program","(to) delete","(to) save"};
        tab2 = {"une fenêtre","le mot de passe","un fichier","une base de données","un raccourci","la corbeille","installer","programmer","effacer","sauvegarder"};  
        tab3 = {"/","/","/","/","/","/","/","/","supprimer","enregistrer"};
        tab4 = {"/","/","/","/","/","/","/","/","/","/"};
    }
    
    public void L1P3(){
        tab1 = {"a net surfer","a network","a search engine","a browser","a server","a login","a link","a website","a homepage","(to) download"};
        tab2 = {"un internaute","le réseau","un moteur de recherche","un navigateur","un serveur","un identifiant","un lien","un site web","une page d'accueil","télécharger"};  
        tab3 = {"/","/","/","/","/","/","/","/","/","/"};
        tab4 = {"/","/","/","/","/","/","/","/","/","/"};
    }
    
    public void L2P1(){   
        tab1 = {"the teacher","the nursery school teacher","the head teacher","the inspector","the pupil","the student","the boarder","the day pupil","the good pupil","the bad pupil"};
        tab2 = {"le professeur","l'instituteur maternelle","le directeur","l'inspecteur","l'élève","l'étudiant","l'interne","l'externe","le bon élève","le mauvais élève"};  
        tab3 = {"Maitresse","/","/","/","/","/","/","/","/","/"};
        tab4 = {"Maitre","/","/","/","/","/","/","/","/","/"};
    }
    
    public void L2P2(){
        tab1 = {"the term","the timetable","the subject","the lesson","the period","the free period","the courses","the classroom","the double period","the registration"};
        tab2 = {"le trimestre","l'emploi du temps","la matière","la leçon","le cours","l'heure de permanence","l'ensemble des cours","la salle de classe","le cours de deux heures","l'appel en début de cours"};  
        tab3 = {"/","/","/","/","/","/","/","/","/","/"};
        tab4 = {"/","/","/","/","/","/","/","/","/","/"};
    }
    
    public void L2P3(){
        tab1 = {"the homework","the exercise","the problem","the question","the answer","the test","the written test","the oral test","the mistake","the result"};
        tab2 = {"le devoir","l'exercice","le problème","la question","la réponse","le test","le test écrit","le test oral","la faute","le résultat"};  
        tab3 = {"/","/","/","/","/","l'interrogation","l'interrogation écrite","l'interrogation orale","l'erreur","/"};
        tab4 = {"/","/","/","/","/","l'interro","l'interro écrite","l'interro orale","/","/"};
    }
    
    public void L3P1(){   
        tab1 = {"a baby","a child","a teenager","the father","the mother","the husband","the in-laws","the stepmother","the nephew","the aunt"};
        tab2 = {"un bébé","un enfant","un adolescent","le père","la mère","le mari","la belle famille","la belle-mère","le neveu","la tante"};  
        tab3 = {"/","/","/","/","/","l'époux","/","la belle mère","/","/"};
        tab4 = {"/","/","/","/","/","/","/","/","/","/"};
    }
    
    public void L3P2(){
        tab1 = {"the birth","childhood","teenage","youth","adulthood","old age","the death","the engagement","the wedding","the honeymoon"};
        tab2 = {"la naissance","l'enfance","l'adolescence","la jeunesse","l'age adulte","la vieillesse","la mort","les fiançailles","le mariage","la lune de miel"};  
        tab3 = {"/","/","/","/","/","/","/","/","/","/"};
        tab4 = {"/","/","/","/","/","/","/","/","/","/"};
    }
    
    public void L3P3(){
        tab1 = {"(to) have children","(to) grow up","be under age","be in one's teens","(to) come of age","(to) bring up children","(to) meet","(to) fall in love","(to) get engaged","(to) get married"};
        tab2 = {"avoir des enfants","grandir","etre mineur","etre adolescent","devenir majeur","élever les enfants","se rencontrer","tomber amoureux","se fiancer","se marier"};  
        tab3 = {"/","/","/","/","/","/","rencontrer","/","/","/"};
        tab4 = {"/","/","/","/","/","/","/","/","/","/"};
    }
    
    public void L4P1(){   
        tab1 = {"now","today","yesterday","tomorrow","then","later","tonight","right now","last night","this morning"};
        tab2 = {"maintenant","aujourd'hui","hier","demain","puis","plus tard","ce soir","pour le moment","hier soir","ce matin"};  
        tab3 = {"/","/","/","/","alors","/","/","en ce moment","la nuit dernière","/"};
        tab4 = {"/","/","/","/","ensuite","/","/","/","/","/"};
    }
    
    public void L4P2(){
        tab1 = {"already","recently","lately","soon","at once","still","though","yet","however","nevertheless"};
        tab2 = {"déjà","récemment","dernièrement","bientôt","immédiatement","toujours","quoique","encore","cependant","quand même"};  
        tab3 = {"/","/","/","/","/","encore","/","cependant","toutefois","cependant"};
        tab4 = {"/","/","/","/","/","/","/","pourtant","cependant","pourtant"};
    }
    
    public void L4P3(){
        tab1 = {"nowhere","everywhere","anywhere","over there","there","here","away","out","near","far"};
        tab2 = {"nulle part","partout","n'importe où","là-bas","là","ici","loin","dehors","près","loin"};  
        tab3 = {"/","/","/","/","/","/","/","/","/","/"};
        tab4 = {"/","/","/","/","/","/","/","/","/","/"};
    }
    
    public void L5P1(){   
        tab1 = {"(to) accept","(to) allow","(to) ask","(to) believe","(to) borrow","(to) break","(to) bring","(to) buy","(to) can","(to) be able"};
        tab2 = {"accepter","permettre","demander","créer","emprunter","casser","apporter","acheter","pouvoit","etre capable de"};  
        tab3 = {"/","/","/","/","/","/","/","/","/","/"};
        tab4 = {"/","/","/","/","/","/","/","/","/","/"};
    }
    
    public void L5P2(){
        tab1 = {"(to) cancel","(to) change","(to) clean ","(to) complain","(to) cut","(to) dance","(to) draw","(to) drink","(to) drive","(to) eat"};
        tab2 = {"annuler","changer","nettoyer","se plaindre","couper","danser","dessiner","boire","conduire","manger"};  
        tab3 = {"/","/","/","/","/","/","/","/","/","/"};
        tab4 = {"/","/","/","/","/","/","/","/","/","/"};
    }
    
    public void L5P3(){
        tab1 = {"(to) explain","(to) fall","(to) fill","(to) find","(to) finish","(to) fit","(to) fix","(to) fly","(to) forget","(to) give"};
        tab2 = {"expliquer","tomber","remplir","trouver","finir","mettre","fixer","voler","oublier","donner"};  
        tab3 = {"/","/","/","/","/","ajuster","arranger","/","/","/"};
        tab4 = {"/","/","/","/","/","/","décider","/","/","/"};
    }
}


Merci de votre aide :)
A voir également:

1 réponse

KX Messages postés 16734 Date d'inscription samedi 31 mai 2008 Statut Modérateur Dernière intervention 24 avril 2024 3 015
Modifié par KX le 8/05/2014 à 11:17
Bonjour,

Tu peux faire :
tab1 = new String [] {"a", "b", ...};


Mais selon ton besoin tu peux également faire :

List<String> list1; // import java.util.List

list1 = Arrays.asList("a", "b", ...); // import java.util.Arrays

C'est un peu plus mieux mais c'est loin d'être propre.
Il faudrait voir ce que tu veux faire exactement. Peut être que la manipulation de fichiers .properties serait plus adaptée.

Exemple :

FileInputStream input = new FileInputStream("text.properties"); // import java.io.FileInputStream

Properties prop = new Properties(); // import java.util.Properties
prop.load(input);
input.close();

System.out.println(prop.getProperty("L1P1.tab1.txt1")); // the hardware


text.properties

L1P1.tab1.txt1=the hardware
L1P1.tab1.txt2=a computer
...
L1P1.tab2.txt1=le matériel informatique
L1P1.tab2.txt2=un ordinateur
...
L1P2.tab1.txt1=a window
L1P2.tab1.txt2=the password
...
0