variable argument not working

someone tell why this not working

class Init

{

public void assign(String... args)

{

for(Object str:args)

System.out.println(str);

}

}

public class VarArg1

{

public static void main(String args[])

{

Init abc=new Init();

abc.assign("asdfasd","sdfas","asdfa","adfas");

abc.assign(2,'c',"asdfa",5.333);//can send different types as i am recieving it with an object array.

}

}

[482 byte] By [SaveTheNewbiea] at [2007-11-26 18:36:26]
# 1
abc.assign(2,'c',"asdfa",5.333);//can send different types as i am recieving it with an object array.You are not receiving it with an object array. You are receiving it with a String array and these aren't all Strings
zadoka at 2007-7-9 6:10:28 > top of Java-index,Java Essentials,New To Java...
# 2

When you post code, please use[code] and [/code] tags as described in [url=http://forum.java.sun.com/help.jspa?sec=formatting]Formatting tips[/url] on the message entry page. It makes it much easier to read.

Also, it would help if you'd provide more detail than "not working."

However, you appear confused about one thing:

//can send different types as i am recieving it with an object array.

No, you're using a Sring array, and 2, 'c', and 5.333 are not Strings.

jverda at 2007-7-9 6:10:28 > top of Java-index,Java Essentials,New To Java...
# 3

> someone tell why this not working

>

> class Init

> {

> public void assign(String... args)

> {

> for(Object str:args)

> System.out.println(str);

> }

> }

>

> public class VarArg1

> {

> public static void main(String args[])

> {

> Init abc=new Init();

> abc.assign("asdfasd","sdfas","asdfa","adfas");

> abc.assign(2,'c',"asdfa",5.333);//can send

> nd different types as i am recieving it with an

> object array.

This is wrong. You are recieving with an String array and here you have primitives.

> }

> }

Delete this line

abc.assign(2,'c',"asdfa",5.333);

and it will work.

qUesT_foR_knOwLeDgea at 2007-7-9 6:10:28 > top of Java-index,Java Essentials,New To Java...
# 4

> someone tell why this not working

>

> class Init

> {

> public void assign(String... args)

> {

> for(Object str:args)

> System.out.println(str);

> }

> }

>

> public class VarArg1

> {

> public static void main(String args[])

> {

> Init abc=new Init();

> abc.assign("asdfasd","sdfas","asdfa","adfas");

> abc.assign(2,'c',"asdfa",5.333);//can send

> nd different types as i am recieving it with an

> object array.

> }

> }

You can *not* send it types other than Strings.. you declare the method as "public void assign(String... args)" .. the method is expecting to be passd Strings. If you changed it to "public void assign(Object... args)" then you could pass it other stuff. However, I don't think I'd recommend that approach to programming. It'd work, but you'd eventually get some weird output as every Object's toString() method was invoked that you sent to it. (and as we all know, they're not always standardized)

JJCoolBa at 2007-7-9 6:10:28 > top of Java-index,Java Essentials,New To Java...
# 5
but i get code from teacher and teacher saying get output....?
SaveTheNewbiea at 2007-7-9 6:10:28 > top of Java-index,Java Essentials,New To Java...
# 6

Either your professor is mad or he/she is testing you.

Anyways i didn't notice this

public void assign(String... args)

{

for(Object str:args)

System.out.println(str);

}

}

Make this

public void assign(Object... args)

{

for(Object str:args)

System.out.println(str);

}

}

This will work.

So it looks like he has screwed the code to test you students.

qUesT_foR_knOwLeDgea at 2007-7-9 6:10:28 > top of Java-index,Java Essentials,New To Java...
# 7
> but i get code from teacher and teacher saying get> output....?Then your teacher can't code. I suggest you drop the class and find another professor.
zadoka at 2007-7-9 6:10:28 > top of Java-index,Java Essentials,New To Java...
# 8
> but i get code from teacher and teacher saying get> output....?If the compiler and your teacher disagree, your teacher is wrong.
jverda at 2007-7-9 6:10:28 > top of Java-index,Java Essentials,New To Java...
# 9
> but i get code from teacher and teacher saying get> output....?Did you copy/paste it, download it, get it in an email, etc.?Or did you read it and (mis?)type it in?
jverda at 2007-7-9 6:10:28 > top of Java-index,Java Essentials,New To Java...
# 10
i write it in book
SaveTheNewbiea at 2007-7-9 6:10:28 > top of Java-index,Java Essentials,New To Java...
# 11

> i write it in book

You wrote it in a book?

Do you mean you read it from a book and typed it in here? If so, either the book has a mistake or you made a mistake when copying it. As somebody pointed out, changing String... to Object... will fix it. (The primtives oculd cause a problem, but I think autoboxing will take care of that.)

jverda at 2007-7-9 6:10:28 > top of Java-index,Java Essentials,New To Java...
# 12

> > i write it in book

>

> You wrote it in a book?

>

> Do you mean you read it from a book and typed it in

> here? If so, either the book has a mistake or you

> made a mistake when copying it. As somebody pointed

> out, changing String... to Object... will fix it.

> (The primtives oculd cause a problem, but I think

> autoboxing will take care of that.)

@OP please note that the alternative i suggested is with respect to 1.5 and not the previous versions.

You can read about autoboxing here anyways just incase you don't know

http://java.sun.com/j2se/1.5.0/docs/guide/language/autoboxing.html

Good Luck

qUesT_foR_knOwLeDgea at 2007-7-9 6:10:28 > top of Java-index,Java Essentials,New To Java...
# 13
> @OP please note that the alternative i suggested is> with respect to 1.5 and not the previous versions. Not an issue. If varargs is present, so is autoboxing. They were both introduced in 1.5
jverda at 2007-7-9 6:10:28 > top of Java-index,Java Essentials,New To Java...
# 14

> > @OP please note that the alternative i suggested

> is

> > with respect to 1.5 and not the previous versions.

>

>

> Not an issue. If varargs is present, so is

> autoboxing. They were both introduced in 1.5

Oh! Thanks. I wasn't aware of that.

qUesT_foR_knOwLeDgea at 2007-7-9 6:10:28 > top of Java-index,Java Essentials,New To Java...