Hi all, Im having issues with this program. I've looked online and seen others that have basically the same program but have issues also. My issue is a compiling error, thats one problem thats obvious to me. This program is suppose to get the user to enter grades for five different courses and and ten different students. My compiling error is: InputGrades.java:26: illegal start of type
for (int s = 1; s <= 10; s++)
I have been working on this for the past 5 hours trying to make sense of what i have seen online and what my issues are. I have other classes that i will include if needed but for now i will just include my InputGrades class:

import java.io.*;
  public class InputGrades {
  private int idGrade;
  private CollegeCourse[] courses = new CollegeCourse[5];
  public InputGrades(int gradeID)
  {
        idGrade = gradeID;
        courses[0] = new CollegeCourse('A', "CIS 210", 3);
        courses[1] = new CollegeCourse('B', "CIS 211", 2);
        courses[2] = new CollegeCourse('C', "CIS 212", 2);
        courses[3] = new CollegeCourse('D', "CIS 213", 3);
        courses[4] = new CollegeCourse('F', "CIS 214", 1);
   }
public static void main(String[] args) throws IOException
{
       InputGrades inputgrades = new InputGrades(1);

          char value;
          boolean match = false;
          BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
            }
            public int getCourse() 
            {
                  return courses[i];
            }
          for (int s = 1; s <= 10; s++) 
             {
               System.out.println("Enter ID for student#: " + s);
             }
          for (int n = 1; n <= 5; n++) 
             {
               System.out.print("\tEnter grade for course ID#" + n + ": " 
                    + inputGrades.courses[x].getCourseID());
               value = stdin.readLine().charAt(0);
          if ((value == 'A') || (value == 'B') || (value == 'C')
                || (value == 'D') || (value == 'F')) 
                 {
                   match = false;
             } 
                 else 
                 {
             System.out.println("Please re-enter the grade");
             match = true;
             --n;
             }
            }
          }
     }
 }

I would very much appreciate any help and an explanation of anything that might need it.

Thanks much,
Sam

Look at your brackets. You can't put a for loop in the middle of a class definition. It has to be inside of a method. On line 21, you put a '}' that closes your main method. Then you defined a method "getCourse()" and afterwards, you have a bunch of code that is not inside of any particular method.

The getCourse method was a last guess effort kind of thing that I seen online else where. This is one of the most difficult and in depth program I have tried to do so its a lot to try to comprehend. Is the getCourse needed if I were to take the } off line 21 as the code after the getCourse would then belong to the main method? Is that right or am I way off?

Thanks,
Sam

You cannot define methods inside of other methods. Any code, other than variable declarations, needs to be inside of a method. All you need to do (to make your code compile) is fix your brackets so that your code follows the rules I just mentioned. Making your code run correctly is a different issue. Once your code compiles correctly, repost it and explain *in detail* what problems you are having.

After glancing at your code, I noticed that you defined getCourse() to return an integer, but within the method, you returned an object reference. Your code won't compile until you fix that and your bracket issue. One advice is whenever you start a new new block of statements(ie while, if, for, etc.) put a bracket and a close bracket immediately so that you know you will not get this kind of error again.

Hi Red999 i had actually seen that done online where someone else was trying to get help on this same program. The person that was helping said that they needed to have that in there so thats why i added it in there. It was actually formated as

public getCourse(int i) 
{
  return courses[i];
}

but the compiler told me it was an illegal start of expression, so i changed it to the way i have it now. Im not really sure where the "i" that it was returning comes from. I know i need a int but how should i do that, do i make another int variable, can i use one that i already have? I took out the closing bracket, that i actually put in there when i was having compiling issues to try to fix it, that was my fault. Thanks for the help.

Sam

Word of advice. Instead of trying to find ways around problems that occur, Try to find the reason/root of the errors.


Apart from the obvious error of everything underneath the main method floating in space, Why do you have 5 closing braces at the bottom of your code? You only need 3(even though its completely wrong, learning to manage your braces is a basic skill and you should start). One too close the class brace, 1 to close the for loop, and 1 to close the else

Edited 6 Years Ago by Akill10: n/a

I just never took those braces back out as ive been adding and subtracting things to try to make this work and adding opening braces to the code and then had to add a closing, but when i took out the opening the closing never came back out. I am trying to find the cause of the error, i've spent a lot of time on this program, a lot of time by myself trying to find it out and researching online about the errors but im slightly over my head on this one, i just started trying to learn arrays and dont fully understand them, thats why i was hoping to get help here. I dont just want an answer, im hoping that any hints or if an answer that it will be explained why it must be that way. Im trying to learn my best and appreciate everyones patience while im learning. What do you mean that it is just floating? I will include what i have changed in the past few days so its updated for everyone to see:

import java.io.*;
  public class InputGrades {
  private int idGrade;
  private CollegeCourse[] courses = new CollegeCourse[5];
  public InputGrades(int gradeID)
  {
        idGrade = gradeID;
        courses[0] = new CollegeCourse('A', "CIS 210", 3);
        courses[1] = new CollegeCourse('B', "CIS 211", 2);
        courses[2] = new CollegeCourse('C', "CIS 212", 2);
        courses[3] = new CollegeCourse('D', "CIS 213", 3);
        courses[4] = new CollegeCourse('F', "CIS 214", 1);
   }
public static void main(String[] args) throws IOException
{
       InputGrades inputgrades = new InputGrades(1);

          char value;
          boolean match = false;
          BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
            
             public int getCourse() 
             {
               return courses[];
             }
             for (int s = 1; s <= 10; s++) 
             {
               System.out.println("Enter ID for student#: " + s);
             }
          for (int n = 1; n <= 5; n++) 
             {
               System.out.print("\tEnter grade for course ID#" + n + ": " 
                    + inputGrades.courses[x].getCourseID());
               value = stdin.readLine().charAt(0);
          if ((value == 'A') || (value == 'B') || (value == 'C')
                || (value == 'D') || (value == 'F')) 
                 {
                   match = false;
             } 
                 else 
                 {
             System.out.println("Please re-enter the grade");
             match = true;
             --n;
             }
            }
          
     }
 }

Thanks,
Sam

Edited 6 Years Ago by samuel17: n/a

This article has been dead for over six months. Start a new discussion instead.