java - reading char array of number more than 10 -
I'm not sure how I can read a number like 10 and above for a four array, have been converted into postfix, currently I have a point I able to do this, but it would be wrong to answer when evaluating postfix equation comes in many points. For example 10 + 13 + 4 will give this wrong answer, but 1 + 3 + 4 would be correct.
// class main Kailkprsr time = new Kailkprrs ( "13 + 20 + 3 + 4"); Calc.toPostfix (); Calc.displayPostfix (); Calc.evaluatePostfix (); Calc.displayResult (); // The above results will be for 1320 + 3 + 4 + 9.0 / square. CalPPerser Public Nil Postfix (for (CR C: originalExp.toCharArray ()) {Switch (C) {Case '0': Case '1' Case '3': Case '4': Case '5': Case '6': Case '7': Case '8': Case '9': Postfix XP.Append (C); Breach; Case '+': Case '-': if (postfixStack.empty ()) {postfixStack. push (c);} Else {if ((postfixStack.peek). equals (C))) {postfixExp.append (postfixStack.pop ()); PostfixStack.push (c); } Else {postfixExp.append (postfixStack.pop ()); PostfixStack.push (c); } } break; }} While (! PostfixStack.empty ()) {postfixExp.append (postfixStack.pop ()); }} Public EvaluationPostics () {String Postfix = Postfix XP.posting (); Case: 'Case': Case '2': Case '3': Case '4': Case '5': For Case '6' (Four C: Postfix. Cochrane ()) {Switch (C) ': Case '7': Case '8': Case '9': Postfix Area Stacks. Push (C); break; Case '+': First Opport = Double. PRSD Double (PostfixSurstStopPop (.) ToString ()); Second opand = double. PRSABB (Postfix responsestoppop () .toastring ()); PostfixResultStack.push (FirstOver + SecondOpend); break; Case '-': First Operation = Double. Prsd double (postfixststoppop () .tostring ()); Second opand = double. PRSABB (Postfix responsestoppop () .toastring ()); PostfixResultStack.push (FirstOperand - Second Operand); break; }}}}
The solution is to use it:
scanner sc = new scanner ("12 + 3 - 456"). UseDelimiter ("\\ b"); While (sc.hasNext ()) {if (sc.hasNextInt ()) {int num = sc.nextInt (); System.out.printf ("An integer [% s]% n", number) found; } Else {String op = sc.next (). Trim (); System.out.printf ("got an option [% s]% n", op); }}
The above print ():
found an integer [12] found a session [+] found an int [3] found an op [-] An integer is obtained [456]
is the delimiter \ b
(backslash is double in a Java string), which is the "word boundary". It occurs between the other characters between the word character (code is) and non-word characters \ w
(which are operators).
As a warning, this delimiter will not properly calculate the floating point numbers with reggex decimal points, but more specific regex can be written to solve those specific issues.
Note also scanner
to String
to parse the int
; If you do happen way can that throws it never an exception (such as the first to admit that it hasNextInt ()
).
Related Questions
- - A quick
scanner
Comments
Post a Comment