Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

solving problems

 
bhavneet kaur
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 333
Firefox Browser Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have to pass a run-time argument of at least length of 3.
 
bhavneet kaur
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 333
Firefox Browser Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
dear still i am not getting can you explain in detail


 
Enkita mody
Ranch Hand
Posts: 333
Firefox Browser Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 333
Firefox Browser Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@bhavneet kaur--------- please use UseAMeaningfulSubjectLine (<----click) and use UseCodeTags (<----Click) and once again Welcome to Ranch!!!
 
bhavneet kaur
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 333
Firefox Browser Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 333
Firefox Browser Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 333
Firefox Browser Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic