Results 1 to 3 of 3
  1. #1
    Join Date
    Nov 2012
    Rep Power

    Default Why the program only display the first person name?

    //Car class
    public class Car {
    //data properties
    //datatype = Owner and user = object -- has-a relationship
    private Owner user; //implement the Owner class .. declare an object for Owner
    private String plateNo;
    private String color;
    private int year;
    private String make;
    private String model;
    private double capacity;

    public Car() {

    public Car(Owner user,String plateNo,String color,int year,String make,String model,double capacity){

    //user = object ; toString() --> instance method --> object.method
    public String toString(){
    return(user.toString()+ " \t"+plateNo+"\t" + color + "\t"+ year + "\t"+ make + "\t"+ model + "\t" + capacity);

    //Owner class
    public class Owner {
    //data properties
    private String name;
    private int ic;

    public Owner() {

    //cons.with parameters
    public Owner(String name,int ic){;

    public String toString(){
    return (name + "\t" + ic);


    //main class
    import java.util.Scanner;
    public class Test {

    public static void main(String[] args) {
    Scanner scan = new Scanner(;
    //declare an array for car type
    Car[] car = new Car[3];

    for(int i=0; i<car.length; i++){

    System.out.print("Enter name : ");
    String name=scan.nextLine();
    System.out.print("Enter IcNo : ");
    int ic= scan.nextInt();
    System.out.print("Enter plate : ");
    String plateNo =;
    System.out.print("Enter color : ");
    System.out.print("Enter year : ");
    int year =scan.nextInt();
    System.out.print("Enter make : ");
    String make;
    System.out.print("Enter model : ");
    String model;
    System.out.print("Enter capacity : ");
    double capacity =scan.nextDouble();

    car[i]= new Car(new Owner(name,ic),plateNo,color,year,make,model,capac ity);
    }//end for
    int regNo = 1001;
    //display output
    for(int i=0; i<car.length; i++)
    System.out.println((regNo+i) + car[i].toString());
    Last edited by; 11-03-2012 at 10:21 AM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Blog Entries
    Rep Power

    Default Re: Why the program only display the first person name?

    The problem is in code not shown, I believe.

    Please edit your post and add [code] [/code] tags around your code.
    Consider showing your Owner class.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Madgaon, Goa, India
    Rep Power

    Default Re: Why the program only display the first person name?

    Why do you have two consecutive lines that read the nextLine from the Scanner input?

    Where do you think one of those nextLine() calls should be relocated so that the value read for the name is actually what you type in after the prompt is displayed?

    Hint: other Scanner methods like nextDouble() do not consume the newline character that is added to the Scanner when you press <Enter>

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Replies: 43
    Last Post: 03-21-2011, 10:53 AM
  2. [Free] Looking for a skilled person
    By JohnnyL in forum Jobs Offered
    Replies: 0
    Last Post: 02-08-2011, 02:55 PM
  3. Display program
    By nhlfan in forum New To Java
    Replies: 2
    Last Post: 11-22-2007, 12:00 AM
  4. My program doesnt display anything
    By Bojevnik in forum AWT / Swing
    Replies: 2
    Last Post: 10-19-2007, 02:50 PM
  5. Replies: 1
    Last Post: 08-07-2007, 07:34 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts