How to get variables from the command line in Java? -
i've finished writing java program drinksbot. have 1 issue; need 2 variables command line saved int variables eg.
the user types in:
java drinksbot 30 40
and program begins run, , saves
int cupstock= 30; int shotstock = 40; //or whatever user typed command line.
any appreciated; i'm new this!
my code begins this:
import java.util.scanner; public class drinksbot { static scanner keyboard = new scanner (system.in); public static void main(string[] args) { system.out.print("hello, what's name? have"+cupstock+" cups left, , " + shotstock+" shots left.);
... continues run rest of program etc.
read args[]
array in main method. variables passed in command line assigned string array defined in main method.
public static void main(string[] args) { string cupstock = args[0]; string shotstock = args[1]; system.out.print("hello, what's name? have"+cupstock+" cups left, , " + shotstock+" shots left.); }
run java drinksbot 30 40
30 cupstock , 40 shotstock
update:
to value integer convert string int
int cupstock = integer.parseint(args[0]); int shotstock = integer.parseint(args[1]);
Comments
Post a Comment