• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how do i do this

 
melkor leprichaun
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how do i read an arra backward
if i put 1 2 3 4 i want it to print 4 3 2 1
using this code
public class Echo {
public static void main (String[] args) {
for (int i = 0; i < args.length; i++)
System.out.println(args[i]);
}
}
 
Lisa D'Aniello
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
just reverse your loop:
for (int i = args.length; i >= 0; i--)
{
System.out.println(args[i]);
}
Hope this helps,
Lisa M.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi magical creature,
Welcome to JavaRanch!
Your "for" loop counts from 0 to args.length-1. Instead, make it count from args.length-1 to 0, and you'll have exactly what you want.
 
melkor leprichaun
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for your help it was very helpfull i changed it a little to get it to work here is working code.
public class Echo {
public static void main (String[] args) {
for (int i = args.length -1; i > -1; i--)

System.out.println(args[i]);
}
}
 
William Barnes
Ranch Hand
Posts: 986
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Always use curley braces.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic