Error in bubble ordering

0

I'm doing a program where the names of students and three grades are recorded, then they have to sort their averages from lowest to highest (I chose the bubble / bubble sorting method) however I get the following error:

Grades.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");
              }
              }
   }
}
    
asked by Stephanie B Bautista 10.02.2017 в 04:05
source

3 answers

1

Let's start with the line where you define int promedio[] =new int[100]; , you should change it by int[] promedio = new int[100]; to remind yourself 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 you add the variable add instead ( add=add+1; ), so that you will always be writing in the same position of the matrices. I change everything for add and, in addition, I start with 0 and increase at the end of the data entry.

Be careful with integer arithmetic. Inside the test loops you are using the comparison if(calificacion[a]>=11) and then on the outside you use }while(calificacion[a]>10 || calificacion[a]<1); . It is equivalent in integers, but if someday you apply floating point arithmetic (to implement decimals) you will find strange behaviors (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 on both sites so that you get a message in case of a negative value (as you have it would not).

The first block do { seems to be not open.

The second and third qualification is stored in segunda[a]=entrada.nextInt(); but then you check the limits entered by referring to the previous matrix, calificacion ().

For the block of the second qualification you do not open the key of do { , but you close it, so you unpack the structure of the code.

By entering the third qualification, you immediately calculate the average, instead of waiting for the limits to be checked, plus you open the do { later. I have changed it to the right place.

In option 2 you have a closed loop immediately 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 which you must go looking for the elements to sort, when the number of elements in the matrices is stored in add (formerly a hodgepodge between a a add ). As promedio is an array, it generates that error you suffer .

You put an exit message after the execution of the output System.exit(0) , so it will never be displayed.

The outer loop do { that checks the menu and repeats it in case of not leaving, since you do not use the value of menu_salida you should put a while (true); (in fact it gives an error for not having anything).

Line nombre[y+1] = auxnombre; is wrong if you define auxnombre as integer. You must change it by String auxnombre; .

Your fixed code would be (see, you have not done anything to show the result of the order):

import java.io.*;
import java.util.*;

public class Calificaciones {
   public static void main (String[] args) {
      Scanner entrada = new Scanner(System.in);
      Scanner sc = new Scanner(System.in);
      Scanner entrada2 = new Scanner(System.in);

      String[] nombre = new String[100];
      String auxnombre;
      int opcion_menu=0, ejecutar=0, salir=0;
      int passed = 0, flunked = 0;

      int promedio[] =new int[100];
      int otro = 0, auxpromedio;
      int a = 0, x = 0, add = 1, 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");
        System.out.println("[1]. Captura");
        System.out.println("[2]. Consulta");
        System.out.println("[3]. Salida ");
        opcion_menu = entrada.nextInt();
        switch (opcion_menu) {
        case 1:
          System.out.println("Ingresa el nombre del alumno:");
          nombre[add] = sc.nextLine().toUpperCase();
          do {
            do {
              System.out.println("Ingresa la calificacion del primer parcial:");
              calificacion[add] = entrada.nextInt();
              if(calificacion[add] < 1 || calificacion[add] > 10) {
                System.out.println("ERROR \nIngresa un valor no mayor de 10");
              }
            } while (calificacion[add] > 10 || calificacion[add] < 1);
            do {
              System.out.println("Ingresa la calificacion del segundo parcial");
              segunda[add] = entrada.nextInt();
              if (segunda[add] < 1 || segunda[add] > 10) {
                System.out.println("ERROR \nIngresa un valor no mayor de 10");
              }
            } while (segunda[add] > 10 || segunda[add] < 1);
            do {
              System.out.println("Ingresa la calificacion del tercer parcial");
              tercera[add]=entrada.nextInt();
              if (tercera[add] < 1 || tercera[add] > 10) {
                System.out.println("ERROR \nIngresa un valor no mayor de 10");
              }
            } while (tercera[add] < 1 || tercera[add] > 10);
            promedio[x]=(calificacion[x]+segunda[x]+tercera[x])/3;
            add++;
            System.out.println("Deseas ingresar otras calificaciones? \n 1.Si \n 2.No");
            otro=entrada.nextInt();
          } while (otro == 1);
          break;
        case 2:
          System.out.println("Consultas de promedios");
          for (int i = 0; i < add; i++) {
            for (y = 1; y < add; 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;
              }
            }
          }
          break;
        case 3:
          System.out.println("¿Desea salir del programa? 1. Si 2. No");
          salir = entrada.nextInt();
          if (salir == 1) {
            System.out.println("Gracias por usar el programa");
            System.exit(0);
          }
          break;
        default:
          System.out.println("Favor de ingresar otra opcion del 1 al 3");
        }
      } while (true);
   }
}
    
answered by 10.02.2017 / 09:33
source
1

I checked your code and it has syntax problems since you declare the following:

    for(int i=1; i<promedio; i++)

It should be like this:

    for (int i = 1; i < promedio.length; i++) 

Your resulting code should look like this:

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;
        String 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.length; i++) {
                for (y = 1; y < promedio.length; 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");
            }
        }
    }
}
    
answered by 10.02.2017 в 04:29
0

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 decoupled from the original logic, both be maintainable and your code will be clearer for you and for another who sees it. Good luck!

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class Test {

    public static void main(String[] args) {

        // se construye la lista en el mismo orden que se agregan los estudiantes: Harold, Daniel y Victor
        List<Student> students = buildStudents();
        System.out.println(students);
        // se ordena la lista por promedio de menor a mayor: Victor, Harold y Daniel
        Collections.sort(students, new QualificationComparator());
        System.out.println(students);
    }

    private static List<Student> buildStudents() {

        List<Student> studens = new ArrayList<Student>();

        Student student = new Student();
        student.setName("Harold");
        student.setQualifications(Arrays.asList(98.2d, 75.34d, 64.0d));
        studens.add(student);

        student = new Student();
        student.setName("Daniel");
        student.setQualifications(Arrays.asList(100.0d, 87.5d, 92.1d));
        studens.add(student);

        student = new Student();
        student.setName("Victor");
        student.setQualifications(Arrays.asList(81.3d, 62.9d, 43.54d));
        studens.add(student);

        return studens;
    }

    static class Student {

        private String name;

        private List<Double> qualifications;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public List<Double> getQualifications() {
            return qualifications;
        }

        public void setQualifications(List<Double> qualifications) {
            this.qualifications = qualifications;
        }

        @Override
        public String toString() {
            return "Student{" +
                    "name='" + name + '\'' +
                    '}';
        }

    }

    static class QualificationComparator implements Comparator<Student> {

        @Override
        public int compare(Student student, Student studentToCompare) {

            Double average = calculateAverage(student.getQualifications());
            Double averageToCompare = calculateAverage(studentToCompare.getQualifications());

            return average.compareTo(averageToCompare);
        }

        private Double calculateAverage(List<Double> qualifications) {

            // usamos BigDecimal para no perder precision
            BigDecimal result = new BigDecimal(0d);

            for (Double qualification : qualifications) {
                result = result.add(new BigDecimal(qualification));
            }

            BigDecimal length = new BigDecimal(qualifications.size());
            // dividimos y redondeamos hacia arriba
            BigDecimal average = result.divide(length, RoundingMode.CEILING);

            return average.doubleValue();
        }

    }

}
    
answered by 13.02.2017 в 15:40