-4

My code is posted below. I'm generating random numerical grades and assigning them letter grades. Will then create a client class to test the program. My issue is towards the end of the program. I'm getting an error saying illegal start of expression. Maybe I just need a fresh set of eyes to look at this. I can't seem to figure it out. Thanks!

import java.util.Random;

public class Convertgrade
{
     private int numberOfStudents;

     private int[] studentGrades = new int[100];


     public Convertgrade()
     {
            numberOfStudents = 0;
     }

      //one arg constructor, sets numberOfStudents = students
      public Convertgrade(int students)

      {
             numberOfStudents = students;
             setGrades(students);
      }

      //mutator
      public void setStudents(int students)
      {
            if(students < 0)
            {
                       numberOfStudents = 0;
             }
             else
            {
                       numberOfStudents = students;
                       setGrades(students);
              }
      }

        //accessor
       public int getStudents()

       {
       return numberOfStudents;
        }

       public void setGrades(int students)
       {
              Random randomNumber = new Random(); 
              for(int count = 0; count < students; count++)
              {
                    studentGrades[count] = randomNumber.nextInt(101);
                    }
               }

          public char[] getLetterGrades()
          {
                   char[] letterGrades = new char[numberOfStudents];
                   for(int count = 0; count < numberOfStudents; count++)
                   {
                           if(studentGrades[count] <= 59)
                           letterGrades[count] = 'F';

                           else if(studentGrades[count] <= 69)
                           letterGrades[count] = 'D';

                           else if(studentGrades[count] <= 79)
                           letterGrades[count] = 'C';

                           else if(studentGrades[count] <= 89)
                           letterGrades[count] = 'B';

                           else if(studentGrades[count] <= 100)
                           letterGrades[count] = 'A';
                            }
                       return letterGrades;
                       }

           public int getAs()

           {

                      int numberOfAs = 0;

                      for(int count = 0; count < numberOfStudents; count++)

                      {

                                if(studentGrades[count] >= 90)

                                numberOfAs++;

                       }
                  return numberOfAs;
             }

           public int[] getNumberOfEachGrade()

          {

                 int[] numberOfGrades = new int[numberOfStudents];

                 for(int count = 0; count < numberOfStudents; count++)

                  {

                        if(studentGrades[count] <= 59)

                        numberOfGrades[count] ++;

                        else if(studentGrades[count] <= 69)

                        numberOfGrades[count] ++;

                        else if(studentGrades[count] <= 79)

                        numberOfGrades[count] ++;

                        else if(studentGrades[count] <= 89)

                        numberOfGrades[count] ++;



                        else if(studentGrades[count] <= 100)

                        numberOfGrades[count] ++;

                 }

              return numberOfGrades;

             }



            public String toString()

            {

                  System.out.format("The number of students = %d\n", numberOfStudents);

                  for(int count = 0; count < numberOfStudents; count++)

                  {

                           System.out.format("Grade %d is %d\n", count + 1, studentGrades[count]);

                  }

            return String.format("\n");

             public void sort_array(int[] array); 
             //GETTING ERRORS HERE^: ILLEGAL START OF EXPRESSION

            {

                    int intTemp;

                    int count = 0;

                    int index = 0;

                    for(count = 0; count < (array.length - 1); count++)

                    {

                         for(index = 0; index < (array.length - 1); index++)

                         {

                                if(array[index] > array[index +1])

                                {

                                        intTemp = array[index +1];

                                        array[index +1] = array[index];

                                        array[index] = intTemp;

                                    }

                           }

                     }

               }

      }

}
Ataur Rahman Munna
  • 3,887
  • 1
  • 23
  • 34
Ali Raza
  • 5
  • 1

3 Answers3

2

I veeery strongly suggest you use an IDE, like Eclipse or IntelliJ. Or anything that can help you maintain a healthy code style, like Sublime Text with some Java code style package.

Anything would do!


You have 3 problems:

  1. a missing closing bracket (}) line 149 (roughly)
  2. an extra semicolon line 150 and
  3. one too many brackets at the very end of your file.

I cleaned up your code for you this time, but it's just typing issues that I hope you can find a way to avoid in the future :).

