I am making a program where the names of the students and three grades are registered, then their averages have to be ordered from lowest to highest (I chose the bubble/bubble ordering method), however I get the following error:
Ratings.java:90: error: bad operand types for binary operator '<' for(int i=1; i
import java.io.*;
import javax.swing.*;
import java.awt.*;
import java.util.*;
public class Calificaciones{
public static void main (String[]args){
Scanner entrada;
entrada=new Scanner(System.in);
Scanner sc=new Scanner(System.in);
Scanner entrada2;
entrada2=new Scanner(System.in);
String[]nombre= new String[100];
int opcion_menu=0; int ejecutar=0;
int salir=0; int passed;
int flunked;
int promedio[]=new int[100];
int otro=0;
int auxpromedio;
int auxnombre;
int a=0;
int x=0;
int add=1;
int y=0;
int calificacion[]= new int[100];
int segunda[]=new int[100];
int tercera[]=new int[100];
do{
System.out.println("Elija la opcion deseada \n [1].Captura \n [2].Consulta \n [3].Salida ");
opcion_menu=entrada.nextInt();
if(opcion_menu>3)
{
System.out.println("Favor de ingresar otra opcion del 1 al 3");
}
}while(opcion_menu>3 || opcion_menu<1);
do{
if(opcion_menu==1)
{
System.out.println("Ingresa el nombre del alumno:");
nombre[a]=sc.nextLine().toUpperCase();
add=add+1;
do{
System.out.println("Ingresa la calificacion del primer parcial:");
calificacion[a]=entrada.nextInt();
if(calificacion[a]>=11)
{
System.out.println("ERROR \nIngresa un valor no mayor de 10");
}
}while(calificacion[a]>10 || calificacion[a]<1);
System.out.println("Ingresa la calificacion del segundo parcial");
segunda[a]=entrada.nextInt();
if(calificacion[a]>=11)
{
System.out.println("ERROR \nIngresa un valor no mayor de 10");
}
}while(calificacion[a]>10 || calificacion[a]<1);
System.out.println("Ingresa la calificacion del tercer parcial");
tercera[a]=entrada.nextInt();
promedio[x]=(calificacion[x]+segunda[x]+tercera[x])/3;
do{
if(calificacion[a]>=11)
{
System.out.println("ERROR \nIngresa un valor no mayor de 10");
}
}while(calificacion[a]>10 || calificacion[a]<1);
System.out.println("Deseas ingresar otras calificaciones? \n 1.Si \n 2.No");
otro=entrada.nextInt();
}while(otro==1);
if(opcion_menu==2){
System.out.println("Consultas de promedios");
for(int i=1; i<promedio; i++)
{
for(y=1; y<promedio; y++);
if(promedio[y]>promedio[y+1])
{
auxpromedio=promedio[y];
promedio[y]=promedio[y+1];
promedio[y+1]=auxpromedio;
auxnombre=nombre[y];
nombre[y]=nombre[y+1];
nombre[y+1]=auxnombre;
}
}
}
if(opcion_menu==3)
{
System.out.println("¿Desea salir del programa? 1. Si 2. No");
salir=entrada.nextInt();
if(salir==1){
System.exit(0);
System.out.println("Gracias por usar el programa");
}
}
}
}
I checked your code and it has syntax problems as you declare the following:
It should be like this:
Your resulting code should look like this:
Let's start with the line where you define
int promedio[] =new int[100];
, you should change it toint[] promedio = new int[100];
to remind you that it is an array of integers as soon as you see the definition.We continue with the data entry, where you enter everything in the index
a
(nombre[a] = sc.nextLine().toUpperCase()
) but add the variableadd
in its place (add=add+1;
), so that you will always be writing in the same position of the arrays. I change all toadd
and also start with0
and increment at the end of the data entry.Be careful with integer arithmetic. Inside the check loops you are using compare
if(calificacion[a]>=11)
and then outside you use}while(calificacion[a]>10 || calificacion[a]<1);
. It is equivalent in integers, but if you ever apply floating point arithmetic (to implement decimals) you will find strange behavior (such as being able to enter grades between 10 and 11, such as 10.5). In addition, I recommend you to put exactly the same check in both places so that you get a message in case of a negative value (as you have it, I would not do it).The first block
do {
doesn't seem to be open.The second and third qualifications are stored in
segunda[a]=entrada.nextInt();
but then you check the bounds entered by referencing the previous array,calificacion
().For the second qualification block you don't open the brace
do {
, but you close it, so you mess up the structure of the code.Entering the third grade immediately calculates the average, instead of waiting for the bounds check, plus you open the
do {
later. I have changed it to the right place.In option 2 you have an immediately closed loop
for(y=1; y<promedio; y++);
that prevents the cyclic execution of the next block{}
.On the other hand, in both loops you are using
promedio
as a variable to get to looking for the elements to sort, when the number of elements in the arrays is stored inadd
(previously a hodgepodge betweena
aadd
). Sincepromedio
it is an array, it generates that error that you suffer from .You put a post-execution exit message to exit
System.exit(0)
, so it will never be displayed.The outer loop
do {
that checks the menu and repeats it if it doesn't exit, since you don't use the value ofmenu_salida
you should put awhile (true);
(in fact it gives an error because it doesn't have anything).The line
nombre[y+1] = auxnombre;
is wrong if you defineauxnombre
as integer. You must change it toString auxnombre;
.Your fixed code would be (note, you haven't done anything to display the sort result):
In Java I would use the interface
Comparator
where you delegate the task of comparing to a different object, this will make the ordering logic decouple from the original logic, both are maintainable and your code is clearer for you and for others who see it. Luck!!!