4 Replies - 127 Views - Last Post: 05 February 2012 - 11:50 AM Rate Topic: -----

Topic Sponsor:

#1 NonSleep  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 15-May 11

Adding object to an Array problem

Posted 05 February 2012 - 10:37 AM

Hi everyone .. i'm new with Java, so i need a little help from you guys

I wrote a program that uses an array and Scanner to enter the data of 5 students by using for loop
i didn't have any errors but the output is something like "Student@1d58aae" i think i have problem with adding objects into my array .. i tried ArrayList but i had the same out put

here is my code, using array (i have another one using ArrayList)

import java.util.*;
public class StudentArray2 {
        
    
    public StudentArray2() {
    }
    
   
    public static void main(String[] args) {
        // TODO code application logic here
        
       Scanner k1=new Scanner(System.in);
        
       
        
     Student[] a1=new Student[5];
     
     
     String x;
     String y;
     double z;
        
        for(int i=0;i<5;i++)
        {
        	 System.out.println("Enter the name: ");
           x=k1.next();
        
        System.out.println("Enter the ID : ");
        y=k1.next();
        
        System.out.println("Enter the GPA: ");
       z=k1.nextDouble();
        
        Student s1=new Student(x,y,z);
        

        a1[i]=new Student(x,y,z);
       
        }
        
     
        
        for(int i=0;i<5;i++)
        System.out.println("the Name is " + a1[i] );
        
        
    }
}



Is This A Good Question/Topic? 0
  • +

Replies To: Adding object to an Array problem

#2 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1413
  • View blog
  • Posts: 6,037
  • Joined: 20-September 08

Re: Adding object to an Array problem

Posted 05 February 2012 - 10:45 AM

You need to override Student.toString() to get meaningful output
Was This Post Helpful? 0
  • +
  • -

#3 NonSleep  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 15-May 11

Re: Adding object to an Array problem

Posted 05 February 2012 - 11:09 AM

so i have to change the output to String??
Was This Post Helpful? 0
  • +
  • -

#4 smohd  Icon User is offline

  • Critical Section
  • member icon



Reputation: 1644
  • View blog
  • Posts: 4,126
  • Joined: 14-March 10

Re: Adding object to an Array problem

Posted 05 February 2012 - 11:25 AM

In your Student class create a toString() method and define how you want your data to be displayed. By this you will be overriding the normal toString() from an object class which returns that output.
Was This Post Helpful? 0
  • +
  • -

#5 NonSleep  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 15-May 11

Re: Adding object to an Array problem

Posted 05 February 2012 - 11:50 AM

great!! thanks a lot .. it's work now, and thank you for the link of toString() post :^:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1