Results 1 to 4 of 4
- 05-11-2011, 04:20 AM #1
Member
- Join Date
- May 2011
- Posts
- 2
- Rep Power
- 0
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();
}
}
- 05-11-2011, 04:32 AM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
The constructor argument is Name, it should be name.
- 05-11-2011, 04:37 AM #3
Member
- Join Date
- May 2011
- Posts
- 2
- Rep Power
- 0
thank you. it was one of those careless mistakes where i accidentally made it N instead of n. thank you very much.
- 05-11-2011, 04:40 AM #4
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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
-
How to "allow null" in a very simple script?
By Mattedatten in forum New To JavaReplies: 7Last Post: 12-04-2010, 11:09 PM -
String output showing "null"
By hayden06f4i in forum New To JavaReplies: 6Last Post: 11-05-2010, 10:21 AM -
JOptionPane.showMessageDialog(null,"Etc Etc"); - What does null actually do?
By markious in forum New To JavaReplies: 2Last Post: 03-19-2010, 05:30 PM -
jList Issues - getSelectedValue() Keeps returning "null"
By MoobKeeng in forum New To JavaReplies: 0Last Post: 07-28-2009, 06:45 PM -
the dollar sign "$", prints like any other normal char in java like "a" or "*" ?
By lse123 in forum New To JavaReplies: 1Last Post: 10-20-2008, 07:35 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks