Java : How to create an array from object then store String and double in that array

I want to create an array from object then store in that array with 2 different type of date i'm still learning java and this is kinda my task and i searched many times and have find nothing so please i need your help :D the base class :

package com.matcho.task;

public class Subject {
    String name;
    Double grade;

    public Subject(String myname, double myGrade) {
        name = myname;
        grade = myGrade;
    }

    public void printermethod() {
        System.out.println("Your Subject is " + name + " and ur grade is "
                + grade);
    }
}

and this is the main class :

package com.matcho.task;

import java.util.Scanner;

public class subjectUseing {

    public static void main(String[] args) {
        String studentName = "";
        double studentGrade = 0;
        Subject object1 = new Subject(studentName, studentGrade);
        Scanner input = new Scanner(System.in);
        System.out.print("Please, Enter the Size of the Array : ");
        int arraySize = input.nextInt();
        Subject[] gradeArray = new Subject[arraySize];
        for (int i = 0; i < gradeArray.length; i += 2) {
            if ((i + 1) % 2 == 0) {
                System.out.println("Please, Enter Subject Number Grade");
                studentGrade = input.nextDouble();
                studentGrade = object1.grade;
                gradeArray[i] = object1.grade; 
                //Error said (cannot convert  from Double to Subject)
                //object1.grade = (Double)gradeArray[i];
                //gradeArray[i] = object1.grade;
                continue;
            } else {
                System.out.println("Please, Enter Subject Number Name");
                studentName = input.next();
                studentName = object1.name;
                //Error said (cannot convert  from String to Subject)
                gradeArray[i] = new Subject(object1.name, object1.grade);
                // gradeArray[i] = object1.name;
                // gradeArray[i] = new String(object1.name); // Failed T_T
            }

        }// For End
        for (int i = 0; i < gradeArray.length; i += 2) {
            System.out.println("Your Grade in each Subject is : "
                    + gradeArray[i] + " " + gradeArray[i + 1]);
        }// For End
    }
}

i tried many ways and search many times but found nothing so please i need help because this error [cannot convert from Double to Subject] blow up my mind :D

Jon Skeet
people
quotationmark

You basically need to simplify your loop: on each iteration of the loop, ask for both the subject name and the grade, then create one object to store them both:

for (int i = 0; i < gradeArray.length; i++) {
    System.out.println("Please enter the subject name");
    String name = input.next();
    System.out.println("Please enter the subject grade");
    double grade = input.nextDouble();
    gradeArray[i] = new Subject(name, grade);
}

Note the declaration of the variables inside the loop - you don't need them outside the loop, so don't declare them there.

Then for display, you'd just use:

for (Subject grade : gradeArray) {
  System.out.println(grade);
}

Again, there's no need to skip every other item in the array - each element in the array is a reference to a Subject object, which contains both a name and a grade.

(Or add getName and getGrade methods to Subject so that you can customize the output.)

Note that you may find Scanner a bit of a pain to work with - nextDouble() won't consume a line break, for example, which may mean you get an empty string when reading the next name. You might want to consider just reading a line at a time and using Double.parseDouble to parse a string. (Or use NumberFormat.)

people

See more on this question at Stackoverflow