Infix to Postfix calculation: having problems.. help?

This is a program that when you input infix expression, it converts it to postfix expressions the evaluates the postfix..
I'm having difficulty in my main method. How do i loop it? when the user wants to another input an expression?..
import java.io.*;
import java.util.*;
import javax.swing.*;
public class Calculator {
     private static Stack operators = new Stack();
     private static Stack operands = new Stack();
     private static int precedence( char operator ) {
          if (operator == '+' || operator == '-' )
             return 1;
          else if (operator == '*' || operator == '/' || operator == '%')
              return 2;
          return 0;
     private static String convertToPostfix( String infix ) {
     StringTokenizer input = new StringTokenizer( infix );
     String symbol;
     String postfix = "";     
     while (input.hasMoreTokens()) {          
         symbol = input.nextToken();     
          if (Character.isDigit(symbol.charAt(0))){
              postfix = postfix + " " + (Integer.parseInt(symbol));     
          }else if (symbol.equals("(")){ 
               Character isOperator = new Character('(');
               operators.push( isOperator );
          }else if (symbol.equals(")")) {
               while (((Character)operators.peek()).charValue() != '(') {
                   postfix = postfix + " " + operators.pop();
               }operators.pop();
          } else {
               while (!operators.empty() && !(operators.peek()).equals("(") && precedence(symbol.charAt(0)) <= precedence(((Character)operators.peek()).charValue()))
                    postfix = postfix + " " + operators.pop();
                    Character isOperator = new Character(symbol.charAt(0));
                    operators.push( isOperator );
          while (!operators.empty())
               postfix = postfix + " " + operators.pop();
                    return postfix;
     private static int evaluate( String postfix ) {
     StringTokenizer input2 = new StringTokenizer( postfix );
     int value;
     String symbol;
     while (input2.hasMoreTokens()) {
         symbol = input2.nextToken();
          if (Character.isDigit(symbol.charAt(0))) {
              Integer isOperand = new Integer(Integer.parseInt(symbol));
               operands.push( isOperand );
          }else{
               int op2 = ((Integer)operands.pop()).intValue();
               int op1 = ((Integer)operands.pop()).intValue();
               int result = 0;
                    switch(symbol.charAt(0)){
                         case '*': result = op1 * op2;
                                        break;
                         case '+': result = op1 + op2;
                                        break;
                         case '-': result = op1 - op2;
                                        break;
                         case '/': result = op1 / op2;
                                        break;
                         case '%': result = op1 % op2;
                                        break;
                    Integer isOperand = new Integer(result);
                    operands.push( isOperand );
          value = ((Integer)operands.pop()).intValue();
          return value;
     public static void main(String args[]) throws IOException {
                 JOptionPane.showMessageDialog(null,  "Hello user! I hope you have a wonderful time using the
                    calculator.  = ]"  , "Welcome", JOptionPane.PLAIN_MESSAGE);     
              JOptionPane.showMessageDialog(null,  "Note: Enter Infix expressions with spaces in between.",
                    "Welcome", JOptionPane.PLAIN_MESSAGE);
              String infix = JOptionPane.showInputDialog("Enter Mathematical Expression Here: ");
              String output = "The expression in Postfix is: " +  convertToPostfix(infix);
              JOptionPane.showMessageDialog(null, output);
              String answer = "The answer to the equation:  " + evaluate(convertToPostfix(infix));
              JOptionPane.showMessageDialog(null, answer);
                   String options[] = {"Yes","No",}
               int option = JOptionPane.showOptionDialog(null,"Do you want evaluate an expression?", "Calculator",  
                   JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null,options,options[0]); 
              JOptionPane.showMessageDialog(null,  "Thank you for using the calculator.\n Have a nice day!  = ]");
}

Don't waste people's time by posting your question in more than one forum, please.
Continued here: [http://forums.sun.com/thread.jspa?threadID=5333073]

Similar Messages

  • Add Album Art - Having Problems - HELP!

    I'm having problems adding album art to my itunes library. I've already done the "advanced"--->"Get Album Art" thing but there are quite a few albums that iTunes DIDN'T find. So, I selected one that iTunes didn't find and I drug a .jpg file from a folder, into the bottm-left corner of iTunes and it associated that image with all the files I'd selected from that particular album. Great...so far.
    The problem came when I tried to do the same thing a second time, with another album. Now, the little window at the bottom left reads, "Album Art not Modifiable!" I've tried dragging images of the album cover into the space anyway, but to no avail. WHAT GIVES?
    Can someone tell me how to fix these missing album covers?
    Also, some of the albums iTunes didn't 'find' the artwork for are albums that are even sold BY iTunes! How could it not find the album art for albums I can find right there in iTunes?? I have a new iPhone and, because of the way you can surf albums and songs, it's really important that I get all the album art stuff squared-away!
    SOMEONE PLEASE HELP ME OUT!
    Thanks, in advance!
    Nick
    Austin, TX
    HP Pavillion DV8000z   Windows XP Pro  

    Instead of trying to drag the art into iTunes, you
    can copy and paste it. To do this, right click on a
    song with incorrect or missing artwork. Choose "Get
    Info" from the menu that opens. Then click on the
    artwork tab--if there's wrong artwork showing, click
    on the album cover and then the delete button--now
    you'll have a blank box. (If you've already
    right-clicked and selected "copy" for the correct
    jpeg you've found)--right click in this blank box and
    choose "paste". Then click on the ok/yes/save
    button--whatever it is in the lower right corner and
    your correct artwork should show up in iTunes.
    If you want to do an entire album--hold down your
    shift key and highlight all of the songs on the
    album, right click>get info>say yes when you get the
    protest box>choose the artwork tab>right click and
    choose paste.
    Your song tags have to exactly match the way songs
    are tagged in the iTunes store to have the artwork
    download automatically. Sometimes a misplaced space
    can be the culprit. Often iTunes downloads the wrong
    artwork--so allowing iTunes to handle your album art
    is not foolproof!
    When I try to do what you suggested, I get a iTunes warning that says:
    "The Song "Song Name Here" cound not be used because the original file could not be found. Would you like to locate it?"
    If I click "Yes" I get a browser window, and if I click "Cancel" I get the info page you talked about; but I can't click on the image because the "Artwork Tab" doesn't exist! I checked some other files and it turns out, that the "Artwork" tab ONLY shows up on files where there's ALREADY some artwork showing! My problem is not that the WRONG artwork is associated with the song or album, it's that there isn't ANY artwork associated with it.
    So my question is still: HOW do I associate artwork with music that doesn't show a picture, even after iTunes has searched for the appropriate one and come up with NO image at all?
    I'm wondering why iTunes can't locate the original file on some of these songs, though. The only thing I can think of is that, because I'm in the process of re-organizing my music on my computer into album folders, I may have moved a lot of files from the location where iTunes originally found them. Some songs, iTunes has no problem locating, but they seem to be the songs that are part of complete albums I've created outside iTunes and then ADDED to my "My Music" folder, where iTunes seems to find it, shortly after it's added to the folder. So, this makes me think I would be better off deleting EVERYTHING from my Library (except those songs/albums I bought from iTunes) and then RE-add ONLY the re-organized music folders- keeping all the stuff I have yet to straighten-out, completely out of iTunes until it IS organized. How do I clear my library and, then, how do I rebuild it from the music folders I choose? Do you think this would fix my problem?
    Nick
    Austin, TX
    HP Pavillion DV8000z   Windows XP Pro  
    HP Pavillion DV8000z   Windows XP Pro  

  • Calculator: having problem with main method

    This is a program that converts Infix expression to Postfix and evaluates it.I used eclipse to run this.
    In my main method, how do i get it to loop? when the user still wants to input another expression? i just can't seem to figure this out.
    import java.io.*;
    import java.util.*;
    import javax.swing.*;
    public class Calculator {
         private static Stack operators = new Stack();
         private static Stack operands = new Stack();
         private static int precedence( char operator ) {
              if (operator == '+' || operator == '-' )
                 return 1;
              else if (operator == '*' || operator == '/' || operator == '%')
                  return 2;
              return 0;
         private static String convertToPostfix( String infix ) {
         StringTokenizer input = new StringTokenizer( infix );
         String symbol;
         String postfix = "";     
         while (input.hasMoreTokens()) {          
             symbol = input.nextToken();     
              if (Character.isDigit(symbol.charAt(0))){
                  postfix = postfix + " " + (Integer.parseInt(symbol));     
              }else if (symbol.equals("(")){ 
                   Character isOperator = new Character('(');
                   operators.push( isOperator );
              }else if (symbol.equals(")")) {
                   while (((Character)operators.peek()).charValue() != '(') {
                       postfix = postfix + " " + operators.pop();
                   }operators.pop();
              } else {
                   while (!operators.empty() && !(operators.peek()).equals("(") && precedence(symbol.charAt(0)) <= precedence(((Character)operators.peek()).charValue()))
                        postfix = postfix + " " + operators.pop();
                        Character isOperator = new Character(symbol.charAt(0));
                        operators.push( isOperator );
              while (!operators.empty())
                   postfix = postfix + " " + operators.pop();
                        return postfix;
         private static int evaluate( String postfix ) {
         StringTokenizer input2 = new StringTokenizer( postfix );
         int value;
         String symbol;
         while (input2.hasMoreTokens()) {
             symbol = input2.nextToken();
              if (Character.isDigit(symbol.charAt(0))) {
                  Integer isOperand = new Integer(Integer.parseInt(symbol));
                   operands.push( isOperand );
              }else{
                   int op2 = ((Integer)operands.pop()).intValue();
                   int op1 = ((Integer)operands.pop()).intValue();
                   int result = 0;
                        switch(symbol.charAt(0)){
                             case '*': result = op1 * op2;
                                            break;
                             case '+': result = op1 + op2;
                                            break;
                             case '-': result = op1 - op2;
                                            break;
                             case '/': result = op1 / op2;
                                            break;
                             case '%': result = op1 % op2;
                                            break;
                        Integer isOperand = new Integer(result);
                        operands.push( isOperand );
              value = ((Integer)operands.pop()).intValue();
              return value;
         public static void main(String args[]) throws IOException {
                     JOptionPane.showMessageDialog(null,  "Hello user! I hope you have a wonderful time using the
                        calculator.  = ]"  , "Welcome", JOptionPane.PLAIN_MESSAGE);     
                  JOptionPane.showMessageDialog(null,  "Note: Enter Infix expressions with spaces in between.",
                        "Welcome", JOptionPane.PLAIN_MESSAGE);
                  String infix = JOptionPane.showInputDialog("Enter Mathematical Expression Here: ");
                  String output = "The expression in Postfix is: " +  convertToPostfix(infix);
                  JOptionPane.showMessageDialog(null, output);
                  String answer = "The answer to the equation:  " + evaluate(convertToPostfix(infix));
                  JOptionPane.showMessageDialog(null, answer);
                       String options[] = {"Yes","No",}
                   int option = JOptionPane.showOptionDialog(null,"Do you want evaluate an expression?", "Calculator",  
                       JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null,options,options[0]); 
                  JOptionPane.showMessageDialog(null,  "Thank you for using the calculator.\n Have a nice day!  = ]");
    }

    Can probably be done better...
    boolean stop = false;
    while(!stop) {
    //your optionpanes..
         if(option == 0) {
              stop = true;
    }Also, your yes/no optionpane could be exchanged to:
    int option = JOptionPane.showConfirmDialog(null,"Do you want evaluate an expression?", "Calculator", JOptionPane.YES_NO_OPTION);Edited by: prigas on Sep 20, 2008 12:04 PM

  • Switching iTunes from mac to pc - having problems - help, please!!!!

    I have an iPod and a Shuffle and all my music is in iTunes on my mac.
    I want to now have my iTunes on my pc instead. I understand my iPod is not compatible, so I plan to re-format it once I get iTunes all squared away.
    I downloaded iTunes onto my pc and...
    Issue #1: When I open i Tunes I receive an error (no code) that says: "The registry settings used by the iTunes drivers for importing and burning CDs and DVDs are missing. This can happen as a result of installing other CD burning software. Please reinstall iTunes." I can hit "ok" on the message and iTunes opens up.
    I uninstalled and reinstalled. Same message. True, I have other CD burning software on the computer but don't want to delete it. Don't know if this is really an issue except for having to bypass that error message every time I open iTunes....
    Issue #2: I Exported my iTunes library from the mac onto an external hard drive formated in fat32 thinking I could either import the music into iTunes on my pc, or, even better, use the external drive to store the music for use with iTunes on my pc.
    Ideally, I'd store the music on the external hard drive and use iTunes on my PC with my iPod and Shuffle.
    BUT, after searching the support site and discussions and trying the suggestions I found, I stil have no music showing up in iTunes on the pc.
    Can anyone help me? I've spent quite a bit of time on this already and I really can't keep wasting the time. Thank you!!!!!!!!!
    mac mini   Mac OS X (10.4.6)   switching to a Dell with windows XP
    mac mini   Mac OS X (10.4.6)  
    mac mini   Mac OS X (10.4.6)  
    mac mini   Mac OS X (10.4.6)  
    mac mini   Mac OS X (10.4.6)  

    First you will want to look for updates to your other CD burning programs that will possiably fix this issue or try uninstalling reinstalling both iTUnes and quicktime.
    Im not that mac savy, but on the windows side of things, iTUnes is very picky on what is where.you can recreat the Library on your PC if the file you saved from the mac is a "iTUnes Music Library.xml" file but that would be a hard thing and a mess cuz iTUnes for windows doesn't "Watch" folders. So if that Nickelback song you have was on C:/Mac/Music or however a mac does folders, then it would have to be in the EXACT location in windows. If not your going to have a very sloppy library full of ! marks.
    But if you want you can recreate the library following these instructions http://docs.info.apple.com/article.html?artnum=93313

  • Help! - code for infix to postfix

    can someome please send me the code for infix to postfix?
    thanks

    Or--and I know this will sound crazy, but just keep an open mind--you could try doing your homework yourself, and then post your attempt here when you get stuck, along with details of what specific problems you are having.
    If you are unwilling or unable to do that, then you should look elsewhere. This site is not a place to get code written for you. Try finding someone who will do it for money.

  • Hi I am having problems removing mail from trash folder. Can anyone help please

    Hi I am having problems removing mail from trash folder. I can send mails to the folder but cannot delete them permanently from the trash folder. I am using Iphone 3G and anm up to date with OS. Can anyone help please

    Check that there isn't a different Trash folder to the one with the proper Trash symbol on it.
    If you see one in your list of folders rather than a separate one, highlight it, then click on Mailbox...Use this mailbox for...Trash

  • HT4623 please help - installed IOS7 and now I am having problems playing Words Free on my Iphone.  It gives me a pop up telling me to connect to ITunes but will not connect.  Cannot get out and have to continually shut down my phone and start over.

    please help - installed IOS7 and now I am having problems playing Words Free on my Iphone.  It gives me a pop up telling me to connect to ITunes but will not connect.  Cannot get out and have to continually shut down my phone and start over.

    Hi, jeantwin.
    Thank you for visiting Apple Support Communities.
    The steps in the article below may help you resolve the issue with push notifications.
    iPad and iPod touch: Unable to use YouTube or Push notifications
    http://support.apple.com/kb/ts3305
    If the issue persists, try signing out of your Apple ID and then sign back in.
    iOS: Changing the signed-in iTunes Store Apple ID account
    http://support.apple.com/kb/HT1311
    Cheers,
    Jason H.

  • Hi Can anyone help. I'm having problem with FaceTime on my IPAD .It connects but cuts out Simolarly when someone calls me it connects , then cuts out ThanksBrian

    Hi Having problem with FACETIME on my IPAD Connects to call but then cuts out , similarly calls in connect &amp; cut out Can someone help. Brian

    My ipad is doing the same thing to it won't let me except a incoming call on Facebook and I don't know what to do

  • I am having problems rebooting my mid 2009 macbook pro (grey screen other issues), need help!

    I am having problems rebooting, I have experienced the same problem as others with the grey screen but my case is a little different, It started with my mid 2009 macbook pro acting a little slower than normal, the screen would freeze, mouse wouldnt move and i'd be forced to "force reboot" this went on for a couple of days until i was tinkering with the wifi connection and it froze again. I had to reboot and this time i noticed that it took a long time to get to the login screen. i put in my password and it went into infinite load (i also tried the guest account and other accounts with same result). i tried rebooting again and the same thing would happen. after several reboots, the screen just stayed grey, sometimes with the apple logo and gear sometimes without it. Since its a 2009, (and for the most part, still works like a champ) i decided test the ram, i took one stick of ram out at a time to see if i could notice anything wrong with them. when i stuck just one in, the mac was still having the same problem rebooting. i pulled that ram and and stuck in the other and i noticed that this time the apple logo never appeard and a big crossed out circle appeared. figuring that the ram must be the problem, i ordered new ram and installed it. i noticed that when i rebooted for the first time with the new ram it was rather quick and took me straight to the login screen and i was able to log in with no problem. at this point i thought the problem was solved, but i noticed the macbook still acting funny. at first, none of my files appeared on my desktop or in the finder window. after about 20 minutes, everything magically popped up. i also noticed long pauses/freezes with the colored pin wheel spinnig. i closed the laptop and opened it again and the screen just stayed black. i tried rebooting again, and again i got the grey screen with no signs of being able to log in... i waited about 20 minutes and tried to reboot again and again i was able to log in, however the macbook is still acting slow and will freeze with no signs of unfreezing without a reboot.
    my macbook is 5 years old and has plenty of miles on it, so i understand if the thing needs to be replaced alltogether but does anyone know what could be the issue? could the bad ram have corrupted the hard drive in anyway? would re-installing the OS fix it?? and if so, how would i do that if i downloaded mavericks from the app store? are there any other solutions to this problem?
    all my files and what not are backed up on an external HD so theres nothing on it that i'd miss desperately if i had to whipe the hard drive but id really like to get her working again without any whiping of the HD. if anyone can help me out, it'd be greatly appreciated

    It is very difficult to offer troubleshooting suggestions when the "os version" you are using is unknown as each os has their own troubleshooting solutions. 
    How large is your hard drive and how much hard drive space do you have left? 

  • Having problems accessing any apps on my iCloud. Set up and signed in successfully but then when I click on an app it just says there's a problem but doesn't say how to fix it. Please help, am very new to this!

    Hi
    I am very new to updating my iPod touch and this is the first time I've used the iCloud. However, I am having problems accessing any apps on my iCloud such as photos, reminders etc. I've set it up on my iPod touch successfully and I can sign in on my PC. However, when I try to access any of the apps it says there was a problem. It doesn't tell me how to fix it or what the problem is.
    Is anyone able to help?

    It is working now. I no longer get the message about the item not being available in the US and I can access the store.

  • I'm having problems with my app, plants vs. zombies 2. It'll open and stay open for a few seconds then shut down. Help please.

    I'm having problems with my app, plants vs. zombies 2. It'll open and stay open for a few seconds then shut down. Help please.

    Check to see if there are Updates available... If already up to date...
    Try This...
    Close All Open Apps... Sign Out of your Account... Perform a Reset... Try again...
    Reset  ( No Data will be Lost )
    Press and Hold the Sleep/Wake Button and the Home Button at the Same Time...
    Wait for the Apple logo to Appear...
    Usually takes about 15 - 20 Seconds... ( But can take Longer...)
    Release the Buttons...

  • I am having problems being able to sign out of everything that requires a sign in and password can you help and some games only show have the page

    I have been doing online banking with this bank for 7 months with no problems. About 2 weeks ago I started having problems with certain things within the account. I called the support team with the bank and told them the problem and they suggested to uninstall firefox and reinstall so I did. Now I can move around within the account but can not sign off. Also all my accounts that require a sign in I have the same problem and some games I play only load up part of the game. I play goldminer and now I can not see the little miner to play the game. Can you help????????

    That issue can be caused by corrupted cookies.
    *https://support.mozilla.org/kb/Cannot+log+in+to+websites
    Clear the cache and the cookies from sites that cause problems.
    "Clear the Cache":
    *Tools > Options > Advanced > Network > Offline Storage (Cache): "Clear Now"
    "Remove Cookies" from sites causing problems:
    *Tools > Options > Privacy > Cookies: "Show Cookies"
    If clearing the cookies doesn't help then it is possible that the file <i>cookies.sqlite</i> that stores the cookies is corrupted.<br />
    Rename (or delete) <b>cookies.sqlite</b> (cookies.sqlite.old) and delete other present cookie files like <b>cookies.sqlite-journal</b> in the Firefox Profile Folder in case the file cookies.sqlite got corrupted.
    *http://kb.mozillazine.org/Cookies

  • I had already update mi new factory unlocked iPhone 4S to iOS 5.1.1 and I still having problems with "invalid sim". I'm from Argentina and here we don't have official service. PLEASE HELP ME, WHAT CAN I DO??? In Argentina is too expensive an iPhone 4S.

    I had already update mi new factory unlocked iPhone 4S to iOS 5.1.1 and I still having problems with "invalid sim". I'm from Argentina and here we don't have official service. PLEASE HELP ME, WHAT CAN I DO??? In Argentina is too expensive an iPhone 4S. Please I need help to continue believing in Apple. Thanks a lot. If anybody speak spanish better.  

    Was it purchased from an official Apple Store, not a reseller?  Only Apple Stores sell official factory unlocked phones.  The others sell hacked to unlock phones, and the unlocking is unstable.
    Try these steps, as needed:
    1. Reset phone - press both home and on/off buttons for at least 10 seconds until the Apple logo appears.
    2. Settings > General > Reset > Reset Network Settings.
    3. Replace SIM card (and reset network settings again)
    4. Restore Phone in iTunes using a backup
    5. Restore in iTunes as new, without using a backup

  • Can you help I was having problems with my iPod so I restored it but I accidentally set it up as a new iPod is there any way to restore it from an old  backup when I right click iPod in iTunes there isn't one anyway to recover one or find it on computer

    Hi can you help I was having problems with my iPod so I restored it but I
    accidentally set it up as a new iPod is there any way to restore it from an old
    backup when I right click iPod in iTunes there isn't one anyway to recover one
    or find it on computer

    The following says how to restore from backup.
    iOS: How to back up
    If you go to iTunes>Preferences>Devices you can see if you have an iTunes backup. You need one dated before or the exact time you started the restore.

  • I am having problems with my outlook 2011. While i am able to check and SEND email on all my other devices ( Ipad, iphone, Macbook), I am unable to do so with my iMac. This problem started suddenly and the error message i get is error 5.7.8. Please Help

    I  am having problems with my outlook 2011. While i am able to check and SEND email on all my other devices ( Ipad, iphone, Macbook), I am unablesend any email with my iMac ( i can recieve email) . This problem started suddenly and the error message i get is error 5.7.8. I have read the threads on line and went into settings, even created a new profile, nothing helps...Please advice..is this something to do with my keychain Access?

    As Outlook is not an Apple product, you will find more helpers familiar with Outlook here:
    Office for Mac forums

Maybe you are looking for

  • Problem with socket object writing

    hi, I made this little programm , with a class point , a server and a client. I try to send via the socket several point object. If send differents objects ( with several new point(... )) it works fine , but if i send the same object changing only th

  • How should i  start

    Hi Experts, i joined a company which has SAP implementation, it went live in other modules  except HR. so all SD,MM is running thru sap but no HR. so i joined as HR consultant and i am a fresher. so please guide me how should i start .i read the busi

  • How do i open a new doc in specific folder?

    In Windows, right click of mouse gives options to open up different kinds of new files in that particular folder. In Mac, I open a file and then have to save it to a particular folder. Any way to open it in the folder directly?

  • A fatal error occured in the PD Layer during RFx response

    Dear Experts, While creating the RFx response the error is, "A fatal error occured in the PD Layer. The transaction needs to be terminated." I just checked the earlier threads discussed on this forum says, this is due to non maintaining of number ran

  • Scroll direction not working in either direction

    I'm running Mountain Lion. The scroll direction works under account A, but if I switch users B, it stops working all together under B account. I've been to System Prefs and see nothing unusual. It's system wide, can't scroll in the finder or in appli