0

What's is wrong with the logic of the code? There're two methods: readAllExams creates and returns an array of the objects and also calls another method readExam which returns object. A passed Scanner object is a textfile which has 10 lines different people's names, ID, midterm or final, and score, for example: John McGregor 2 'F' 100. So what did I do wrong here? The method gives smth like this: [LExam;@717da562. Thanks in advance, folks!

public static void main(String [] args) throws FileNotFoundException
    {
        Scanner data = new Scanner(new File("Exam.txt"));
        Exam[] tempObject = readAllExams(data);
        System.out.println(tempObject);


    }

    public static Exam[] readAllExams(Scanner s) throws ArrayIndexOutOfBoundsException
    {
        readExam(s);
        String firstName = "";
        String lastName = "";
        int ID = 0;
        String examType = "";
        int score = 0;

        int index = 0;

        Exam[] object = new Exam[50];

        while(s.hasNext())
        {
            //Returns firtsName and lastName 
            firstName = s.next();
            lastName = s.next();

            //Returns ID number
            if(s.hasNextInt())
            {
                ID = s.nextInt();
            }
            else 
                s.next();

            //Returns examType which is 'M' or 'F'
            examType = s.next();

            if(s.hasNextInt())
            {
                score = s.nextInt();
            }

            object[index] = new Exam(firstName, lastName, ID, examType, score);
            System.out.println();
            index++;
        }
        return object;


    }

    public static Exam readExam(Scanner s)
    {
        String firstName = "";
        String lastName = "";
        int ID = 0;
        String examType = "";
        int score = 0;

        while (s.hasNext())
        {
            //Returns firtsName and lastName 
            firstName = s.next();
            lastName = s.next();

            //Returns ID number
            if(s.hasNextInt())
            {
                ID = s.nextInt();
            }
            else 
                s.next();

            //Returns examType which is 'M' or 'F'
            examType = s.next();

            if(s.hasNextInt())
            {
                score = s.nextInt();
            }
        }
        Exam temp = new Exam(firstName, lastName, ID, examType, score);
        return temp;
    }
rsutormin
  • 1,629
  • 2
  • 17
  • 21
Mike
  • 23
  • 4
  • You're printing the reference variable's address that is referring to the array instead of the elements in that array. Use for loop and print each element in the array like this 'tempObject[i];' – Prudhvi Feb 11 '15 at 04:53
  • That is the address of the object temp. Try accessing its methods/attributes to get your desired output. – Raghav Feb 11 '15 at 04:53
  • 1. Give Exam a decent `toString()` implementation, and 2. Don't try to print out an array blindly. Either iterate through the array printing each item or use `Arrays.toString(myArray)` and print that. This question has been asked and answered **many** times. – Hovercraft Full Of Eels Feb 11 '15 at 04:53
  • @Raghav: it's not necessarily an address of anything. It's the hashCode returned by the object being printed. From the Object API: `getClass().getName() + '@' + Integer.toHexString(hashCode())`. Please check the API first. – Hovercraft Full Of Eels Feb 11 '15 at 04:55
  • One of many duplicates found. Question closed. – Hovercraft Full Of Eels Feb 11 '15 at 04:56
  • @Raghav: for more specifics on what the default hashCode represents, please see [this answer](http://stackoverflow.com/a/4931181/522444). It *may* represent an address of an object, but there's no guarantee of this. – Hovercraft Full Of Eels Feb 11 '15 at 05:01

1 Answers1

0

You are by default calling toString on Exam array and hence that's what you see. You need following:

  • Implement toString method in Exam to print firstName, Id, lastName, examType

    public String toString() {
        return "id:" + ID + " fname: " + firstName; 
    }
    
  • Change your System.out.println(tempObject); to System.out.println(Arrays.toString(tempObject)); Which will internally call toString on every Exam object in array and will print exam array in nice readable format.
SMA
  • 36,381
  • 8
  • 49
  • 73