Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Help in java code

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help in java code

    I'm learning data structure from "Introduction to Java Programming Liang" and here are the codes from Liang's book but I don't understand. Please help me out.
    From line 38 to line 57 there are two conditions:
    (this program asked to calculate a string when your entered on google searchlike (3+2)-3+5*3/3 well, something like that)

    else if (token.charAt(0) == '+' || token.charAt(0) == '-')

    and the other one is :

    else if (token.charAt(0) == '*' || token.charAt(0) == '/')

    What is the purpose of that split? Why didn't he put them all like this:

    if (token.charAt(0) == '+' || token.charAt(0) || token.charAt(0) == '*' || token.charAt(0) == '/' == '-')

    and why in the while loop, the first one we need 4 conditions to end the loop
    (operatorStack.peek() == '+' ||
    operatorStack.peek() == '-' ||
    operatorStack.peek() == '*' ||
    operatorStack.peek() == '/')) {

    but on the second "if else" there were only two
    operatorStack.peek() == '*' ||
    operatorStack.peek() == '/'))

    Here is the complete codes from Liang's book



    1 import java.util.Stack;
    2
    3 public class EvaluateExpression {
    4 public static void main(String[] args) {
    5 // Check number of arguments passed
    6 if (args.length != 1) {
    7 System.out.println(
    8 "Usage: java EvaluateExpression \"expression\"");
    9 System.exit(1);
    10 }
    11
    12 try {
    13 System.out.println(evaluateExpression(args[0]));
    14 }
    15 catch (Exception ex) {
    16 System.out.println("Wrong expression: " + args[0]);
    17 }
    18 }
    19
    20 /** Evaluate an expression */
    21 public static int evaluateExpression(String expression) {
    22 // Create operandStack to store operands
    23 Stack<Integer> operandStack = new Stack<Integer>();
    24
    25 // Create operatorStack to store operators
    26 Stack<Character> operatorStack = new Stack<Character>();
    27
    28 // Insert blanks around (, ), +, -, /, and *
    29 expression = insertBlanks(expression);
    30
    31 // Extract operands and operators
    32 String[] tokens = expression.split(" ");
    33
    34 // Phase 1: Scan tokens
    35 for (String token: tokens) {
    36 if (token.length() == 0) // Blank space
    37 continue; // Back to the while loop to extract the next token
    38 else if (token.charAt(0) == '+' || token.charAt(0) == '-') {
    39 // Process all +, -, *, / in the top of the operator stack
    40 while (!operatorStack.isEmpty() &&
    41 (operatorStack.peek() == '+' ||
    42 operatorStack.peek() == '-' ||
    43 operatorStack.peek() == '*' ||
    44 operatorStack.peek() == '/')) {
    45 processAnOperator(operandStack, operatorStack);
    46 }
    47
    48 // Push the + or - operator into the operator stack
    49 operatorStack.push(token.charAt(0));
    50 }
    51 else if (token.charAt(0) == '*' || token.charAt(0) == '/') {
    52 // Process all *, / in the top of the operator stack
    53 while (!operatorStack.isEmpty() &&
    54 (operatorStack.peek() == '*' ||
    55 operatorStack.peek() == '/')) {
    56 processAnOperator(operandStack, operatorStack);
    57 }
    58
    59 // Push the * or / operator into the operator stack
    60 operatorStack.push(token.charAt(0));
    61 }
    62 else if (token.trim().charAt(0) == '(') {
    63 operatorStack.push('('); // Push '(' to stack
    64 }
    65 else if (token.trim().charAt(0) == ')') {
    66 // Process all the operators in the stack until seeing '('
    67 while (operatorStack.peek() != '(') {
    68 processAnOperator(operandStack, operatorStack);
    69 }
    70
    71 operatorStack.pop(); // Pop the '(' symbol from the stack
    72 }
    73 else { // An operand scanned
    74 // Push an operand to the stack
    75 operandStack.push(new Integer(token));
    76 }
    77 }
    78
    79 // Phase 2: process all the remaining operators in the stack
    80 while (!operatorStack.isEmpty()) {
    81 processAnOperator(operandStack, operatorStack);
    82 }
    83
    84 // Return the result
    85 return operandStack.pop();
    86 }
    87
    88 /** Process one operator: Take an operator from operatorStack and
    89 * apply it on the operands in the operandStack */
    90 public static void processAnOperator(
    91 Stack<Integer> operandStack, Stack<Character> operatorStack) {
    92 char op = operatorStack.pop();
    93 int op1 = operandStack.pop();
    94 int op2 = operandStack.pop();
    95 if (op == '+')
    96 operandStack.push(op2 + op1);
    97 else if (op == '-')
    98 operandStack.push(op2 - op1);
    99 else if (op == '*')
    100 operandStack.push(op2 * op1);
    101 else if (op == '/')
    102 operandStack.push(op2 / op1);
    103 }
    104
    105 public static String insertBlanks(String s) {
    106 String result = "";
    107
    108 for (int i = 0; i < s.length(); i++) {
    109 if (s.charAt(i) == '(' || s.charAt(i) == ')' ||
    110 s.charAt(i) == '+' || s.charAt(i) == '-' ||
    111 s.charAt(i) == '*' || s.charAt(i) == '/')
    112 result += " " + s.charAt(i) + " ";
    113 else
    114 result += s.charAt(i);
    115 }
    116
    117 return result;
    118 }
    119 }

    Thank you very much!


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help in java code

    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE GOES HERE
    [/code]
    to get highlighting and preserve formatting.

    Be sure to remove the line numbers and properly indent the nested statements.

    What is the purpose of that split?
    What is the split you talk about?

    The author's lack of comments in the code describing why he coded something that way should be explained in the text.

    NOTE: I don't try to read code that is not properly formatted in code tags.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Help in java code

    Wow! Your first post of 2 total was nearly 2 years ago. You said at that time you'd been studying Java for a year and a half. Welcome back to the forum, and I wish you luck at continuing your study.

    Please read this topic to learn how to post code in code or highlight tags and other useful info for new members.

    Please post your code correctly using code or highlight tags per the above link.

  4. #4
    Junior Member
    Join Date
    Nov 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help in java code

    @GregBrannon: Yes, and I quitted it, took web design class, have front-end web design job. Now, want to work on back-end(pay more) therefore, I'm going back to java.

  5. #5
    Member
    Join Date
    May 2013
    Posts
    106
    My Mood
    Amused
    Thanks
    16
    Thanked 9 Times in 9 Posts

    Default Re: Help in java code

    I've been studying web development too. I pick up the back end stuff pretty quickly but front end always gives me trouble. HTML and CSS aren't really tough but I feel like good front end stuff requires an artistic bent that I don't have.

    Like the others have said, embed your code stuff in the code tags. It's tough to read as is.

Similar Threads

  1. Replies: 0
    Last Post: May 23rd, 2013, 04:35 PM
  2. Trouble Porting my Java File Reading Code to Android Code
    By Gravity Games in forum Android Development
    Replies: 0
    Last Post: December 6th, 2012, 04:38 PM
  3. Replies: 5
    Last Post: November 14th, 2012, 10:47 AM
  4. Replies: 3
    Last Post: September 3rd, 2012, 11:36 AM
  5. problem in my code java code graph editeur
    By kisokiso in forum Java Theory & Questions
    Replies: 5
    Last Post: January 6th, 2012, 08:36 AM