import java.util.Random;

public class Convertgrade {

    private int numberOfStudents;
    private final int[] studentGrades = new int[100];

    public Convertgrade() {
        this.numberOfStudents = 0;
    }

    // one arg constructor, sets numberOfStudents = students
    public Convertgrade(final int students) {
        this.numberOfStudents = students;
        setGrades(students);
    }

    // mutator
    public void setStudents(final int students) {
        if (students < 0) {
            this.numberOfStudents = 0;
        } else {
            this.numberOfStudents = students;
            setGrades(students);
        }
    }

    // accessor
    public int getStudents() {
        return this.numberOfStudents;
    }

    public void setGrades(final int students) {
        final Random randomNumber = new Random();
        for (int count = 0; count < students; count++) {
            this.studentGrades[count] = randomNumber.nextInt(101);
        }
    }

    public char[] getLetterGrades() {
        final char[] letterGrades = new char[this.numberOfStudents];
        for (int count = 0; count < this.numberOfStudents; count++) {
            if (this.studentGrades[count] <= 59) {
                letterGrades[count] = 'F';
            } else if (this.studentGrades[count] <= 69) {
                letterGrades[count] = 'D';
            } else if (this.studentGrades[count] <= 79) {
                letterGrades[count] = 'C';
            } else if (this.studentGrades[count] <= 89) {
                letterGrades[count] = 'B';
            } else if (this.studentGrades[count] <= 100) {
                letterGrades[count] = 'A';
            }
        }

        return letterGrades;
    }

    public int getAs() {
        int numberOfAs = 0;
        for (int count = 0; count < this.numberOfStudents; count++) {
            if (this.studentGrades[count] >= 90) {
                numberOfAs++;
            }
        }

        return numberOfAs;
    }

    public int[] getNumberOfEachGrade() {
        final int[] numberOfGrades = new int[this.numberOfStudents];
        for (int count = 0; count < this.numberOfStudents; count++) {
            if (this.studentGrades[count] <= 59) {
                numberOfGrades[count]++;
            } else if (this.studentGrades[count] <= 69) {
                numberOfGrades[count]++;
            } else if (this.studentGrades[count] <= 79) {
                numberOfGrades[count]++;
            } else if (this.studentGrades[count] <= 89) {
                numberOfGrades[count]++;
            } else if (this.studentGrades[count] <= 100) {
                numberOfGrades[count]++;
            }
        }

        return numberOfGrades;

    }

    @Override
    public String toString() {
        System.out.format("The number of students = %d\n", this.numberOfStudents);
        for (int count = 0; count < this.numberOfStudents; count++) {
            System.out.format("Grade %d is %d\n", count + 1, this.studentGrades[count]);
        }

        return String.format("\n");
    }

    public void sort_array(final int[] array) {
        int intTemp;
        int count = 0;
        int index = 0;
        for (count = 0; count < (array.length - 1); count++) {
            for (index = 0; index < (array.length - 1); index++) {
                if (array[index] > array[index + 1]) {
                    intTemp = array[index + 1];
                    array[index + 1] = array[index];
                    array[index] = intTemp;
                }
            }
        }
    }
}
ccjmne
  • 9,333
  • 3
  • 47
  • 62
0

You have some braces {} and semi-colons in the wrong place.

Your indentation looks a bit messed up too - maybe it's just how you copied the code when making this post, but being careful with your indentation can make errors like these more obvious.

  • You have a semi-colon after 'public void sort_array(int[] array)' which shouldn't be there
  • You have a missing closing brace } immediately before this
  • You have an extra closing brace at the end of the file
CAW
  • 369
  • 1
  • 8
0

Replace this part:

        return String.format("\n");

         public void sort_array(int[] array); 
         //GETTING ERRORS HERE^: ILLEGAL START OF EXPRESSION

By this:

    return String.format("\n");
}

public void sort_array(int[] array) {
    // OMG THERE ISN'T ERRORS ANYMORE :)

Next time you can tell there's something strange if you see such "return" statements hanging in nowhere...

Zoette
  • 1,241
  • 2
  • 18
  • 49