Hi all - I am having a little trouble getting going on the methods for this assignment. I need to use a method which passes a Scanner as the object to create an array that is the correct size, but has null values. It reads in a file which contains a number on a single line at the top, which is the number of Student objects that are on the list. The Student objects themselves are first name, last name, followed by three test scroes. I am new to Java and am struggling on getting this going and am looking for any advice. The method must only return an array of correct size but with null values..as other methods will be used to populate the array. For example, if the number on the first line is three, it needs to create an array with fifteen null "slots".

import java.io.File;
import java.io.FileNotFoundException;

import java.util.Scanner;

public class ProgrammingAssignment {
    public static void main(String[] args) {
        Student[] studentArray = null;
        Scanner sc = new Scanner(System.in);
        Scanner readFile = null;
        String fileName = "";
        boolean done = false;
        int tries = 1;
        File file = null;
        while (!done) {
            try {
                System.out.println("Please enter the name of the file: ");
                fileName = sc.nextLine();
                file = new File(fileName);
                readFile = new Scanner(file);
                System.out.println(file);
                done = true;
            }
            catch (FileNotFoundException fnfe) {
                System.out.println("File not found");
                if (tries == 3) {
                    System.out.println("Maximum tries exceeded.");
                    System.exit(1);
                }   
                tries++;
            }
        }   

        sc.close();
        readFile.close();

    }

    //creates and returns an uninitialized array of Student instances
    //the returned array will be of the right size but each "slot" will
    //have null values.
    //needs to read the first line of the input file.  

    public static Student[] createStudentArray(Scanner sc) {


    }

Recommended Answers

All 12 Replies

The other part of the code we are provided is:

public class Student {
    private String firstName;
    private String lastName;
    private double average;

    public Student(String firstName, String lastName, double average) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.average = average;
    }   

    public String getFirstName() {
        return firstName;
    }
    public String getLastName() {
        return lastName;
    }

    public double getAverage() {
        return average;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public void setAverage(double average) {
        this.average = average;
    }

    public String toString() {
        return firstName + "\n" + lastName + "\n" + average;
    }
}

When the requirement asks you to creating an array size of n with all null value, it means you simply create an array, and then iterate through the array to assign null value to each index slot.

Object[] anObject = new Object[SIZE];  // create an array object size of SIZE
// then iterate and assign null value to each slot
for (int i=0; i<anObject.length; i++) { anObject[i] = null; }

The reason is to ensure that each slot of the array has been initialised. You should be able to adapt this code portion to your own purpose.

instead of returning student[] give datatype like int[] and assign null value by iterating loop as suggested by Taywin.

Thanks

Taywin: why do you need to do that? null is the default value of an object, so, if you create the array like this:

Object[] nullArr = new Object[length];

the only object there that is not null is the array, all of it's elements are (by default) null.

Yes, that's right. A new array of objects is full of nulls.
jalpesh: the required method signatere returns Student[], but it any case you cannot assign nulls to an int, or any other primitive.

@stultuske, you are correct, but I want to be explicit. It would be good for beginners to not jump steps but rather go through it at least for a few times when they still don't understand the language enough. :) A good thing for showing this is that they should at least have an idea how to create an object array and be able to assign other values even though it is not null.

I kind of understand how to create the array with a specified size, but I am confused as to how to implement it into a method with a scanner class..I can't figure out how to get it to read the first line of the program and take that information and create an array.

what do you mean implement it? don't.

you can access (or assign) the elements of your array by:

Object[] arr = new Object[nrOfElements];
arr[0] = new Object("b"); // assign a value
Object b = arr[0]; // retrieve the value

for the rest, put it in a loop, in which you run over each index, and assign each element of the array with a value you read in through your scanner

Reading a line from the file via a scanner is exactly the same as reading stuff from the console via a scanner, and you already have done that at least once! Just use an ordinary Scanner method like nextLine, nextInt etc. to read the info from the scanner and use that info to specify the size of the array. This is a lot easier than you think!

So something like this?

public static Student[] createStudentArray(Scanner sc){
arraySize = sc.nextInt();
Student[] studentArray = new Student[arraySize]
for(int i = 0; i < studentArray.length; i++){
studentArray[i] = null;}
}

Yes, just like that! Only thing to worry about is what happens if the user doesn't type a vialid number, but maybe you can ignore that for now.

I don't think we have to worry about that right now, as the file has a guaranteed format with an interger as the first line. Thanks for the help everyone..still getting the hang of methods and arrays.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.