1

I am new to coding so bear with me. In my code, I am getting an illegal start of type error in my for loop (located in the student class). How can I fix this error?

In my for loop, I want to have my array grades have 10 values (which is already defined), and these values will be random from 60 to 99. In my opinion, I think that part is correct, and throughout I have used correct methodology. So if anybody can see where I have gone wrong I would be very grateful :)

import javax.swing.*;

public class StudentBase
{
  public static void main(String[] args){
      StudentBase app = new StudentBase();
      app.go();
    }

    void go(){
        //this is the main routine
         JOptionPane.showMessageDialog(null, "Welcome!" + '\n');

         String n = JOptionPane.showInputDialog("Type in your name");
         JOptionPane.showMessageDialog(null, "Press ok if your name is" + " " +  n);

         String h = JOptionPane.showInputDialog("What is your height? (in meters: eg - 1.80)");
         JOptionPane.showMessageDialog(null, "Press ok if your height is" + " " + h + " " + "meters");

         JOptionPane.showMessageDialog(null, "Introduction to student class:" + " " + "Hello! My name is" + " " + n + " " + "and I am" + " " + h + " " + "meters tall.");

         String students[] = new String [20];
         JOptionPane.showMessageDialog(null, students);
    }
}

//this is where Student Class is defined

class Student{
    //add variables, constructors and methods here
    String name = "";
    String height = "";
    int grades [] =new int [10];

  for(i = 0; i < grades.length; i++)
    {
        grades[i] = rand(60,99);
    }


    public Student(String n, String h, int g[])
    {
        name = n;
        height = h;
        grades = g;

    }

}
  • 4
    Your code should be inside some method. You can't have a for loop directly in the class level. – Eran Mar 14 '16 at 14:55
  • most likely, by correcting your code. you 've forgotten a bracket somewhere, or misplaced it (or another symbol). it would be easier to answer if you showed where the error occurs – Stultuske Mar 14 '16 at 14:55

1 Answers1

0

The issue that is causing that specific error is that you're trying to run a for-loop without it being part of any method or constructor. You have to encompass it first. Also, you need to declare i's type in your loop, otherwise you'll get a different error as well.

class Student{
    //add variables, constructors and methods here
    String name = "";
    String height = "";
    int grades [] =new int [10];

    public void initGrades()
    {
        for(int i = 0; i < grades.length; i++)
        {
            grades[i] = rand(60,99);
        }
    }


    public Student(String n, String h, int g[])
    {
        name = n;
        height = h;
        grades = g;
        initGrades();
    }

}
Mage Xy
  • 1,803
  • 30
  • 36
  • Now it gives me an error of "cannot find symbol - method rand(int,int)" what should I do to correct this? – Ileia Spyropoulos Mar 14 '16 at 15:24
  • The code I gave you is just a snippet that corrects the syntactical problems in the code you provided. I assumed you had `rand()` defined somewhere else. The error you're getting now very clearly explains what the issue is: There is no visible method called `rand` defined anywhere in your code. Specifically, you need to define it somewhere in the `Student` class. – Mage Xy Mar 14 '16 at 15:58
  • Sorry to ask this (it will probably sound stupid) but to do so, do I need to add this: import java.util.Random; ? Or is there another way to define it? – Ileia Spyropoulos Mar 14 '16 at 17:03
  • You can either define your own rand method in the Student class, or you can import java.util.Random and use Random's `nextInt` method. Or you could combine the two approaches - make a method named `rand` that creates a `java.util.Random` object and uses it to return a random integer. If you need more clarification, you should ask another question, since this is not really part of your original question. – Mage Xy Mar 14 '16 at 17:16
  • Before you do post a new question, though, you can take a look at [this question](http://stackoverflow.com/questions/363681/generating-random-integers-in-a-specific-range), which should give plenty of detail on how to accomplish what it looks like you're trying to do. – Mage Xy Mar 14 '16 at 17:17