Skip to main content
AI Assist is now on Stack Overflow. Start a chat to get instant answers from across the network. Sign up to save and share your chats.
added 145 characters in body
Source Link
Ravi K Thapliyal
  • 51.9k
  • 9
  • 80
  • 90

Your main() method can take input parameters of type String if your program is run through a console like

java YourClass arg1 arg2 

Now, within mainmain() if you iterate the String [] like

for (arg : arguments) System.out.println(arg); 

it should print

arg1 arg2 

Demo :

public class AddTwoNumbers { public static void main(String[] args) {   if(args.length == 2) {  try { int a = Integer.parseInt(args[0]); int b = Integer.parseInt(args[1]); System.out.println("a + b = " + a + " + " + b + " = "+ (a + b)); } catch (NumberFormatException e) { System.err.println("Invalid Input: Please enter numbers."); }   } else {  System.err.println("Missing Input: Please enter TWO numbers."); } } } 

You can run this throughon your console as

java AddTwoNumbers 2 3 

and it should print

a + b = 2 + 3 = 5 

Your main() method can take input parameters of type String if your program is run through a console like

java YourClass arg1 arg2 

Now, within main if you iterate the String [] like

for (arg : arguments) System.out.println(arg); 

it should print

arg1 arg2 

Demo :

public class AddTwoNumbers { public static void main(String[] args) { try { int a = Integer.parseInt(args[0]); int b = Integer.parseInt(args[1]); System.out.println("a + b = " + a + " + " + b + " = "+ (a + b)); } catch (NumberFormatException e) { System.err.println("Invalid Input: Please enter numbers."); } } } 

You can run this through console as

java AddTwoNumbers 2 3 

and it should print

a + b = 2 + 3 = 5 

Your main() method can take input parameters of type String if your program is run through a console like

java YourClass arg1 arg2 

Now, within main() if you iterate the String [] like

for (arg : arguments) System.out.println(arg); 

it should print

arg1 arg2 

Demo :

public class AddTwoNumbers { public static void main(String[] args) {   if(args.length == 2) {  try { int a = Integer.parseInt(args[0]); int b = Integer.parseInt(args[1]); System.out.println("a + b = " + a + " + " + b + " = "+ (a + b)); } catch (NumberFormatException e) { System.err.println("Invalid Input: Please enter numbers."); }   } else {  System.err.println("Missing Input: Please enter TWO numbers."); } } } 

You can run this on your console as

java AddTwoNumbers 2 3 

and it should print

a + b = 2 + 3 = 5 
edited body
Source Link
Paul Bellora
  • 55.4k
  • 18
  • 135
  • 185

Your main() method can take input parameters of type String if your program is run through a console like

java YourClass arg1 arg2 

Now, within main if you iterate the String [] like

for (arg : arguments) System.out.println(arg); 

it should print

arg1 arg2 

Demo :

public class AddTwoNumbers { public static void main(String[] args) { try { int a = Integer.parseInt(args[0]); int b = Integer.parseInt(args[0]args[1]); System.out.println("a + b = " + a + " + " + b + " = "+ (a + b)); } catch (NumberFormatException e) { System.err.println("Invalid Input: Please enter numbers."); } } } 

You can run this through console as

java AddTwoNumbers 2 3 

and it should print

a + b = 2 + 3 = 5 

Your main() method can take input parameters of type String if your program is run through a console like

java YourClass arg1 arg2 

Now, within main if you iterate the String [] like

for (arg : arguments) System.out.println(arg); 

it should print

arg1 arg2 

Demo :

public class AddTwoNumbers { public static void main(String[] args) { try { int a = Integer.parseInt(args[0]); int b = Integer.parseInt(args[0]); System.out.println("a + b = " + a + " + " + b + " = "+ (a + b)); } catch (NumberFormatException e) { System.err.println("Invalid Input: Please enter numbers."); } } } 

You can run this through console as

java AddTwoNumbers 2 3 

and it should print

a + b = 2 + 3 = 5 

Your main() method can take input parameters of type String if your program is run through a console like

java YourClass arg1 arg2 

Now, within main if you iterate the String [] like

for (arg : arguments) System.out.println(arg); 

it should print

arg1 arg2 

Demo :

public class AddTwoNumbers { public static void main(String[] args) { try { int a = Integer.parseInt(args[0]); int b = Integer.parseInt(args[1]); System.out.println("a + b = " + a + " + " + b + " = "+ (a + b)); } catch (NumberFormatException e) { System.err.println("Invalid Input: Please enter numbers."); } } } 

You can run this through console as

java AddTwoNumbers 2 3 

and it should print

a + b = 2 + 3 = 5 
added 485 characters in body
Source Link
Ravi K Thapliyal
  • 51.9k
  • 9
  • 80
  • 90

Your main() method can take input parameters of type String if your program is run through a console like

java yourClassYourClass arg1 arg2 

Now, within main if you iterate the String[]String [] like

for (arg : arguments) System.out.println(arg); 

it should print

arg1 arg2 

Demo :

public class AddTwoNumbers { public static void main(String[] args) { try { int a = Integer.parseInt(args[0]); int b = Integer.parseInt(args[0]); System.out.println("a + b = " + a + " + " + b + " = "+ (a + b)); } catch (NumberFormatException e) { System.err.println("Invalid Input: Please enter numbers."); } } } 

You can run this through console as

java AddTwoNumbers 2 3 

and it should print

a + b = 2 + 3 = 5 

Your main() method can take input parameters of type String if your program is run through a console like

java yourClass arg1 arg2 

Now, within main if you iterate the String[] like

for (arg : arguments) System.out.println(arg); 

it should print

arg1 arg2 

Your main() method can take input parameters of type String if your program is run through a console like

java YourClass arg1 arg2 

Now, within main if you iterate the String [] like

for (arg : arguments) System.out.println(arg); 

it should print

arg1 arg2 

Demo :

public class AddTwoNumbers { public static void main(String[] args) { try { int a = Integer.parseInt(args[0]); int b = Integer.parseInt(args[0]); System.out.println("a + b = " + a + " + " + b + " = "+ (a + b)); } catch (NumberFormatException e) { System.err.println("Invalid Input: Please enter numbers."); } } } 

You can run this through console as

java AddTwoNumbers 2 3 

and it should print

a + b = 2 + 3 = 5 
Source Link
Ravi K Thapliyal
  • 51.9k
  • 9
  • 80
  • 90
Loading