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

    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){
    this.user=user;
    this.plateNo=plateNo;
    this.color=color;
    this.year=year;
    this.make=make;
    this.model=model;
    this.capacity=capacity;
    }

    //toString()
    //user = object ; toString() --> instance method --> object.method
    //user.toString()
    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;

    //methods
    public Owner() {
    }

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

    //toString
    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(System.in);
    //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();
    scan.nextLine();
    System.out.print("Enter IcNo : ");
    int ic= scan.nextInt();
    System.out.print("Enter plate : ");
    String plateNo = scan.next();
    System.out.print("Enter color : ");
    String color=scan.next();
    System.out.print("Enter year : ");
    int year =scan.nextInt();
    System.out.print("Enter make : ");
    String make =scan.next();
    System.out.print("Enter model : ");
    String model =scan.next();
    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 yyKHOR_1021@live.com; 11-03-2012 at 10:21 AM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    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
    Location
    Madgaon, Goa, India
    Posts
    11,302
    Rep Power
    20

    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>

    db
    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
  •