My list still has Tail and head as null

Asked

Viewed 57 times

0

It was all going well until I noticed that the list is not filled out, leaving my head and Tail as null. Supposedly the program should read from an input file and fill in the list within a cycle.

Here is the code in question:

package projectosalarios;
import java.io.BufferedWriter;
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

class Funcionario{
    String Nome;
    String BI;
    int Departamento;
    int Cargo;
    int Salario;
    public Funcionario(String Nome, String BI, int Departamento, int Cargo, int Salario){
        this.Nome = Nome;
        this.BI = BI;
        this.Departamento = Departamento;
        this.Cargo = Cargo;
        this.Salario = Salario;
    }

}

class Lista {
    Node head;
    Node tail;

    public static void add(Lista Lista, Funcionario NovoFuncionario) {
        Node novoNo = new Node (NovoFuncionario);
                if(Lista.head == null){
                    Lista.head = novoNo;
                    Lista.tail = novoNo;
                    }else {
                    novoNo.next = Lista.head;
                Lista.head = novoNo;
        }
    }
}


class Node{
    Funcionario value;
    Node next;
    public Node(Funcionario value){
        this.value = value;
    }
}

public class ProjectoSalarios {


    public static void main(String[] args) throws IOException {

        String NomeFicheiro = "C:\\Users\\Nelson\\Documents\\NetBeansProjects\\ProjectoSalarios\\funcionarios.txt";
        String NomeFicheiro2 = "C:\\Users\\Nelson\\Documents\\NetBeansProjects\\ProjectoSalarios\\salarios.txt";

        Lista ListaFuncionario = new Lista();

        int ArraySalario[];
        ArraySalario = new int[15];

        try{
            File Ficheiro2 = new File(NomeFicheiro2);
            try (Scanner leitorFicheiro = new Scanner (Ficheiro2)) {

                while(leitorFicheiro.hasNextLine()){
                    String linha = leitorFicheiro.nextLine();
                    String dados[] = linha.split(":");
                    int Cargo = Integer.parseInt(dados[0]);
                    int Valor = Integer.parseInt(dados[1]);
                    ArraySalario[Cargo-1]= Valor;

                }
            }

        } catch (FileNotFoundException exception) {
            String mensagem = "Erro: o ficheiro " + NomeFicheiro2 + " não foi encontrado.";
            System.out.println(mensagem);
        }

        try{
            File Ficheiro = new File(NomeFicheiro);
            Scanner leitorFicheiro = new Scanner(Ficheiro);
            Funcionario NovoFuncionario;
            while(leitorFicheiro.hasNextLine()){
                String Linha = leitorFicheiro.nextLine();
                String dados[] = Linha.split(":");
                String Nome = dados[0];
                String BI = dados[1];
                int Departamento = Integer.parseInt(dados[2]);
                int Cargo = Integer.parseInt(dados[3]);


                NovoFuncionario = new Funcionario(Nome, BI, Departamento, Cargo, ArraySalario[Cargo-1]); 
                Lista.add(ListaFuncionario, NovoFuncionario);
            }
        }catch (FileNotFoundException exception) {
            String mensagem = "Erro: o ficheiro " + NomeFicheiro + " não foi encontrado.";
            System.out.println(mensagem);
        }

        try (BufferedWriter Writer = new BufferedWriter ( new FileWriter("C:\\Users\\Nelson\\Documents\\NetBeansProjects\\ProjectoSalarios\\output.txt"))) {
            Writer.write(ProcurarSalario(ListaFuncionario, "12312355"));
            Writer.write(SomaSalario(ListaFuncionario, 1));
            Writer.write(SomaSalarioCargo(ListaFuncionario, 1));
            Writer.write(SalarioAlto(ListaFuncionario, 1000));
            Writer.write(SalarioAltoPessoas(ListaFuncionario, 1000));
            Writer.write(SalarioBaixo(ListaFuncionario, 1000));
            Writer.write(SalarioBaixoPessoas(ListaFuncionario, 1000));
            Writer.write(SalarioAltoDepartamento(ListaFuncionario, 1));
            Writer.write(SalarioBaixoDepartamento(ListaFuncionario, 1));
            Writer.write(Duplicados(ListaFuncionario, "12312355"));
        }

    }

    static int ProcurarSalario(Lista Lista, String BI){ //1
        Node Copy = Lista.head;
        while(Copy!=null){
            if(Copy.value.BI.equals(BI)){
                return Copy.value.Salario;
            }
   Copy = Copy.next;
        }return 0;
    }

    static int SomaSalario(Lista Lista, int Departamento){ //2
        Node Copy = Lista.head;
        int Sum= 0;
        while(Copy !=null){
            if(Copy.value.Departamento == 10){
                Sum+=Copy.value.Salario;
            }
            Copy = Copy.next;
        }return Sum;
    }

    static int SomaSalarioCargo(Lista Lista, int Cargo){ //3
        Node Copy = Lista.head;
        int Sum = 0;
        while (Copy !=null){
            if(Copy.value.Cargo == 3){
                Sum+=Copy.value.Salario;
            }
            Copy = Copy.next;
        }return Sum;
    }

    static int SalarioAlto(Lista Lista, int Salario){ //4
        Node Copy = Lista.head;
        while(Copy !=null && Copy.next != null){ //CHECK HERE
            if(Copy.value.Salario < Copy.next.value.Salario ){
                Copy = Copy.next;
            }else{
                Copy.next = Copy.next.next;
            }
        }return Copy.value.Salario;
    }

    static int SalarioAltoPessoas(Lista Lista, int Salario){ //5
        Node Copy = Lista.head;
        int i = 1;
        while(Copy !=null && Copy.next != null){ //CHECK HERE
            if(Copy.value.Salario < Copy.next.value.Salario ){
                Copy = Copy.next;
                i=1;
            }else if(Copy.value.Salario > Copy.next.value.Salario){
                Copy.next = Copy.next.next;
            }else{
                i++;
                Copy.next = Copy.next.next;
            }
        }return i;
    }

    static int SalarioBaixo(Lista Lista, int Salario){ //6
        Node Copy = Lista.head;
        while(Copy !=null && Copy.next != null){ //CHECK HERE
            if(Copy.value.Salario > Copy.next.value.Salario ){
                Copy = Copy.next;
            }else{
                Copy.next = Copy.next.next;
            }
        }return Copy.value.Salario;
    }



    static int SalarioBaixoPessoas(Lista Lista, int Salario){ //7
        Node Copy = Lista.head;
        int i = 1;
        while(Copy !=null && Copy.next != null){ //CHECK HERE
            if(Copy.value.Salario > Copy.next.value.Salario ){
                Copy = Copy.next;
                i=1;
            }else if(Copy.value.Salario < Copy.next.value.Salario){
                Copy.next = Copy.next.next;
            }else{
                i++;
                Copy.next = Copy.next.next;
            }
        }return i;
    }

    static int SalarioAltoDepartamento(Lista Lista, int Departamento){ //8
        Node Copy = Lista.head;
        int departamento [] = new int [10];
        while(Copy!=null){
            departamento[Copy.value.Departamento-1] += Copy.value.Salario;
            Copy=Copy.next;
        }
        int Maximo=departamento[0];
        int Codigo=0;
        for(int i : departamento) {
            if(departamento [i] > Maximo){
                Maximo = departamento[i];
                Codigo=i+1;
            }
        }return Codigo;
    }

    static int SalarioBaixoDepartamento(Lista Lista, int Departamento){ //9
        Node Copy = Lista.head;
        int departamento [] = new int [10];
        while(Copy!=null){
            departamento[Copy.value.Departamento-1] += Copy.value.Salario;
            Copy=Copy.next;
        }

        int f=0;
        if(departamento[f] == 0){
            f++;
        }
        int Minimo = departamento[f];
        int Codigo=0;
        for(int i : departamento) {
            if(departamento [i] <= Minimo && departamento [i] != 0){
                Minimo = departamento[i];
                Codigo=i+1;
            }
        }return Codigo;
    }

    static String Duplicados(Lista Lista, String BI){
        Node Copy = Lista.head;
        String Escrita =BI;
        while(Copy!=null){
            if(!Copy.value.BI.equals(BI)){
                Copy = Copy.next;
            }else{
                Escrita += ";" + Copy.value.Nome ;
                Copy = Copy.next;
                }
            if(Escrita.equals(BI)){
                Escrita = "Nenhum";
            }
        }return Escrita;
    }





}

The files have the following information:

employees.txt:

Victor Valente:12312355:1:1
João Neves:12312777:10:1
Ivo Leite:12343119:10:2
Osvaldo Pires:12312765:2:1
Tiago Santos:12302050:2:2
Joana Cegripe:12999121:2:3
João Almeida:12340050:5:3
Rui Sebastião:12319101:10:3
Miguel Patrocínio:14233000:3:2
Raquel Cunhada:14244111:2:7
Luís Prima:12319101:1:1

salaries.txt

1:1000
2:1050
3:1200
4:1080
5:1700
6:1081
7:1700
8:1010
9:1910
10:890

I also add that debug only points out to me how my list was not filled in.

*Edit: Code updated and still not filling list, head and Tail continue null.

1 answer

0

Hello, welcome to you.

As it is a university job I will keep on some tips just:

1) File2 is covered, but is not added to your list.

2) File is then tried, but without closing the file2, it is possible that it is falling in the catch and not opening it.

3) Put more System.out.println(message) by your code when you get into a problem like this, it will help you understand the problem. Knowing where you’re going and the value of variables to know what’s working and where it’s failing. Or use the app’s own Debugger, but it’s important to know if it’s entering the catch and not opening the files, for example, to help you solve the problem.

Good job,

  • An answer to Rodrigo, "File2" is not supposed to be added to the list, this is a separate file to create an array of salaries for a simpler addition to the list later using the "Funcionario" class. When it comes to your second point, I was thinking about it to be honest but my code partner tried to do it but it went wrong so I should try to close "File 2" and then open "File" to see how to run. I also appreciate the tip from the end. :)

  • I inform you that even with the changes the list is still not done, head and Tail continue as null.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.