Jump to content

-JAVA- Creating an array using a variable from another class

Igor Perrotta
Go to solution Solved by MrMG,

Alright, I think I see your problem.

 Processo filaDeProcesso[] = new Processo[numeroDeProcessos];
 

This is executed at the moment you create a ContadorDeProcessos  object. That happens in the first line of this method:

 public void analisarTamanho() throws Exception {
        ContadorDeProcessos contadorNUmero =  new ContadorDeProcessos(); //RIGHT HERE. That means that here your array is set to a length of 0
        BufferedReader analisador = new BufferedReader(new FileReader("process.txt"));
        while (analisador.readLine() != null) {
            numeroDeProcessos++;
            

        }
        System.out.println("NUMERO DE PROCESSO" + numeroDeProcessos);
        contadorNUmero.setNumero(numeroDeProcessos);
        System.out.println("TESTE" + contadorNUmero.getNumero());
        contadorNUmero.numero = numeroDeProcessos;
        analisador.close();
    } 

that means your "contadorNUmero" object now has an array with the length 0.

First of all. Is there a reason why ContadorDeProcessos extends Leitor? I don't think you should do that, because ContadorDeProcessos doesn't really have anything to do with the Leitor class (Which I assume means Reader. If you don't understand what inheritance is normally used for then I can explain you that if you want).

Next you should be using a Constructor in ContadorDeProcessos, like this:

 

 

 public class ContadorDeProcessos {
    Leitor leitor =  new Leitor();
    int numero;
    Processo filaDeProcesso[];
    
    //This is the Constructor. This is called whenever you write "new ContadorDeProcessos(someInteger);"
    public ContadorDeProcessos(int numero){
    	this.numero = numero;
        filaDeProcesso[] = new Processo[numero]; //Now the array has the size of numeroDeProcessos
    ]
    
    public int getNumero() {
        return numero;
    }


    public void setNumero(int numero) {
        this.numero = numero;
    }


public void vernumero() {
    System.out.println(numero);
    
}

    public void setProcessoNaFila(Processo process, int num) {

        filaDeProcesso[num] = process;


    }
    
} 

 

Next in your Leitor code:

 public class Leitor {
    int numeroDeProcessos = 0;
    int contadorDeprocesso = 0;
    Escalonador escalonador = new Escalonador();

    public void analisarTamanho() throws Exception {
        BufferedReader analisador = new BufferedReader(new FileReader("process.txt"));
        while (analisador.readLine() != null) {
            numeroDeProcessos++;
            

        }
        ContadorDeProcessos contadorNUmero =  new ContadorDeProcessos(numeroDeProcessos); //Here numeroDeProcessos has the correct value and will now pass this value to the contadorNUmero object. This line will call the Constructor of ContadorDeProcessos
        System.out.println("NUMERO DE PROCESSO" + numeroDeProcessos);
        System.out.println("TESTE" + contadorNUmero.getNumero());
        analisador.close();
    } 

 

Now everything should happen in the right order and your array should have the right size.

 

Edit: The reason why it did not work before is because your array was created while numeroDeProcessos was still 0. Even if you change numeroDeProcessos afterwards, your array size stays the same once it is created. You just had to create the array once numeroDeProcessos had the right value. That was the mistake.

Hey guys, how are you today? 

I have an assignment for my school that requires me to implement a round-robin process scheduler without using java collectibles, ArrayList or any other stuff really. 

I'm having this probably very stupid problem. I'm taking an int variable that is my number of processes that is in the class that reads a text file and trying to create a static array of processes in another class.  This int value will set the array size. I tested to see if the right value is correct, and it is. the problem is attributing between classes.

i tested with using setters, directly attributing and in the desired class the value is still 0.

Link to comment
Share on other sites

Link to post
Share on other sites

Do you have any code that you could show? And I am not sure if I understood your problem correctly. You are trying to access an attribute which is stored in another Class?

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, MrMG said:

Do you have any code that you could show? And I am not sure if I understood your problem correctly. You are trying to access an attribute which is stored in another Class?

 

Yes, i'm trying to acess a int variable and create an array using it.

 

THis is the class im trying to take numeroDeProcessos and use in contadorDeProcessos, you can see multiple atempts and a very poorly organized code, that's the result of me almost giving up 

 

public class Leitor {
    int numeroDeProcessos = 0;
    int contadorDeprocesso = 0;
    Escalonador escalonador = new Escalonador();

    public void analisarTamanho() throws Exception {
        ContadorDeProcessos contadorNUmero =  new ContadorDeProcessos();
        BufferedReader analisador = new BufferedReader(new FileReader("process.txt"));
        while (analisador.readLine() != null) {
            numeroDeProcessos++;
            

        }
        System.out.println("NUMERO DE PROCESSO" + numeroDeProcessos);
        contadorNUmero.setNumero(numeroDeProcessos);
        System.out.println("TESTE" + contadorNUmero.getNumero());
        contadorNUmero.numero = numeroDeProcessos;
        analisador.close();
    }

===========

public class ContadorDeProcessos extends Leitor {
    Leitor leitor =  new Leitor();
    int numero = numeroDeProcessos;
    
    public int getNumero() {
        return numero;
    }


    public void setNumero(int numero) {
        this.numero = numero;
    }


    Processo filaDeProcesso[] = new Processo[numeroDeProcessos];
public void vernumero() {
    System.out.println(numero);
    
}

    public void setProcessoNaFila(Processo process, int num) {

        filaDeProcesso[num] = process;


    }
    
}

Link to comment
Share on other sites

Link to post
Share on other sites

Ok. What do you expect to happen and what is happening in your code? Do you get an error message?`Or is something not behaving how you want it to behave? Or do you not know how to proceed from here?

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, MrMG said:

Ok. What do you expect to happen and what is happening in your code? Do you get an error message?`Or is something not behaving how you want it to behave?

 

I'm trying to use the value in numeroDeProcessos to set the numero in the ContadorDeProcessos class. the value is right in the leitor class, but when I try to set in the ContadorDeProcessos class, the value stays 0, not allowing me to create the right size array and throwing an exception.

you can see in the code the multiples attempts i made, the only way it works is for me to set the array size anything more than the number i need, that in this case is 4. but i want something that can work in multiple cases

Link to comment
Share on other sites

Link to post
Share on other sites

Alright, I think I see your problem.

 Processo filaDeProcesso[] = new Processo[numeroDeProcessos];
 

This is executed at the moment you create a ContadorDeProcessos  object. That happens in the first line of this method:

 public void analisarTamanho() throws Exception {
        ContadorDeProcessos contadorNUmero =  new ContadorDeProcessos(); //RIGHT HERE. That means that here your array is set to a length of 0
        BufferedReader analisador = new BufferedReader(new FileReader("process.txt"));
        while (analisador.readLine() != null) {
            numeroDeProcessos++;
            

        }
        System.out.println("NUMERO DE PROCESSO" + numeroDeProcessos);
        contadorNUmero.setNumero(numeroDeProcessos);
        System.out.println("TESTE" + contadorNUmero.getNumero());
        contadorNUmero.numero = numeroDeProcessos;
        analisador.close();
    } 

that means your "contadorNUmero" object now has an array with the length 0.

First of all. Is there a reason why ContadorDeProcessos extends Leitor? I don't think you should do that, because ContadorDeProcessos doesn't really have anything to do with the Leitor class (Which I assume means Reader. If you don't understand what inheritance is normally used for then I can explain you that if you want).

Next you should be using a Constructor in ContadorDeProcessos, like this:

 

 

 public class ContadorDeProcessos {
    Leitor leitor =  new Leitor();
    int numero;
    Processo filaDeProcesso[];
    
    //This is the Constructor. This is called whenever you write "new ContadorDeProcessos(someInteger);"
    public ContadorDeProcessos(int numero){
    	this.numero = numero;
        filaDeProcesso[] = new Processo[numero]; //Now the array has the size of numeroDeProcessos
    ]
    
    public int getNumero() {
        return numero;
    }


    public void setNumero(int numero) {
        this.numero = numero;
    }


public void vernumero() {
    System.out.println(numero);
    
}

    public void setProcessoNaFila(Processo process, int num) {

        filaDeProcesso[num] = process;


    }
    
} 

 

Next in your Leitor code:

 public class Leitor {
    int numeroDeProcessos = 0;
    int contadorDeprocesso = 0;
    Escalonador escalonador = new Escalonador();

    public void analisarTamanho() throws Exception {
        BufferedReader analisador = new BufferedReader(new FileReader("process.txt"));
        while (analisador.readLine() != null) {
            numeroDeProcessos++;
            

        }
        ContadorDeProcessos contadorNUmero =  new ContadorDeProcessos(numeroDeProcessos); //Here numeroDeProcessos has the correct value and will now pass this value to the contadorNUmero object. This line will call the Constructor of ContadorDeProcessos
        System.out.println("NUMERO DE PROCESSO" + numeroDeProcessos);
        System.out.println("TESTE" + contadorNUmero.getNumero());
        analisador.close();
    } 

 

Now everything should happen in the right order and your array should have the right size.

 

Edit: The reason why it did not work before is because your array was created while numeroDeProcessos was still 0. Even if you change numeroDeProcessos afterwards, your array size stays the same once it is created. You just had to create the array once numeroDeProcessos had the right value. That was the mistake.

Link to comment
Share on other sites

Link to post
Share on other sites

 
 
 
 
 
 
5
3 hours ago, MrMG said:

Alright, I think I see your problem.


 Processo filaDeProcesso[] = new Processo[numeroDeProcessos];
 

This is executed at the moment you create a ContadorDeProcessos  object. That happens in the first line of this method:


 public void analisarTamanho() throws Exception {
        ContadorDeProcessos contadorNUmero =  new ContadorDeProcessos(); //RIGHT HERE. That means that here your array is set to a length of 0
        BufferedReader analisador = new BufferedReader(new FileReader("process.txt"));
        while (analisador.readLine() != null) {
            numeroDeProcessos++;
            

        }
        System.out.println("NUMERO DE PROCESSO" + numeroDeProcessos);
        contadorNUmero.setNumero(numeroDeProcessos);
        System.out.println("TESTE" + contadorNUmero.getNumero());
        contadorNUmero.numero = numeroDeProcessos;
        analisador.close();
    } 

that means your "contadorNUmero" object now has an array with the length 0.

First of all. Is there a reason why ContadorDeProcessos extends Leitor? I don't think you should do that, because ContadorDeProcessos doesn't really have anything to do with the Leitor class (Which I assume means Reader. If you don't understand what inheritance is normally used for then I can explain you that if you want).

Next you should be using a Constructor in ContadorDeProcessos, like this:

 

 


 public class ContadorDeProcessos {
    Leitor leitor =  new Leitor();
    int numero;
    Processo filaDeProcesso[];
    
    //This is the Constructor. This is called whenever you write "new ContadorDeProcessos(someInteger);"
    public ContadorDeProcessos(int numero){
    	this.numero = numero;
        filaDeProcesso[] = new Processo[numero]; //Now the array has the size of numeroDeProcessos
    ]
    
    public int getNumero() {
        return numero;
    }


    public void setNumero(int numero) {
        this.numero = numero;
    }


public void vernumero() {
    System.out.println(numero);
    
}

    public void setProcessoNaFila(Processo process, int num) {

        filaDeProcesso[num] = process;


    }
    
} 

 

Next in your Leitor code:


 public class Leitor {
    int numeroDeProcessos = 0;
    int contadorDeprocesso = 0;
    Escalonador escalonador = new Escalonador();

    public void analisarTamanho() throws Exception {
        BufferedReader analisador = new BufferedReader(new FileReader("process.txt"));
        while (analisador.readLine() != null) {
            numeroDeProcessos++;
            

        }
        ContadorDeProcessos contadorNUmero =  new ContadorDeProcessos(numeroDeProcessos); //Here numeroDeProcessos has the correct value and will now pass this value to the contadorNUmero object. This line will call the Constructor of ContadorDeProcessos
        System.out.println("NUMERO DE PROCESSO" + numeroDeProcessos);
        System.out.println("TESTE" + contadorNUmero.getNumero());
        analisador.close();
    } 

 

Now everything should happen in the right order and your array should have the right size.

 

Edit: The reason why it did not work before is because your array was created while numeroDeProcessos was still 0. Even if you change numeroDeProcessos afterwards, your array size stays the same once it is created. You just had to create the array once numeroDeProcessos had the right value. That was the mistake.

Hey, sorry for taking so long to answer.

First of all, thank you very much for taking the time to help me! your help was very valuable and I've been able to advance a lot in my code! I used inheritance as a desperate measure to see if it worked, lol. I'm rusty on programming (this semester has been mostly theoretical) and I've read a lot of about object-oriented programming to regain all the knowledge i forgot. Again, thank you :D

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×