• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

another array problem  RSS feed

 
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I posted a similar post a couple of days ago about adding numbers in an array. I tried to play with that code to add strings together. Basically, if someone enters java addString mary bob, I was trying to get it to display mary + bob = marybob. However, it's not working (obviously, or I wouldn't be posting . I know my code is wrong, but I'm still having trouble following logic in loops, so if anyone could help, thanks! Here's my code so far.


[This message has been edited by tyler jones (edited December 09, 2000).]
 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by tyler jones:
[B]I posted a similar post a couple of days ago about adding numbers in an array. I tried to play with that code to add strings together. Basically, if someone enters java addString mary bob, I was trying to get it to display mary + bob = marybob. However, it's not working (obviously, or I wouldn't be posting . I know my code is wrong, but I'm still having trouble following logic in loops, so if anyone could help, thanks! Here's my code so far.


[This message has been edited by tyler jones (edited December 09, 2000).][/B]


Try this!
class AddString
{
String f;
String g;
private void sumReturn(String args[])
{
for (int i = 0; i < (args.length); i++)
{
if ( i == (args.length - 2))
f = args[i];
if ( i == (args.length - 1))
g = args[i];
if ( i == ( args.length - 1 ))
System.out.print(" + ");
System.out.print(args[i]);
}
System.out.println(" = " + f + g);
}
public static void main(String args[])
{
AddString a = new AddString();
a.sumReturn(args);
}
}
 
tyler jones
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot. Could you explain a bit of the looping logic? That is where I'm having trouble understanding it. Thanks again.
 
Eric Edwards
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by tyler jones:
Thanks a lot. Could you explain a bit of the looping logic? That is where I'm having trouble understanding it. Thanks again.


We have entered two names on the command line. When the names are read in they will populate the first two "storage buckets" of the array. In Java they are numbered 0, 1, 2, etc.
In the for loop, the loop will continue to loop until it is equal to what you set it at. In this case, it is less than args.length.
The first time the loop iterates i is equal to 0. The first if says that if (i is equal to args.length -2), this would be 2 - 2 = 0. So, I does equal 0, so it will make String f = the value of
args[i] and at this point, i = 0, so it will put String f = the value of the first "storage bucket" in the array, which is args[0].
Then it will check the other if statements.
Then it will loop or iterate and the value of i will be increased by 1 because you have i++ as the incrementor of your for loop. So now i is equal to 1.
So now it will check the if statements based on i being 1
Then it will iterate again this time i will become 2.
Now, since i is now eqial to two, and you said it should iterate while i < args.length (which is 2), it will no longer iterate.
So, for (i = 0(this is the beginning value of i); i < args.length( this is where you tell it to iter until this condition exists); i++(this is what increments the value of i).
I hope all ths babble has helped.
Good Luck!
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Eric there is a problem with the solution that U have given
It will give correct o/p if we give two command line parameters.
The program will fail if we give less than two or greator than two parameters.
If we want it to work for any number of parameters then I think this will do.
class AddString
{
private void sumReturn(String args[])
{
String str = new String();
int i = 0;
if (args.length > 0)
{
for (int j = 0; j < args.length; j++)
{
str=str+args[j];
}
for(i = 0; i < args.length - 1; i++)
{
System.out.print(args[i] + "+");
}
System.out.println(args[i] + " = " + str);
}
}
public static void main(String args[])
{
AddString a = new AddString();
a.sumReturn(args);
}
}

Regards
Ashish
[Added spaces after '<' and elsewhere so that browsers don't confuse your code with HTML - Jim]
[This message has been edited by Jim Yingst (edited December 09, 2000).]
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ashish - anytime you use the '<' symbol in this forum you should follow it with a space character so that the browser doesn't think that you're trying to write an HTML tag, which won't be displayed in most cases. I also put in spaces around most of the other operators just because I think code looks better that way .
Also, you should be aware that JavaRanch has a naming policy, which you can read about here. Please re-register with an account name that uses a space rather than an underscore within your name. Thank you.
 
Eric Edwards
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Asheesh_talwar:
Eric there is a problem with the solution that U have given
It will give correct o/p if we give two command line parameters.
The program will fail if we give less than two or greator than two parameters.
If we want it to work for any number of parameters then I think this will do.
class AddString
{
private void sumReturn(String args[])
{
String str = new String();
int i = 0;
if (args.length > 0)
{
for (int j = 0; j < args.length; j++)
{
str=str+args[j];
}
for(i = 0; i < args.length - 1; i++)
{
System.out.print(args[i] + "+");
}
System.out.println(args[i] + " = " + str);
}
}
public static void main(String args[])
{
AddString a = new AddString();
a.sumReturn(args);
}
}

Regards
Ashish
[Added spaces after '<' and elsewhere so that browsers don't confuse your code with HTML - Jim]
[This message has been edited by Jim Yingst (edited December 09, 2000).]


Yes, you are correct. There are many ways to do this. I thought about this, and he was concerned mainly with the logic of a loop, and not of the command line being read in for any number of parms, so, to simplify things, I did it my way.
Regards,
Eric
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!