Search code examples
javaloopsfor-loopoptimizationargs

Java: Looping array with for() with args[x]


I have this simple code, but wanna loop the args in order for each time the user wants to input data on it can be refreshed, can i do this with for()? and if so... how i do implement this the best way possible?

class t1{  
public static void main(String args[]){  
System.out.println("Your name is: "+args[0]+" "+args[1]+" "+args[2]+" "+args[3]);   
}
} 

Solution

  • Try this:

    public static void main(String args[]){  
        System.out.print("Your name is:");
        for(String arg : args) {
            System.out.print(" " + arg);
        }
        System.out.println();
    }