Inaccurate for loop in Java array -
i've tried many hours solve array task, seems me i'm stuck. task create program prints number of given command line parameters , lists them.
you gave 2 command line parameters. below given parameters: 1. parameter: 3455 2. parameter: john_smith
my program starts wrong index + i'm not sure given task. how program know how many parameters use if hasn't been initialized? or lost exercise?
this i've done:
import java.util.scanner; public class ex_01 { public static void main(string[] args) { // todo auto-generated method stub scanner reader = new scanner(system.in); int param = reader.nextint(); string[] matrix = new string[param]; (int = 0; < matrix.length; i++) { matrix[i] = reader.nextline();// part loop fails } system.out.println("you gave " + matrix.length + " command line parameters.\nbelow given parameters:"); (int = 0; < matrix.length; i++) { system.out.println(i + 1 + " parameter: " + matrix[i]); } } }
and own output:
3 //this number of how many parameters user wants input 2 // third one? omg // gave 3 command line parameters. below given parameters: 1 parameter: 2 parameter: 2 3 parameter: omg
edit:
i did it! did it!! after more googling found this:
if (args.length == 0) { system.out.println("no arguments given."); } else { (string : args) { } }
then modified program , voilĂ program compiled. here whole program:
import java.util.scanner; public class echo { public static void main(string[] args) { if (args.length == 0) { system.out.println("no arguments given."); } else { (string : args) { } } system.out.println("you gave " + args.length + " command line parameters.\nbelow given parameters:"); (int = 0; < args.length; i++) { system.out.println(i + 1 + ". parameter: " + args[i]); } } }
i want thank answered this, needed! :)
command line arguments not text read program scanner
, like, strings specified in args
array argument main. seems need output contents of array instead of reading input , outputting it.
Comments
Post a Comment