[headshot4444] Java calculator operator parsing menu

User Tag List

Results 1 to 3 of 3
  1. #1
    nerdtopia's Avatar Banned
    Reputation
    4
    Join Date
    Apr 2012
    Posts
    37
    Thanks G/R
    0/1
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    [headshot4444] Java calculator operator parsing

    Since headshot4444's inbox is full I'll answer his question here, hopefully it will help someone else too.

    Originally Posted by headshot4444
    Hey I heard you saying you're pretty good with a java. I'm a beginner and stuck on something. I'm trying to read in an operator from an input file. Operators such as "+ - * /". Any good idea on how to read them in? Right now I read them in as strings, and use String.equals("+"), or whatever the operator may be, to determine which operator it is. Thanks.
    Sorry for the late response, I don't really check my messages.

    Well you could compare as a char :P It really depends on what the input from the file looks like. If the file says something like the following you can use an infix expression algorithm.

    Infix:
    13*37
    or
    13 * 37
    Postfix:
    13 37 *
    <strike>I could go into the best way to do this but it requires data structures you probably haven't learned yet (regex string splitting, stack) but the best way to do it (without your teacher accusing you of cheating) goes something like this:</strike> Only answering your question (without your teacher accusing you of cheating) and not writing the entire calculator for you. :P

    Code:
    switch (in.read()) { //InputStream#read() reads a single int from the input stream
                case '+':
                    //add
                    break;
                case '-':
                    //subtract
                    break;
                case '*':
                    //multiply
                    break;
                case '/':
                    //divide
                    break;
                default:
                    //invalid input
                    //break; //break; isn't actually required here because the code will continue execution at the same place because it's the last case in the switch. If it wasn't the last case then without the break; the switch would fall through to the next case in the switch.
    }

    [headshot4444] Java calculator operator parsing
  2. #2
    buyingrafwow's Avatar Banned
    Reputation
    7
    Join Date
    Oct 2012
    Posts
    105
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally Posted by nerdtopia View Post
    Code:
    switch (in.read()) { //InputStream#read() reads a single int from the input stream
                case '+':
                    //add
                    break;
                case '-':
                    //subtract
                    break;
                case '*':
                    //multiply
                    break;
                case '/':
                    //divide
                    break;
                default:
                    //invalid input
                    //break; //break; isn't actually required here because the code will continue execution at the same place because it's the last case in the switch. If it wasn't the last case then without the break; the switch would fall through to the next case in the switch.
    }
    Not sure what he asked for. Reading code from a file and doing math on it?

    Because if that's so, I believe you would need to read each line of the file directly, convert them to ints, then create a seperator and finally, read the lines as this:

    Code:
    37 * 12
    The * would be the seperator, and you would read on the sides of it, then use the *, etc., in a switch statement/case and have it do the math.

  3. #3
    abuckau907's Avatar Active Member
    Reputation
    49
    Join Date
    May 2009
    Posts
    225
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    @buying That doesn't make sense?? Read each line from file..that makes sense. Convert each one (line?) to an int? How do you convert a line of string to int ?!

    *The post question was way too vague ...it all depends on the format ur storing in the txt file..how complicated the equations are (multiple math symbols per line), etc. The problem space wasn't defined well enough. As far as answering his specific question, @nerd answered it just fine with a switch statement.
    Last edited by abuckau907; 11-04-2012 at 01:13 AM.

Similar Threads

  1. Reputation Calculator for EU players.
    By Enfeebleness in forum World of Warcraft General
    Replies: 11
    Last Post: 08-17-2007, 07:19 PM
  2. [New] Arena Season 2 Rating Calculator By Bind
    By bind in forum World of Warcraft Bots and Programs
    Replies: 8
    Last Post: 06-17-2007, 03:14 PM
  3. Bind's Arena Rating Calculator
    By bind in forum World of Warcraft Bots and Programs
    Replies: 16
    Last Post: 03-19-2007, 10:06 AM
  4. Lvling for talent Calculator-thingy
    By Egads in forum World of Warcraft General
    Replies: 1
    Last Post: 09-16-2006, 03:40 AM
  5. How +Spell Damage Is Calculated For Mages
    By impulse102 in forum World of Warcraft Guides
    Replies: 7
    Last Post: 08-06-2006, 06:08 AM
All times are GMT -5. The time now is 07:39 PM. Powered by vBulletin® Version 4.2.3
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved. User Alert System provided by Advanced User Tagging (Pro) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
Google Authenticator verification provided by Two-Factor Authentication (Free) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
Digital Point modules: Sphinx-based search