0

I've created all of my objects in my class "student" by hand and really just need to use an array to put the names, grades and ID numbers into. When I try, it gives me an error about incompatible types. I understand it's probably due to a restriction on some sort of syntax type of error. Could anyone help with the physical grammar of this portion please? It would be much appreciated!

package bilak_sackin_assignment_7;

public class Bilak_Sackin_Assignment_7 {

  public static void main(String[] args) {
    student Frank = new student("Frank", 95.2, 11231);

    student Billy = new student("Billy", 65.6, 656454);

    student Alex = new student("Alex", 65.2, 123456);

    student Miranda = new student("Miranda", 80.0, 963852);

    student Joel = new student("Joel", 89.9, 486248);

    Frank.display();
    Billy.display();
    Alex.display();
    Miranda.display();
    Joel.display();
  }
}

class student {

  String[] name = new String[5];
  double[] grade = new double[5];
  int[] ID = new int[5];

  public student(String[] n, double[] g, int[] d) {
    name = n;
    grade = g;
    ID = d;
  }

  public void display() {
    for (int i = 0; i < 4; i++) {
      System.out.println("Names: " + name[i]);
      System.out.println("Grades: " + grade[i]);
      System.out.println("Student ID: " + ID[i]);
    }
  }
}
5
  • 2
    Student takes a String[] as its first parameters; you are passing a String. Either pass e.g. new String[] { "Miranda" } instead of "Miranda", or change the type of the parameter to String. Commented May 4, 2017 at 19:39
  • 3
    Aside from anything else, now would be a very good time to learn about - and start following - Java naming conventions, and learning to use meaningful names everywhere you can. Commented May 4, 2017 at 19:40
  • Adding to what JonSkeet says, learn to indent your code too. Commented May 4, 2017 at 19:41
  • Adding to what AndyTurmer and JonSkeet said, take a look at capsulation... Commented May 4, 2017 at 19:44
  • thank you @AndyTurner, that seems to make sense. I'm trying to play around with an array to make it simpler and easier to write out code, as opposed to having to make changes to each object individually Commented May 4, 2017 at 19:52

4 Answers 4

1

I'm quite new to this too, but surely you're not needing arrays, you're creating objects. Remove all your [ and ], like this -

public static void main(String[] args) {
        student Frank = new student("Frank",95.2,11231);

        student Billy = new student("Billy", 65.6, 656454);

        student Alex = new student("Alex", 65.2, 123456);

        student Miranda = new student("Miranda", 80.0, 963852);

        student Joel = new student("Joel", 89.9, 486248);

        Frank.display();
        Billy.display();
        Alex.display();
        Miranda.display();
        Joel.display();
    }

}
class student
{

    String name;
    double grade;
    int ID;

    public student(String n, double g, int d)
    {
        name = n;
        grade = g;
        ID = d;

    }
    public void display()
    {

            System.out.println("Names: " + name);
            System.out.println("Grades: " + grade);
            System.out.println("Student ID: " + ID);


    }
}

That works for me.

If I'm right, PLEASE mark me as right, I need the rep! :-)

Sign up to request clarification or add additional context in comments.

6 Comments

Missed that from his original post. It's not needed, will edit.
while that does work, I'm mainly trying to use an array here so I can get to the information in a different way, as opposed to brute forcing my way into changing every object individually
Then you don't want to create new objects each time. Make an array of 'name' and have name[] = {"Frank", "Billy"... etc
would that also neglect the use of needing a constructor?
Yes, you'd get rid of the student class completely and run it all in main.
|
0
public student(String[] n, double[] g, int[] d)
{
name = n;
grade = g;
ID = d;

}

Your constructor expects an array of Strings, an array of doubles, and an array of ints.

This, however, is what you pass:

new student("Joel", 89.9, 486248);

One single String, one single double, and a single int.

My guess, you don't need arrays in your Student class, except for the grades.

So, step one is to remove the arrays for name and id.

step two: pass an array of doubles as grades.

6 Comments

awesome, I see what I did there. However, I just am unsure of the syntax for physically putting those numbers into the array. I tried AndyTurner's method, but it gives me more errors saying "illegal start of expression
so how did you add the numbers in the array?
I haven't been able to yet. I'm curious as to how I could, for both the grades and ID numbers. Once I have a way to put each value into the array using the constructor method, it should work? I was thinking something like "name[0] = {"Frank"}" and so on, but that doesn't seem to work because that array is defined down below, so I can't specifically do that.
this is not related to constructors at all. name[0] = "Frank"; name might be an array, name[0] is just the first element of the array, which is a String.
so would there not be a way to use a constructor input the values into the array as I'm trying? I would basically just have to say "name[] = "Frank, Billy, etc"?
|
0

In your constructor for student, all of the inputs are primitive arrays however, you are not using them as such in the creation of the objects above.

If you want to use a constructor as follows:

Student billy = new Student("Billy", 23, 35);

then you must define the constructor with non-array inputs i.e.

class Student {
    String name;
    double grade;
    int id;
    public Student(String name, double grade, int id) {
        this.name = name;
        this.grade = grade;
        this.id = id;
    }
}

1 Comment

After looking up what "this" does, it seems to be doing the opposite of what I'm intending, but thank you! I'm needing to put these values into separate arrays so my "display()" class can print properly and I can later edit the info in different fashions and such
0

Alrighty, I decided to use @NickL's idea by using the array to construct the objects and then access it as an array afterwards.

package bilak_sackin_assignment_7;

public class Bilak_Sackin_Assignment_7 {

public static void main(String[] args) {

   String name[] = {"Frank", "Billy","Alex", "Miranda", "Joel" };
   double grades[] = {95.2, 65.6, 65.2, 80.0, 89.9};
   int iD[] = {112312, 656454, 123456, 963852, 486248};

    for (int i = 0; i < 5; i++)
    {
    String names = name[i];
    double grade =(grades[i]);
    int ID = (iD[i]);
    System.out.println("Name: " + names + "   |  Grade: " +  grade + "   |  ID number: " +  ID);
    }
}

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.