user input - Inputting different variable types in one time in Java -
i made calculator in c++ , i'm trying recreate mirror-like in java.
there 2 double variables(double a , double b)for 2 operands , char(char op) put in if cycle, instace if op = '+' cout << << op << b << "=" << a+b << endl;.
so write 12+2 in console prompt , see 12+2=14 output.
now in java have 1 per line:
scanner scin = new scanner(system.in); system.out.println("operation>"); = scin.nextdouble(); op = scin.next().charat(0); b = scin.nextdouble(); and have write value , press return each time. how can input in 1 time c++, , maybe in 1 line of code? in advance.
you can't read in multiple variables @ once using scanner, have read them in 1 @ time. however, there nice way allow inputs occur without hitting enter each time or inputting space: set different delimiter! default delimiter whitespace (which includes newlines), set word boundary \b regex.
scanner in = new scanner(system.in).usedelimiter("\\b|\\s+"); now can read in 12+2 , split next calls want them, or can continue hit enter, or can put spaces between values. choice :d
to restore delimiter normal afterwards, use in.reset().
edit: keep word boundary splitting input @ decimal point, use pattern:
(?<!\\.)\\b(?!\\.)|\\s+
Comments
Post a Comment