LebGeeks

A community for technology geeks in Lebanon.

You are not logged in.

#1 October 22 2016

roudy5
Member

[JAVA] Can't see where the problem is ?

Hello everyone, i have a homework that i've been trying to do for like 2 weeks, it's a program to simulate a client with several bank accounts, each account has an account number and his balance...I've done everything else and the last thing i need to do is create a static array that holds the accounts in it.

I already have a class "Compte" which is a class for one account, and the objective is to create another class "Client" which holds several accounts so i guess i should create objects from the "Compte" class and then store the created objects in the array, i created the array using :

Compte comptes[] = new Compte[10];

and then made the first object using:

Compte compte = new Compte(1);

and now the problem is storing "compte" object in the array, i try:

comptes[0] = compte;

and it gives me: cannot find symbol
symbol: class comptes
location: class client
']' expected
<identifier> expected

I'm using NetBeans IDE.

I asked the teacher several times why is it happening and everytime he says i should figure it out on my own and all he said to me is put the code in the constructor..(All the code above is in the Client class)

Question: why can't i store the object "compte" created earlier in the array, should i put the code elsewhere ?

Offline

#2 October 22 2016

anayman_k7
Member

Re: [JAVA] Can't see where the problem is ?

Hello, it helps if you post more code, like the definition of the class, in which package, the class of where you are trying to define the Compte array and in which package and what are the imports in the top of the class.

Offline

#3 October 22 2016

Silentcontrol
Member

Re: [JAVA] Can't see where the problem is ?

are you wirting that code in class client? i agree anayman we need more code
do you have a constructor in class compte with 1 parameter?

Offline

#4 October 22 2016

roudy5
Member

Re: [JAVA] Can't see where the problem is ?

Yea sorry i just wanted the post to be easy to read not a whole lot of text but guess it should be big
here is Compte class:

 package compte;

/**
 *
 * @author A-pc
 */
public class Compte {
    int numeroCompte;
    double solde;
    double tauxInterets = 1.05;
    public Compte(int a){
        numeroCompte = a*5;
        solde = 0;
        tauxInterets = 1.05;
    }
    public Compte(){
    
    }
    public void Depot(int d){
        solde = solde + d;
    }
    public void Retrait(int r){
        solde = solde - r;
    }
    public boolean Decouverte(){
        if(solde<0)
            return true;
        else
            return false;
    }
    public void AffectationInteret(){
        solde = solde*tauxInterets;
    }
    public void Afficher(){
        System.out.println("Numero de compte:"+numeroCompte);
        System.out.println("Solde:"+solde);
    }
    public int Num(){
        int a = numeroCompte;
        return a;
    }
    public static void main(String[] args) {
        Compte c = new Compte(1);
        c.Depot(40000);
        c.Retrait(6000);
        c.AffectationInteret();
        c.Afficher();
        c.Decouverte();
    }
    
}

and the Client class:

 package compte;

/**
 *
 * @author A-pc
 */
public class Client {
    Compte[] comptes = new Compte[5];
    Compte c0 = new Compte(1);
    Compte c1 = new Compte(2);
    Compte c2 = new Compte(3);
    Compte c3 = new Compte(4);
    Compte c4 = new Compte(5);
    comptes[0]=c0;
    comptes[1]=c1;
    comptes[2]=c2;
    comptes[3]=c3;
    comptes[4]=c4;
    
    public void consommer (int num,int s){
        for(int i = 0;i<5;i++){
            if(comptes[i].numeroCompte == num){
                comptes[i].Retrait(s);
            }
        }
    }
    public void ajouter (int num,int s){
        for(int i = 0;i<5;i++){
            if(comptes[i].numeroCompte == num){
                comptes[i].Depot(s);
            }
        }
    }
    public void bilan(){
        for(int i = 0;i<5;i++){
            boolean check = comptes[i].Decouverte();
            if (check == true){
                System.out.println("Ce compte est decovert!");
            }
        }
    }
    public void balance(){
        for(int i = 0;i<5;i++){
            System.out.println("Numero du comptes:"+comptes[i].numeroCompte);
            System.out.println("Solde:"+comptes[i].solde);
        }
    }
}

Sorry for late reply, connection was down..

Last edited by roudy5 (October 22 2016)

Offline

#5 October 22 2016

Adnan
Member

Re: [JAVA] Can't see where the problem is ?

Post the whole exception so we can see where the problem exactly is.

Offline

#6 October 22 2016

roudy5
Member

Re: [JAVA] Can't see where the problem is ?

The error is at

comptes[0]=c0;
comptes[1]=c1;
comptes[2]=c2;
comptes[3]=c3;
comptes[4]=c4;

and i posted earlier what error the IDE gives me.
(if that is what you are asking.. didnt quite understand what u meant)

Offline

#7 October 22 2016

Adnan
Member

Re: [JAVA] Can't see where the problem is ?

You can't fill the array with values outside of a method.

Offline

#8 October 22 2016

anayman_k7
Member

Re: [JAVA] Can't see where the problem is ?

Adnan wrote:

You can't fill the array with values outside of a method.

100%, create a constructor for the client class,

public Client() {
    comptes[0]=new Compte(1);
    comptes[1]=new Compte(2);
    comptes[2]=new Compte(3);
    comptes[3]=new Compte(4);
    comptes[4]=new Compte(5);
}



You don't need to create c1,c2,c3,c4 and c5 separately

Offline

#9 October 22 2016

roudy5
Member

Re: [JAVA] Can't see where the problem is ?

Oh i see.. to make sure i understood it so when i have to fill the array it has to be inside of a constructor or function or main.. whatever the method is right ?
Thanks a lot guys really !

Offline

#10 January 17 2017

danso
Member

Re: [JAVA] Can't see where the problem is ?

Make sure all methods name always start with  a lowercase  and continue as a camel notation. This will make it easier to identify the nature of the code constructs you are referring in java

http://www.javatpoint.com/java-naming-conventions

Offline

Board footer