Results 1 to 4 of 4
  1. #1
    jtap36 is offline Member
    Join Date
    May 2011
    Posts
    2
    Rep Power
    0

    Cool Inheritance, outputs "name : null" instead of inputted name

    i'm trying to make it so that the use inputs the name, model, manufacturer, color, and capacity, and then outputs them. the program reads the inputs correctly but is outputting the name as null. how do i fix this? thanks.



    import java.util.Scanner; //import java.util.scanner
    class Vehicle //creat Vehicle class
    {


    int number;
    String name;
    String model;
    String manufacturer;
    String color;

    Vehicle(int number,String model,String manufacturer,String color, String Name)
    {
    this.number = number;
    this.name = name;
    this.model = model;
    this.manufacturer = manufacturer;
    this.color = color;
    }
    }

    class Person
    {

    private String name;

    public Person()
    {
    name = "No name";
    }
    public Person(String initialName)
    {
    name = initialName;
    }
    public Person(Person theObject)
    {
    name = theObject.name;
    }
    public String getName()
    {
    return name;
    }
    public void setName(String theName)
    {
    name = theName;
    }
    public String toString()
    {
    return("Name: "+name);
    }

    //equals method
    public boolean equals(Object obj)
    {
    if (this == obj)
    return true;
    if (obj == null)
    return false;
    if (!(obj instanceof Person))
    return false;
    Person other = (Person) obj;
    if (name == null)
    {
    if (other.name != null)
    return false;
    } else if (!name.equals(other.name))
    return false;
    return false;
    }

    }
    public class Truck1 extends Vehicle
    {
    int capacity;
    Truck1(int number,String model,String manufacturer,String color,int capacity, String name)
    {
    super( number, model, manufacturer, color, name);
    this.capacity = capacity;
    }
    void show()
    {
    System.out.println("Name = " + name);
    System.out.println("Number = " + number);
    System.out.println("Model = " + model);
    System.out.println("manufacturer = " + manufacturer);
    System.out.println("Color = " + color);
    System.out.println("Capacity = " + capacity);
    }

    public static void main(String[] args)
    {
    Scanner input=new Scanner(System.in);

    System.out.println("Name: ");
    String name = input.next();

    System.out.println("Truck No: ");
    int number=input.nextInt();

    System.out.println("Manufacturer: ");
    String manufacturer=input.next();

    System.out.println("Model: ");
    String model=input.next();

    System.out.println("Color: ");
    String color=input.next();

    System.out.println("Loading Capacity: ");
    int cap=input.nextInt();

    Truck1 t=new Truck1(number,model,manufacturer,color,cap, name);
    System.out.println("****Truck Details****");
    System.out.println();
    t.show();
    }
    }

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    The constructor argument is Name, it should be name.

  3. #3
    jtap36 is offline Member
    Join Date
    May 2011
    Posts
    2
    Rep Power
    0

    Default

    thank you. it was one of those careless mistakes where i accidentally made it N instead of n. thank you very much.

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    You are welcome. Please mark your thread solved with the thread tools at the top of the page. Also, for future reference, if you post code, please wrap it in code tags

    To do this type
    [code]
    YOUR CODE HERE
    [/code]

Similar Threads

  1. How to "allow null" in a very simple script?
    By Mattedatten in forum New To Java
    Replies: 7
    Last Post: 12-04-2010, 11:09 PM
  2. String output showing "null"
    By hayden06f4i in forum New To Java
    Replies: 6
    Last Post: 11-05-2010, 10:21 AM
  3. Replies: 2
    Last Post: 03-19-2010, 05:30 PM
  4. Replies: 0
    Last Post: 07-28-2009, 06:45 PM
  5. Replies: 1
    Last Post: 10-20-2008, 07:35 AM

Tags for this Thread

Posting Permissions

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