Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

I'm trying to store 2 user inputs (doubles) inisde a local array. I don't know the Syntax

Instruction for the method:
readMarks(): accepts Scanner object, return nothing. Reads number of courses, and then reads marks of all courses and stored them in a local array.

I just need the syntax to store all the values inside the array and eventually use that array somewhere else. Is there a way to do that?

import java.util.Scanner;   

public class Student extends Person{
    private int studentNumber;
    private String programName;
    private double gpa;
    private double baseFees;
    private double maxMarks = 100;
    private double maxGPA = 4;

    public Student(int studentNumber, String programName, double gpa,
            double baseFees, String fName, String lName, String mail, long pNumber, double maxMarks, double maxGPA) {
        super(fName, lName, mail, pNumber);
        this.studentNumber = studentNumber;
        this.programName = programName;
        this.gpa = gpa;
        this.baseFees = baseFees;
        this.maxGPA = maxGPA;
        this.maxMarks = maxMarks;
    }
    
    public void readInfo(Scanner input) {
        System.out.println("Enter first Name: ");
        String  fName = input.next();
    
        System.out.println("Enter last name: ");
        String lName = input.next();
    
        System.out.println("Enter email: ");
        String mail = input.next();
    
        System.out.println("Enter phone number: ");
        long pNumber = input.nextLong();    
    
        System.out.println("Enter GPA: ");
        double gpa = input.nextDouble();
    
        System.out.println("Enter baseFees: ");
        double baseFees = input.nextDouble();   
    
        readMarks(input);
    }
    
    public void readMarks(Scanner  input) {
        System.out.println("Enter number of courses: ");
        double numberOfCourses = input.nextDouble();
        
        System.out.println("Enter marks: ");
        double courseMarks = input.nextDouble();
    }   
}

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The number of courses should be int, no need to support fractions for it. And then you will need to create a double array and fill the values:

public void readMarks(Scanner  input) {
    System.out.println("Enter number of courses: ");
    int numberOfCourses = input.nextInt();
    
    System.out.println("Enter marks: ");
    double[] courseMarks = new double[numberOfCourses];
    for (int i = 0; i < numberOfCourses; i++) {
        courseMarks[i] = input.nextDouble();
    }
    // Now you have an array, do something with it, but note, it's local
}   
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading