-1

this may be blurry to grasp but i'm new to java whole thing and i want to know how can i use cmd to type something in user input

E:\My apps\Java apps\test\src\main\java>java ls.java && echo E:/
enter File name or path :

in this input i want to enter the path via cmd the "&& echo E:/" doesn't work

Stux2K
  • 41
  • 10
  • Welcome to Stack Overflow. Please learn that you are supposed to search before asking a question here and when you post a question, tell us what your search brought up and specify how it fell short of solving your problem. It’s for your own sake since (1) you often find a better answer faster that way (in this case your *very surely* would) (2) it allows us to give preciser and more focused answers. It also tends to prevent or at least reduce the number of downvotes. – Ole V.V. Aug 10 '21 at 03:29
  • For example: [Java User Input (Scanner class) - W3Schools](https://www.w3schools.com/java/java_user_input.asp). – Ole V.V. Aug 10 '21 at 03:31

1 Answers1

2

You can use "Command Line Arguments" in order to give the input when running the program!

Assuming your file name is ls.java

compile it using: javac ls.java

when running it using java command type the arguments in front of it like:

java ls E:/path

Now you can catch the arguments in the program

   public class ls {
         public static void main(String[] args)
         {
              String path  = args[0];
         } 
    }

you can add more arguments by spaces and catch them in String array

java ls E:/path 100 200

System.out.println(args[1]); // will print 1
System.out.println(args[2]; //will print 200

NOTE: As implied all the command line arguments are stored in the form of String, if you want to convert them to another format, you'd need to parse them using methods.