0

Can anyone please help me how to get user input in java programs in vs code without using public static void main (String [] args)

I can safely do this thing using method call in BlueJ ide but couldn't figure out how to do this in vs code or any other ides....

Here is the simple code that I want to run--

public class Rectangle
{
    public static void main (int l, int b)
    {
        int area, peri;
        area = l*b;
        peri = 2*(l+b);
        System.out.println("Area = "+area);
        System.out.println("Perimeter = "+peri);
    } 
}
  • 1
    Why don't you want to use the standard ``public static void main(String[] args)`` method? – NomadMaker Nov 20 '20 at 13:36
  • Actually I wanted to know if there are any possibilities of method calling like in BlueJ ide.... in vs code........ – mastero Nov 20 '20 at 15:31

3 Answers3

1

There isn't a practical alternative if you are writing a stand-alone command line application in Java.

There are various libraries for command line argument parsing in Java, but (AFAIK) they all require you to write a public static void main (String [] args) method .... even if it is just simple boilerplate code.

(This is not true for all kinds of Java application. For example webapps in a web container, JavaFX applications, or ... applets)

Stephen C
  • 669,072
  • 92
  • 771
  • 1,162
  • in BlueJ ide there is a GUI Method call in which we have to pass our arguments (User Input)......it is not a command line input either.......so I wanted to know if there are any such cases/possibilities in vs code or any other such ides. – mastero Nov 20 '20 at 15:37
  • Basically, no there isn't. Most IDEs are designed for developing production code. This is not something that standard JVMs support. Hence supporting this in an IDE is counter-productive. You should be learning how to write programs the *normal* way not the BlueJ way. ('Cos code that does things the BlueJ way won't work outside of BlueJ.) – Stephen C Nov 20 '20 at 16:05
1
You cant run a program with out main program. Execution always starts from main program.Your IDE may automatically generate main program, But if you are looking for an alternative way of ( other than command line argument)  getting input  you can use Scanner or any input stream methods 



 import java.util.Scanner
    public class Rectangle
    { 
        public static void main(String arg[])
        {    Scanner myObj = new Scanner(System.in);  // Create a Scanner object
            int l= myObj.nextInt();  // Read user input
            int b=myObj.nextInt(); 
            int area = l*b;
            int peri = 2*(l+b);
            System.out.println("Area = "+area);
            System.out.println("Perimeter = "+peri);
        } 
    }
RatheeshTS
  • 373
  • 1
  • 15
  • in BlueJ ide there is a GUI Method call in which we have to pass our arguments (User Input)......it is not a command line input either.......so I wanted to know if there are any such cases/possibilities in vs code or any other such ides. – mastero Nov 20 '20 at 15:38
0

You can't not do that.

As described here a java application entry point has to be public static void main(String[] args).

This makes sense. The parameters from the command line arguments are Strings, not matter what. To make int out of it, there would be an implicit conversion. Where do you catch parsing errors for that if it happens before it reaches your code?

Reinhold
  • 504
  • 3
  • 14
  • in BlueJ ide there is a GUI Method call in which we have to pass our arguments (User Input)......it is not a command line input either.......so I wanted to know if there are any such cases/possibilities in vs code or any other such ides. – mastero Nov 20 '20 at 15:37
  • @mastero Sorry I don't know what your IDE exposes here. It is not the main entry point to a java application. VS code default java support is rather mediocre compared to other more specialized IDEs such as eclipse or IntelliJ. – Reinhold Nov 22 '20 at 16:43