IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Collection et Stream Java Discussion :

Problème tableau static à vérifier


Sujet :

Collection et Stream Java

  1. #1
    Nouveau membre du Club
    Profil pro
    Inscrit en
    Mai 2005
    Messages
    113
    Détails du profil
    Informations personnelles :
    Localisation : Suisse

    Informations forums :
    Inscription : Mai 2005
    Messages : 113
    Points : 39
    Points
    39
    Par défaut Problème tableau static à vérifier
    Bonjour,

    J'ai un problème dans mon code
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    package calcul;
     
    import java.util.*;
    import java.io.*;
     
    // classe de test
    public class euro{
    int  tab[] ;
        // fonction principale
        public static void main(String args[]) {
            Random rand = new Random(); // constructeur
            int i = rand.nextInt(50);  // génération
            if ( tab[i] == i )
            {
     
            }
            System.out.println(i);  // affichage
        }
    }
    à la ligne 13, sur le if, j'ai le message :
    cannot make static the non static
    Quelqu'un saurait-il m'indiquer comment résoudre cette erreur ?

    Merci d'avance pour votre aide.

  2. #2
    Expert éminent sénior
    Avatar de tchize_
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Avril 2007
    Messages
    25 481
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 44
    Localisation : Belgique

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Avril 2007
    Messages : 25 481
    Points : 48 806
    Points
    48 806
    Par défaut
    tab est déclaré comme variable d'instance de euro, elle n'existe donc que pour les instance d'euro, tu ne peux donc pas y accéder sans avoir une instance, donc pas depuis une méthode statique.

  3. #3
    Expert éminent sénior
    Avatar de tchize_
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Avril 2007
    Messages
    25 481
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 44
    Localisation : Belgique

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Avril 2007
    Messages : 25 481
    Points : 48 806
    Points
    48 806
    Par défaut
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    public static void main(String args[]) {
            euro e = new euro();
            Random rand = new Random(); // constructeur
            int i = rand.nextInt(50);  // génération
            if ( e.tab[i] == i )
            {
     
            }
            System.out.println(i);  // affichage
        }

  4. #4
    Nouveau membre du Club
    Profil pro
    Inscrit en
    Mai 2005
    Messages
    113
    Détails du profil
    Informations personnelles :
    Localisation : Suisse

    Informations forums :
    Inscription : Mai 2005
    Messages : 113
    Points : 39
    Points
    39
    Par défaut
    errereur ligne 14
    Exception in thread "main" java.lang.NullPointerException
    at calcul.euro.main(euro.java:14)


    la ligne 14 : if ( e.tab[i] == i )

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
     
    package calcul;
     
    import java.util.*;
    import java.io.*;
     
    // classe de test
    public class euro{
    	int  tab[] ;
    	public static void main(String args[]) {
            euro e = new euro();
            Random rand = new Random(); // constructeur
            int i = rand.nextInt(50);  // génération
            if ( e.tab[i] == i )
            {
     
            }
            System.out.println(i);  // affichage
        }
    }

  5. #5
    Nouveau membre du Club
    Profil pro
    Inscrit en
    Mai 2005
    Messages
    113
    Détails du profil
    Informations personnelles :
    Localisation : Suisse

    Informations forums :
    Inscription : Mai 2005
    Messages : 113
    Points : 39
    Points
    39
    Par défaut
    ok résolu

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
        public static void main(String args[]){
            int rnd;
     
            Random rand=new Random();
            int[] nums = new int[50];
            boolean[] check = new boolean[50];
            for (int k = 0; k<50; k++)
            {
                rnd = rand.nextInt(50);
     
                //Loop for when there number is already chosen
                while (check[rnd])
                {
                    rnd = rand.nextInt(50);
     
                }
    //Sets the random unique number to a slot in the array
                    nums[k]=rnd;
                    check[rnd]=true;
     
            }
     
            for(int num : nums){
                System.out.println(" " + num);
     
            }
     
        }
    merci

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. [Tableaux] Probléme Tableau / Menu
    Par carnage_59 dans le forum Langage
    Réponses: 7
    Dernier message: 13/10/2005, 13h34
  2. Problème tableau pour une requête
    Par LE NEINDRE dans le forum Langage
    Réponses: 2
    Dernier message: 07/10/2005, 20h21
  3. Problème Tableau Sous DW
    Par Lorik dans le forum Balisage (X)HTML et validation W3C
    Réponses: 7
    Dernier message: 04/10/2005, 11h55
  4. [perl]Problème tableau indexé
    Par LE NEINDRE dans le forum Langage
    Réponses: 8
    Dernier message: 25/08/2005, 21h24
  5. [CR8] Problème tableau sur plusieurs pages???
    Par christophe28 dans le forum SAP Crystal Reports
    Réponses: 5
    Dernier message: 02/11/2004, 15h46

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo