How to create string vectors in java?

4
  

"Make an algorithm to receive an integer n (number of students), an integer m (number of subjects), and nxm grades 0 to 10, which each student obtained in each discipline.      

a) which (s) of course (s) students scored highest on average?   b) which (or which) subject (s) did the students score lower on average?   c) which student (s) obtained the highest overall average   d) which student (s) had lower overall average "

I thought about creating a string for the names of the students, another for the disciplines and then creating the matrix with the notes. Only then would I think of a way to calculate the average. From what I understand, I must print on the screen the names of the disciplines that correspond to the largest and smallest means; the same goes for the students. But I'm not able to create such strings ...

    public static void main(String[] args) {
        Scanner ent = new Scanner(System.in);
        ent.useLocale(Locale.US);
        int n, m, i, j;;
        n = ent.nextInt(); // numero de alunos
        m = ent.nextInt(); // numero de disciplinas            
        String [] a = new String[n]; // nomes dos alunos
        String [] d = new String[m]; // nomes das disciplinas
        double [][] M = new double[n][m]; // notas
        // nomes dos alunos
        for (i=0; i<n; i++) {
            a[i] = ent.nextLine();
        }
        // nomes das disciplinas
        for (j=0; j<m; j++) {
            d[i] = ent.nextLine();
        }
        // monta tabela de notas: alunos X disciplinas
        for (i=0; i<n; i++) {
            for (j=0; j<m; j++) {
                M[i][j] = ent.nextDouble();
            }            
        }
        // calcula a nota média de cada disciplina
        double soma=0, media=0;
        for (j=0; j<m; j++) {
            for (i=0; i<n; i++) {
                soma = soma + M[i][j];
            }
            media = soma/n;
            System.out.println("Média de "+d[j]+": "+media);
        }
    }

When I run the program and type the name of the first student or type all and then I give enter, the following message appears:

  

Exception in thread "main" java.util.InputMismatchException at
  java.util.Scanner.throwFor (Scanner.java:909) at
  java.util.Scanner.next (Scanner.java:1530) at
  java.util.Scanner.nextInt (Scanner.java:2160) at
  java.util.Scanner.nextInt (Scanner.java:2119) at
  aula11.teste.main (teste.java:8) Java Result: 1

I do not know what to say or what I should do to correct the error.

    
asked by anonymous 29.11.2014 / 20:45

1 answer

9

First, look at this part:

    for (j=0; j<m; j++) {
        d[i] = ent.nextLine();
    }

You iterate the variable j , but access the array using i . This is wrong. To avoid errors like this, it is advisable to declare the variable in for itself, like this:

    for (int j=0; j<m; j++) {
        d[j] = ent.nextLine();
    }

And so, you no longer have to declare i and j out of the loop. This has the advantage that if you use the wrong variable, the compiler is more likely to find the error for you.

Continuing back to your original problem, this is your 8 line:

n = ent.nextInt(); // numero de alunos

That is, are you sure that you actually typed an integer, not a decimal number or the name of any student or discipline?

To avoid these types of problems, I recommend putting this in the program, outside the main method, it can be before or after:

private static int lerInt(String mensagem, Scanner scanner) {
    while (true) {
        System.out.println(mensagem);
        String lido = scanner.nextLine().trim();
        try {
            return Integer.parseInt(lido);
        } catch (NumberFormatException e) {
            System.out.println("Desculpe, mas " + lido + " não é um número inteiro. Tente novamente.");
        }
    }
}

private static double lerDouble(String mensagem, Scanner scanner) {
    while (true) {
        System.out.println(mensagem);
        String lido = scanner.nextLine().trim();
        try {
            return Double.parseDouble(lido);
        } catch (NumberFormatException e) {
            System.out.println("Desculpe, mas " + lido + " não é um número real. Tente novamente.");
        }
    }
}

private static String lerString(String mensagem, Scanner scanner) {
    while (true) {
        System.out.println(mensagem);
        String lido = scanner.nextLine().trim();
        if (!lido.isEmpty()) return lido;
        System.out.println("Desculpe, você não digitou nada. Tente novamente.");
    }
}

And then you use them this way:

int n = lerInt("Digite o numero de alunos", ent);
int m = lerInt("Digite o numero de disciplinas", ent);

...

// nomes dos alunos
for (int i=0; i<n; i++) {
    a[i] = lerString("Digite o nome do " + (i + 1) + "o aluno.", ent);
}
// nomes das disciplinas
for (int j=0; j<n; j++) {
    d[j] = lerString("Digite o nome da " + (j + 1) + "a disciplina.", ent);
}

...

for (int i=0; i<n; i++) {
    for (int j=0; j<m; j++) {
        M[i][j] = lerDouble("Digite a nota de " + d[j] + " do aluno " + a[i] + ".", ent);
    }
}

And you can delete these lines:

    ent.useLocale(Locale.US);
    int n, m, i, j;;

There is also a problem in your average calculation. Instead:

    double soma=0, media=0;
    for (j=0; j<m; j++) {
        for (i=0; i<n; i++) {

Use this:

    for (int j=0; j<m; j++) {
        double soma=0, media;
        for (int i=0; i<n; i++) {

The reason is that the sum has to go back to zero when you change discipline.

    
29.11.2014 / 21:42