"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.