Hi guys I'm a noobie to Java and need some help with writing 4 small functions in my command processor program.
My "reversethm" function compiles but gets a weird error when i type "java doit reverse one two three" (supposed to give me back three two one).
The "findlongest" should make the program print out the argument that is the longest (i.e., contains the most characters). If more than one argument are of equal length, it doesn't matter which is printed out. BUT I'm having trouble with it because I don't know how to count characters instead of the whole word...
The findnth command is followed by a number, and then some arguments. It should print out the argument corresponding to the number. The program should print out a warning message if nothing follows the nth command, or if there aren't enough arguments following the number. Need help no idea how to get started on this one.
This is what I have so far
Code:class doit { // auxiliary functions here public static void findnth (String[] args) { }; public static void findlongest (String[] args) { int i; String templong = args[1]; for (i = 2; i < args.length; ++i) { //if (args[i].length > templong.length) <- need to find a way to count the number of characters templong = args[i]; } System.out.println(templong); }; public static void reversethem (String[] args) { int i; for (i = args.length; i > 1 ; --i) { System.out.println(args[i]); } }; public static void echothem (String[] args) { int i; for (i = 1; i < args.length ; ++i) { System.out.println(args[i]); } }; public static void behelpful () { System.out.println("Commands: help echo nth longest sort reverse"); }; public static void main (String[] args) { if (args.length==0) behelpful(); else if (args[0].equals("echo")) echothem(args); else if (args[0].equals("help")) behelpful(); else if (args[0].equals("nth")) findnth(args); else if (args[0].equals("longest")) findlongest(args); else if (args[0].equals("reverse")) reversethem(args); else { System.out.println ("Unknown command: "+args[0]); behelpful(); } }; };
Thanks for your time guys!


Reply With Quote


Bookmarks