Why am I getting a null? [closed] - java

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
Why is it when I call System.out.println(classroom.toStringLong()) I get: classroom: a large lecture hall with a null that goes null to null?
The correct output is supposed to be: classroom: a large lecture hall with a door that goes outside to sidewalk
public class Main {
public static void main(String[] args) {
Space classroom = new Space();
classroom.setName("classroom");
classroom.setDescription("a large lecture hall");
Space sidewalk = new Space();
sidewalk.setName("sidewalk");
sidewalk.setDescription("a plain concrete sidewalk with weeds growing through the cracks");
Portal door = new Portal();
door.setName("door");
door.setDirection("outside");
door.setDestination(sidewalk);
classroom.setPortal(door);
System.out.println(classroom.toStringLong());
}
}
public class Space {
private String _name;
private String _description;
private Portal _portal;
public static void main(String[] args) {
// TODO Auto-generated method stub
}
public String getName() {
return _name;
}
public void setName(String _name){
this._name=_name;
}
public String getDescription() {
return _description;
}
public void setDescription(String _description){
this._description=_description;
}
public Portal getPortal() {
return _portal;
}
public void setPortal(Portal _portal){
this._portal=_portal;
}
public String toString(){
return _name;
}
public String toStringLong(){
if (_portal!= null){
Portal p= new Portal();
p.toStringLong();
String Longcombined=_name + ": " + _description+" with a "+p.toStringLong();
return Longcombined;
}
else{
String Long=_name + ": " + _description;
return Long;
}
}
}
public class Portal {
private String _name;
private String _direction;
private Space _destination;
public String getName() {
return _name;
}
public void setName(String _name){
this._name=_name;
}
public String getDirection(){
return _direction;
}
public void setDirection(String _direction){
this._direction=_direction;
}
public Space getDestination(){
return _destination;
}
public void setDestination(Space _destination){
this._destination=_destination;
}
public String toString(){
String combined=_name+ " that goes "+_direction;
return combined;
}
public String toStringLong(){
Space space=new Space();
String combined=toString() + " to " + space.getDescription() ;
return combined;
}
}

You are creating a new object of space and printing its description which is null
rewrite your toStringLong() method to
In class Space
public String toStringLong(){
if (_portal!= null)
{
// comment this Portal p= new Portal();
_portal.toStringLong();
String Longcombined=_name + ": " + _description+" with a "+_portal.toStringLong();
return Longcombined;
}
else
{
String Long=_name + ": " + _description;
return Long;
}
}
Class Portal ->
public String toStringLong()
{
String combined=toString() + " to " + _destination.getDescription() ;
return combined;
}
Hope this will solve your problem.

When run the this code "Space space=new Space();" inside of the toStringLogn() method, new space object is created and also all instant variables of this object are initialized with their default values. Default value of the String is "null". That is why you get null value when you use this object.
It is better to redefine your toStringLong() as follows,
String combined=toString() + " to " + _destination.getDescription() ;
return combined;

Related

java : Returning null or 0.0 [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I do not understand when my program reads my yml file, it read the type of float but it returns zero or 0.0. I have my float TRIG_EDGE_LEV which return 0.0 but it sould returns 1.5.
public class Loadyml {
//static OscilloDSO1072B dso1072B = new OscilloDSO1072B("visa://192.168.53.67/USB0::0x0957::0x0588::CN55040484::INSTR") ;
private String TRIG_EDGE_SLOP;
private String ENABLE_CHAN_OFFSET;
private float TRIG_EDGE_LEV;
private String TIM_MODE;
private String SCALE_NUMBER;
private String TIM_MAIN_SCAL;
private String SCALE_CHAN;
private String TRIG_EDGE_SOURCE;
private String WAV_SOUR;
private String PROB_CHAN;
private String INVERSE_CHAN;
private String WAV_POINT_MODE;
private String DISPLAY_CHAN;
private String ENABLE_CHAN_BWL_OFF;
private String COUPLING_CHAN;
private String OFFSET_NUMBER;
private String WAV_FORM;
public String getTRIG_EDGE_SLOP() {
return TRIG_EDGE_SLOP;
}
public void setTRIG_EDGE_SLOP(String TRIG_EDGE_SLOP) {
this.TRIG_EDGE_SLOP = TRIG_EDGE_SLOP;
}
public String getENABLE_CHAN_OFFSET() {
return ENABLE_CHAN_OFFSET;
}
public void setENABLE_CHAN_OFFSET(String ENABLE_CHAN_OFFSET) {
this.ENABLE_CHAN_OFFSET = ENABLE_CHAN_OFFSET;
}
public float getTRIG_EDGE_LEV() {
return TRIG_EDGE_LEV;
}
public void setTRIG_EDGE_LEV(float TRIG_EDGE_LEV) {
this.TRIG_EDGE_LEV = TRIG_EDGE_LEV;
}
public String getTIM_MODE() {
return TIM_MODE;
}
public void setTIM_MODE(String TIM_MODE) {
this.TIM_MODE = TIM_MODE;
}
public String getSCALE_NUMBER() {
return SCALE_NUMBER;
}
public void setSCALE_NUMBER(String SCALE_NUMBER) {
this.SCALE_NUMBER = SCALE_NUMBER;
}
public String getTIM_MAIN_SCAL() {
return TIM_MAIN_SCAL;
}
public void setTIM_MAIN_SCAL(String TIM_MAIN_SCAL) {
this.TIM_MAIN_SCAL = TIM_MAIN_SCAL;
}
public String getSCALE_CHAN() {
return SCALE_CHAN;
}
public void setSCALE_CHAN(String SCALE_CHAN) {
this.SCALE_CHAN = SCALE_CHAN;
}
public String getTRIG_EDGE_SOURCE() {
return TRIG_EDGE_SOURCE;
}
public void setTRIG_EDGE_SOURCE(String TRIG_EDGE_SOURCE) {
this.TRIG_EDGE_SOURCE = TRIG_EDGE_SOURCE;
}
public String getWAV_SOUR() {
return WAV_SOUR;
}
public void setWAV_SOUR(String WAV_SOUR) {
this.WAV_SOUR = WAV_SOUR;
}
public String getPROB_CHAN() {
return PROB_CHAN;
}
public void setPROB_CHAN(String PROB_CHAN) {
this.PROB_CHAN = PROB_CHAN;
}
public String getINVERSE_CHAN() {
return INVERSE_CHAN;
}
public void setINVERSE_CHAN(String INVERSE_CHAN) {
this.INVERSE_CHAN = INVERSE_CHAN;
}
public String getWAV_POINT_MODE() {
return WAV_POINT_MODE;
}
public void setWAV_POINT_MODE(String WAV_POINT_MODE) {
this.WAV_POINT_MODE = WAV_POINT_MODE;
}
public String getDISPLAY_CHAN() {
return DISPLAY_CHAN;
}
public void setDISPLAY_CHAN(String DISPLAY_CHAN) {
this.DISPLAY_CHAN = DISPLAY_CHAN;
}
public String getENABLE_CHAN_BWL_OFF() {
return ENABLE_CHAN_BWL_OFF;
}
public void setENABLE_CHAN_BWL_OFF(String ENABLE_CHAN_BWL_OFF) {
this.ENABLE_CHAN_BWL_OFF = ENABLE_CHAN_BWL_OFF;
}
public String getCOUPLING_CHAN() {
return COUPLING_CHAN;
}
public void setCOUPLING_CHAN(String COUPLING_CHAN) {
this.COUPLING_CHAN = COUPLING_CHAN;
}
public String getOFFSET_NUMBER() {
return OFFSET_NUMBER;
}
public void setOFFSET_NUMBER(String OFFSET_NUMBER) {
this.OFFSET_NUMBER = OFFSET_NUMBER;
}
public String getWAV_FORM() {
return WAV_FORM;
}
public void setWAV_FORM(String WAV_FORM) {
this.WAV_FORM = WAV_FORM;
}
public void Loadfichier() throws FileNotFoundException{
try {
System.out.println(Yaml.loadType(new File("config.yml"), Loadyml.class));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public String toString() {
//dso1072B.setchanconftest(commande);
// Float.parseFloat(SCALE_NUMBER);
return "[Commande SCPI='"+WAV_FORM+","+ENABLE_CHAN_BWL_OFF+""
+ ","+TRIG_EDGE_LEV+","+TIM_MODE+""
+ ","+SCALE_NUMBER+","+TRIG_EDGE_SLOP+""
+ ","+DISPLAY_CHAN+","+TIM_MAIN_SCAL+""
+ ","+SCALE_CHAN+","+TRIG_EDGE_SOURCE+""
+ ","+WAV_SOUR+","+INVERSE_CHAN+""
+ ","+PROB_CHAN+","+WAV_POINT_MODE+""
+ ","+ENABLE_CHAN_OFFSET+","+COUPLING_CHAN+""
+ ","+OFFSET_NUMBER+"']";
}
}
How can i fix that ?
Console screen
To solve your issue - use double instead of float - Somewhere inside Jyaml's codebase it tries to match floating point numbers to getter/setter methods (not fields) of type double reflectively. As your field is of type float, reflection based matching fails and you get the default value of 0.0f
On a side note :
Use Boxed/object versions of fields and methods, i.e. - Double instead of double. null make more sense than 0.0 for existence check. (I'd also recommend java 8's Optional).
Jyaml is super old and its codebase looks horrendous , please consider using a newer alternative for yaml parsing, preferably one that uses annotations like Jackson.
With Jackson - use annotations to map names in yaml file as they are and keep your code according to java conventions ( fields and methods should be camel cased)

How to add in if else statement to validate for string

I am using BlueJ for this assignment I have and I have a problem doing this part of the question which is to put decision constructs to ensure that invalid data is not set which I have already tried.In which I placed the if else statement in
the setName portion which does not work. The code will show error if I have put 1 in the GraphicIllustrators main void portion for setName. So where do I need to put the if else statement in the code below.BTW I am using inheritance to do this.So please advise.Thanks!
The coding for the main class:
public class Publishing_Inc
{
private int ID=0;
private String name="-Name Needed-";
private int level=0;
private String jobtitle="-Title Needed-";
private String edit="-Edit Skill-";
public void calculateID(){
int uniqueID;
uniqueID =((int)( Math.random()*10000)+1);
ID = uniqueID;
}
public int getID(){
return ID;
}
public void setName(String d) {
name = d;
}
public String getName() {
return name;
}
public void setTitle(String b){
jobtitle=b;
}
public String getTitle() {
return jobtitle;
}
public void calculatelevel(){
int uniquelevel;
uniquelevel =((int)( Math.random()*3)+1);
level = uniquelevel;
}
public int getlevel() {
return level;
}
public void setEdit(String z){
edit=z;
}
public String getEdit() {
return edit;
}
}
The sub class:
public class GraphicIllustrators extends Publishing_Inc
{
public void displayGraphInformation() {
System.out.println("ID: " + getID());
System.out.println("Name:" + getName());
System.out.println("Job Title: " + getTitle());
System.out.println("Level: " + getlevel());
System.out.println();
}
public static void main (String args[]) {
GraphicIllustrators graphic = new GraphicIllustrators ( );
graphic.calculateID ( );
graphic.setName (" Tim Cook" );
graphic.calculatelevel ();
graphic.setTitle ("Graphic Illustrators" );
graphic.displayGraphInformation( );
}
}
public static void main (String args[]) {
GraphicIllustrators graphic = new GraphicIllustrators ( );
graphic.calculateID ( );
if (input instanceof String) {
graphic.setName ( input ); //where input comes from an input box or query or other source.
}
else
{
//alert the user.
}
graphic.calculatelevel ();
graphic.setTitle ("Graphic Illustrators" );
graphic.displayGraphInformation( );
}
When you pass an argument to the function setName that isn't a String, Java will throw an exception. You need to cast the object sent to a string. If you want to prevent this, you should check the input before it's send the function setName. This will check if the input is an instance of the Object String, if not alert the user.

toString, still giving me a weird output [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
Hey guys I just had a question about toString. On a previous test my professor had this overrided method for toString that was similar to this method which I'm testing:
public String toString()
{
String s="";
s+="units: " + units;
s+="\n";
s+="owner: " +owner;
return s;
}
This method is is inside the class Residential which inherits from a base class Construction. Anyways, this mirrors a problem I had on a test where I would try to do say:
Residential R1 = new Residential();
R1.toString();
I thought R1.toString(); would display, which I put on the test, but obviously it was marked wrong and it doesn't.
So now I'm going over the problem and how to correct it. I tried doing say:
System.out.println(R1.toString());
but it's still giving me some weird output like "Residential#5c538b31". Why does it not overriden?
edit: The whole residential class, I'm aware it's not overridden, now but it wasn't annoted with a #Override by the professor in his code either so I assumed it wasn't needed.
public class Residential extends Construction {
private int units;
private String owner;
Residential ()
{
super();
units = 0;
owner = "Unknown";
}
Residential (String n, int y, double a, int u, String o)
{
super (n,y,a);
units = u;
owner = o;
}
public int getUnits()
{
return units;
}
public void setUnits(int u)
{
units = u;
}
public String getOwner()
{
return owner;
}
public void setOwner(String o)
{
owner = o;
}
public void display()
{
System.out.println("Name: " + getName() + " Year: " + getYear() + " Area: " + getArea() + " Number of Units: " + getUnits() + " Owner: " + getOwner());
}
public boolean isEqual (Residential r)
{
if (this.getName() == r.getName() && this.getYear() == r.getYear() && this.getArea() == r.getArea() && this.units == r.units && this.owner == r.owner)
{
return true;
}
return false;
}
public String toString()
{
String s="";
s += "the units is: " + units;
s += "\n";
s += "Owner: " + owner;
return s;
}
edit 2: Added construction class
public class Construction {
private String buildername;
private int year;
private double area;
Construction()
{
buildername = "Unknown";
year = 0;
area = -1;
}
Construction(String b, int y, double a)
{
buildername = b;
year = y;
area = a;
}
//Mutators
public void setName(String n)
{
buildername = n;
}
public void setYear(int y)
{
year = y;
}
public void setArea (double a)
{
area = a;
}
//Accessors
public String getName()
{
return buildername;
}
public int getYear()
{
return year;
}
public double getArea()
{
return area;
}
public void display()
{
System.out.println("Builder's Name: " + getName() + " Year: " + getYear() + " Area: " + getArea());
}
public boolean isEqual(Construction c)
{
if (this.buildername == c.buildername && this.year == c.year && this.area == c.area)
{
return true;
}
return false;
}
}
but it's still giving me some weird output like "Residential#5c538b31". What am I doing wrong?
This means that your version of the Residential class does not correctly override the toString() method.
To fix this, you need to give your class a proper toString override. I would also give the method an #Override annotation to be sure that it's truly overriding the method.
You also state:
On a previous test my professor had this overrided method for toString that was similar to this method which I'm testing...
... and yet you have not shown us the method that you're "testing". Perhaps you want to do this.
Edit
Regarding your posted code, that code is not what has produced the output that you've posted. Perhaps you need to refresh or restart your IDE, but the output could not possibly come from the posted code.
As an aside, your Residential toString() method should also call its parent class's toString() method in its method body, since the String returned should be part of Residential's String.

JAVA: Creating new objects that don't interfere with other objects of same class

I'm new here (also new to programming) and I tried to look for an answer but couldn't come up with one. My assignment is due today so help would be very appreciated. This problem has happened to me twice, but the first I was able to ignore it by programming another way now I can't anymore. Every time I create a new object (Agent a = new Agent() <-- name of my class), it interferes with other objects of same type (does this have anything to do with reference types?). I'm trying to make a DiscussionDirector class that takes two Agent objects and creates a randomized conversation between them (Random based), but I can't even get started because I haven't been able to create two objects of type Agent yet.
Here's the code for Agent:
import java.util.Calendar;
import java.io.*;
import java.util.Random;
public class Agent{
private static boolean isMale;
private static String birthdate;
private static int birthyear;
private static int birthmonth;
private static int birthday;
private static String name;
private static String nativeCity;
private static String currentCity;
private static String major;
private static Random r = new Random();
public static void main(String[]args){
}
public String getCityNow(){
return this.currentCity;
}
public String getCityBorn(){
return this.nativeCity;
}
public String getName(){
return this.name;
}
public boolean getGender(){
return this.isMale;
}
public String getMajor(){
return this.major;
}
public String getBirthday(){
String birthdate = (this.birthday + "/" + this.birthmonth + "/" + this.birthyear);
return birthdate;
}
public void sayHelloTo(String name){
System.out.println(this.name + " says: Hi " + name + ", I'm " + this.name);
}
public void sayHello(){
System.out.println(this.name + " says: Hello, my name is " + this.name);
}
public void CityBorn(){
System.out.println(this.name + " says: I am from " + this.nativeCity);
}
public void howOldAreYou(){
System.out.print(this.name + " says: I am ");
if(Calendar.getInstance().get(Calendar.MONTH) < this.birthmonth){
System.out.println((Calendar.getInstance().get(Calendar.YEAR) - this.birthyear - 1) + " years old");
}
else if((Calendar.getInstance().get(Calendar.MONTH) == this.birthmonth) && (Calendar.getInstance().get(Calendar.DAY_OF_MONTH) == this.birthday)){
System.out.println((Calendar.getInstance().get(Calendar.YEAR) - this.birthyear - 1) + " years old");
}
else{
System.out.println((Calendar.getInstance().get(Calendar.YEAR) - this.birthyear) + " years old");
}
}
public void sayGender(){
System.out.println(this.name + " says: I am a ");
if(isMale == true){
System.out.print("man");
}
else{
System.out.print("woman");
}
}
public void CityNow(){
System.out.println(this.name + " says: I currently live in " + this.currentCity);
}
public void sayMajor(){
System.out.println(this.name + " says: I am studying " + this.major);
}
public void whoAreYou(){
sayHello();
CityBorn();
howOldAreYou();
sayMajor();
CityNow();
}
public Agent()throws IOException{
this.isMale = r.nextBoolean();
if(this.isMale == true){
WordList MaleNames = new WordList("MaleNames.txt");
this.name = MaleNames.getRandomWord();
}
else{
WordList FemaleNames = new WordList("FemaleNames.txt");
this.name = FemaleNames.getRandomWord();
}
this.birthyear = 1995 - r.nextInt(60); //Agents can't be too young or too old.
this.birthmonth = r.nextInt(11)+1;
if(this.birthmonth == 1|this.birthmonth == 3|this.birthmonth == 5|this.birthmonth == 7|this.birthmonth == 8|this.birthmonth == 10|this.birthmonth == 12){
this.birthday = r.nextInt(30)+1;
}
else if (this.birthmonth == 2){
this.birthday = r.nextInt(27)+1;
}
else{
this.birthday = r.nextInt(29)+1;
}
WordList Major = new WordList("Majors.txt");
this.major = Major.getRandomWord();
WordList Cities = new WordList("Cities.tx");
this.nativeCity = Cities.getRandomWord();
this.currentCity = Cities.getRandomWord();
}
public Agent generateAgent()throws IOException{
return new Agent();
}
}
So yeah, does anyone have any idea why when I create a two Agent() objects and then do generateAgent() objects on them, they're always the same?
Thanks
You've made all your class variables static, which means one copy for the whole class. Remove static on all of them, so they all have one value per object instance.
This is because all fields are declared static in your agent class.
A static field is shared among all instances of the class.
To solve your issue just remove the static keyword!
A typical use of static keyword inside a class is a counter of created instances of that class.
public class Agent{
private static int numberAgent;
private String birthdate;
private int birthyear;
private int birthmonth;
...
}
And in the constructors of the class, you do
numberAgent++;
As the static variable of a class are shared across all the instances of the class,
you will have the number of agents instanciated in all Agent objects.

When printing list it is gibberish [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Java Object default toString
Why is it when I print my List from filereader it is [myServiceOrder#3bc1cac, myServiceOrder#32fe621e, myServiceOrder#5adbb9b9, myServiceOrder#f7e4f49, myServiceOrder#2d874991, myServiceOrder#ceee5f1, myServiceOrder#183a37d9]
public class myServiceOrder implements ServiceOrder, Comparable<myServiceOrder>{
private int number=0;
private String ownerName="";
private String make="";
private String model="";
private int year=0;
public myServiceOrder(int number, String ownerName, String make, String model, int year) {
this.number=number;
this.ownerName=ownerName;
this.make=make;
this.model=model;
this.year=year;
}
public myServiceOrder() {
// TODO Auto-generated constructor stub
}
#Override
public void setOrderNum(int orderNumber) {
number=orderNumber;
}
#Override
public void setYear(int year) {
this.year=year;
}
#Override
public void setOwner(String ownerName) {
this.ownerName=ownerName;
}
#Override
public void setMake(String make) {
this.make=make;
}
#Override
public void setModel(String model) {
this.model=model;
}
#Override
public String getOwner() {
return ownerName;
}
#Override
public String getMake() {
return make;
}
#Override
public String getModel() {
// TODO Auto-generated method stub
return model;
}
#Override
public int getOrderNum() {
// TODO Auto-generated method stub
return number;
}
#Override
public int getYear() {
// TODO Auto-generated method stub
return year;
}
#Override
public String getMakeModelYear() {
// TODO Auto-generated method stub
return make+ " "+ model+ " "+ year+ " ";
}
#Override
public boolean equals(ServiceOrder otherServiceOrder) {
if (getOrderNum()==otherServiceOrder.getOrderNum())
return true;
else
return false;
}
#Override
public int compareTo(ServiceOrder otherServiceOrder, int key) {
int comparisonResult=0;
if(key==1)
{
if(getOrderNum()< otherServiceOrder.getOrderNum())
comparisonResult= -1;
if(getOrderNum()== otherServiceOrder.getOrderNum())
comparisonResult= 0;
if(getOrderNum()> otherServiceOrder.getOrderNum())
comparisonResult= 1;
}
else if(key==2)
{
comparisonResult = getOwner().compareTo(otherServiceOrder.getOwner());
}
else if(key==3)
{
comparisonResult = getOwner().compareTo(otherServiceOrder.getOwner());
}
return comparisonResult;
}
#Override
public int compareTo(myServiceOrder arg0) {
// TODO Auto-generated method stub
return 0;
}
}
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Collections;
import java.util.LinkedList;
import java.util.Scanner;
import javax.swing.JOptionPane;
public class List extends LinkedList<myServiceOrder> {
private static LinkedList<myServiceOrder> newList = new LinkedList();
public void Print() throws Exception
{
System.out.println(newList);
}
public LinkedList<myServiceOrder> createServiceOrder(File inFile) throws Exception {
int number=0;
String ownerName="";
String make="";
String model="";
int year=0;
myServiceOrder serviceList = new myServiceOrder();
Scanner fileScan=new Scanner(inFile);
while (fileScan.hasNext())
{
String ignore;
number = fileScan.nextInt();
//System.out.println(number);
ignore = fileScan.nextLine(); // ignore the newline
ownerName = fileScan.nextLine();
// System.out.println(ownerName);
make = fileScan.nextLine();
// System.out.println(make);
model = fileScan.nextLine();
// System.out.println(model);
year = fileScan.nextInt();
// System.out.println(year);
ignore = fileScan.nextLine(); // ignore the newline
serviceList = new myServiceOrder( number, ownerName, make, model, year);
newList.add(serviceList);
}
fileScan.close();
// System.out.println(newList.viewAll());
return newList;
}
}
Ok I see, my was I dense. I also have a second question: I have to sort the list three different ways depending in my GUI what option I select, I assume that I implement Comparable, but in my compareTo interface it is compareTo(Object o, int key). How can I use that key if the sort method is just Object o. Should I try using a Comparator? if my key=1 how can I tell it to sort that way in my List class?
Classic case of a missing override of the toString() method in your myServiceOrder class.
Take a look here for examples in implementation. This page and Rohit's answer give explanations as to why you need to override toString().
Argh didn't see your second question until now when it's very late:
See this question and this question on the differences between using the Comparable interface vs using the Comparator interface.
How would Java know how you want myService objects to be printed? You can tell it by overriding toString:
#Override
public String toString() {
return "myServiceObject#" + number + "[" + ownername + ", " + make + ", " + model + ", " + year + "]";
}
System.out.println(newList);
This automatically calls the toString() method of the LinkedList class which in turn calls toString() on each of the references in the list (your ServiceOrder objects, in this case). Since you have not provided your own toString() method, the default one in Object is used. This gives the funny output myServiceOrder#3bc1cac which is Java's default way of printing a reference variable. If you wish to see something else, you need to tell Java how to do this by implementing toString() in your ServiceOrder class.
What gets printed is actually the hashcode of the object you print without overriding toString method.. Now since you're printing LinkedList, you can't do that.. Rather you can iterate over the list and print individual element: -
public void Print() throws Exception
{
for (myServiceOrder so: newList) {
System.out.println(so)
}
}
Now, since serviceOrder is itself an object.. You would need to override your toString() in that class..
#Override
public String toString() {
return this.ownerName + this.make + "[" + this.model + " - " + String.valueOf(this.year) + "]";
}

Categories