i have two classes:parentbox and chiildbox

class parentbox
{
  int width;
  int height;
  int length;
}
class chiildbox
{
  public static void main(String args[])
   {
     parentbox babybox1= new parentbox();
parentbox babybox2=babybox1;     
babybox1.height=10;
     babybox1.length=19;
     babybox1.width=2;
     int vol;
     System.out.println("the vol of babybox1 is"+(babybox1.height*babybox1.length*babybox1.width));
     //parentbox babybox2=babybox1;
     System.out.println("Now babybox2 contains:");
     System.out.println("babybox2.height=",+babybox2.height);
     System.out.println("babybox2.length=",+babybox2.length);
     System.out.println("babybox2.width=",+babybox2.width);
  
  }
 }

here i am trying to assign babybox1 values to babybox2.
i am getting 3 errors:
chiildbox.java:14: cannot find symbol
symbol : method println(java.lang.String,int)
location: class java.io.PrintStream
System.out.println("babybox2.height=",+babybox2.height);
^
chiildbox.java:15: cannot find symbol
symbol : method println(java.lang.String,int)
location: class java.io.PrintStream
System.out.println("babybox2.length=",+babybox2.length);
^
chiildbox.java:16: cannot find symbol
symbol : method println(java.lang.String,int)
location: class java.io.PrintStream
System.out.println("babybox2.width=",+babybox2.width);

Recommended Answers

All 4 Replies

The println method takes just ONE parameter, a String expression. You are trying to pass a String and and int as two parameters (separated by a ,) so you get an error saying it can't find a println method that takes a String and an int as parameters.
You can concatenate stuff together to make a single String by using the + operator, eg
String eg = "This is the year " + 2011;
(When you + a String and a number it converts the number to String and concatenates it.)

The println method takes just ONE parameter, a String expression. You are trying to pass a String and and int as two parameters (separated by a ,) so you get an error saying it can't find a println method that takes a String and an int as parameters.
You can concatenate stuff together to make a single String by using the + operator, eg
String eg = "This is the year " + 2011;
(When you + a String and a number it converts the number to String and concatenates it.)

i didn't quite understand what you explained.
But i think,as you said i have used the + operator to print babybox2.length ,babybox2.width and babybox2.height.

Yes, you got very close, but

("babybox2.height=",+babybox2.height) // two parameters

isn't the same as

("babybox2.height=" +babybox2.height) // one parameter

Yes, you got very close, but

("babybox2.height=",+babybox2.height) // two parameters

isn't the same as

("babybox2.height=" +babybox2.height) // one parameter

Ok thanks, i got it.
That was a habit of putting , as in c language like printf("no=%d",no) .

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.