This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes solving problems Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "solving problems" Watch "solving problems" New topic
Author

solving problems

bhavneet kaur
Ranch Hand

Joined: Apr 08, 2012
Posts: 32
class C {

public static void main (String[] a1) {

System.out.print(a1[0] + a1[1] +a1[2]);
}}

output : BC and runtime exception


i am unable to understand

please help
Enkita mody
Ranch Hand

Joined: Aug 06, 2012
Posts: 333

You have to pass a run-time argument of at least length of 3.


OCA7
bhavneet kaur
Ranch Hand

Joined: Apr 08, 2012
Posts: 32

i am not getting
during execution on command prompt

java classname ABC

it prints ABC


but java classname AB or java classname A

its not working


Enkita mody
Ranch Hand

Joined: Aug 06, 2012
Posts: 333

bhavneet kaur wrote:
i am not getting
during execution on command prompt

java classname ABC

it prints ABC


but java classname AB or java classname A

its not working



Yes, because your code a1[2] try to access 3rd element of argument and when you don't provide that it will give error.
bhavneet kaur
Ranch Hand

Joined: Apr 08, 2012
Posts: 32
dear still i am not getting can you explain in detail


Enkita mody
Ranch Hand

Joined: Aug 06, 2012
Posts: 333

bhavneet kaur wrote: dear still i am not getting can you explain in detail




When you do run i.e. java classname ABC then

a1[0] is A
a1[1] is B
a1[2] is C

so length of argument needed to run your program is 3.If you don't provide compile error.
Rameshwar Soni
Ranch Hand

Joined: Feb 03, 2011
Posts: 247
Enkita mody wrote:

When you do run i.e. java classname ABC then

a1[0] is A
a1[1] is B
a1[2] is C

.


Incorrect. Here you are passing only one argument i.e. ABC therefore a1[0] is ABC and not what you said.
Enkita mody
Ranch Hand

Joined: Aug 06, 2012
Posts: 333

Rameshwar Soni wrote:
Enkita mody wrote:

When you do run i.e. java classname ABC then

a1[0] is A
a1[1] is B
a1[2] is C

.


Incorrect. Here you are passing only one argument i.e. ABC therefore a1[0] is ABC and not what you said.


Yes, that is.I had in mind A B C but wrote ABC.
Rameshwar Soni
Ranch Hand

Joined: Feb 03, 2011
Posts: 247
bhavneet kaur wrote:class C {

public static void main (String[] a1) {

System.out.print(a1[0] + a1[1] +a1[2]);
}}



Bhavneet, firstly Welcome to Ranch.

As you can see in your code you are using a1[0], a1[1] and a1[2] and a1 is your String array and in arrays index starts from 0.

So in total there has be 3 strings which you need to pass while executing your program.

So you do this
java Classname first_string "space" second_string "space" third_string, ignore the "space" i.e. for example


java C hello bhavneet kaur

Here a1[0] -----> hello

a1[1]-----> bhavneet

a1[2]-------> kaur

If you any more doubts, feel free to ask.

Rameshwar Soni
Ranch Hand

Joined: Feb 03, 2011
Posts: 247
@bhavneet kaur--------- please use UseAMeaningfulSubjectLine (<----click) and use UseCodeTags (<----Click) and once again Welcome to Ranch!!!
bhavneet kaur
Ranch Hand

Joined: Apr 08, 2012
Posts: 32
thank you so much

it means ,as a1 is a string then without space
example java classname ABC
it will be considered as one,so input from command line must be given with space,right.

i think in a website its given wrong they are giving input A B C
and showing output as BC and runtime Exception





bhavneet kaur
Ranch Hand

Joined: Apr 08, 2012
Posts: 32

class C1
{

static int s;

public static void main(String a[])
{

C1 obj=new C1();
obj.m1();
System.out.println(s);
}

void m1()
{
int x=1;
m2(x);
System.out.println(x+"");
}

void m2(int x)
{
x=x*2;
s=x;

}
}

//its output is :
1
2
why the value of x doesnt changed??
Enkita mody
Ranch Hand

Joined: Aug 06, 2012
Posts: 333

bhavneet kaur wrote: thank you so much

it means ,as a1 is a string then without space
example java classname ABC
it will be considered as one,so input from command line must be given with space,right.

i think in a website its given wrong they are giving input A B C
and showing output as BC and runtime Exception







No,if you will run it with java classname ABC then it wont execute, it will give run-time error.because there is only a1[0].

but if you run it with java classname A B C then it will run and will give output ABC.
Enkita mody
Ranch Hand

Joined: Aug 06, 2012
Posts: 333

bhavneet kaur wrote:
class C1
{

static int s;

public static void main(String a[])
{

C1 obj=new C1();
obj.m1();
System.out.println(s);
}

void m1()
{
int x=1;
m2(x);
System.out.println(x+"");
}

void m2(int x)
{
x=x*2;
s=x;

}
}

//its output is :
1
2
why the value of x doesnt changed??


x is a method local variable of m1.It doesn't belongs to m2.
Rameshwar Soni
Ranch Hand

Joined: Feb 03, 2011
Posts: 247
Enkita mody wrote:

No,if you will run it with java classname ABC then it wont compile, it will give compile error.because there is only a1[0].


You use your words properly. How come java classname ABC won't compile ? We are executing our program here rather than compiling.
Enkita mody
Ranch Hand

Joined: Aug 06, 2012
Posts: 333

Rameshwar Soni wrote:
Enkita mody wrote:

No,if you will run it with java classname ABC then it wont compile, it will give compile error.because there is only a1[0].


You use your words properly. How come java classname ABC won't compile ? We are executing our program here rather than compiling.


okay i will use properly, post edited.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: solving problems
 
Similar Threads
Garbage collection question
Objects having Null value
Question Of The Day!!
what is your blood group?
ClassCastException