How do I turn a string into a mathematical expression?

Hi,
I'm creating a calculator program and I'm trying to take a mathematical expression from a JTextField, like 1 + 2, and turn it into an actual expression that will give me the answer. Is there any way to do this?
Thanks in advance

Yes, but as far as I know, you have to do it yourself. It just boils down to string parsing. I'd rather not give you too much advice for fear that it's a school project. Try it out and post any specific problems that you're having.
Some minor advice... you'll find the String and StringTokenizer classes helpful, along with the java.math package.
I did it by taking the string and converting it to chars and
pushing all the chars on the stackYou deal with stacks but not the stack in Java. Anyways, why use a stack? How will you be able to deal with order of operations if you use a stack and pop them one at a time?

Similar Messages

  • How do you turn a string into an integer?

    how do you turn a string into an integer?
    for ex, if i wanted to turn the string "5" into the integer 5, how would i do that?

    String stringNumber="5";
    try{
    int number=Integer.parseInt(stringNumber);
    }catch(NumberFormatException e){
    System.out.println("The string "+stringNumber+" must be a number!");
    }

  • I made a picture collage in pages from the pictures in iPhoto.  You used to be able to print and then say save as a jpeg to iPhoto and that option is gone.  How do you turn your document into a jpeg now.

    I made a picture collage in pages from the pictures in iPhoto.  You used to be able to print and then say save as a jpeg to iPhoto and that option is gone.  How do you turn your document into a jpeg now?

    If you chose print, you can save as PDF. If you open the saved PDF in preview, you can save as JPEG, GIF, PNG. FYI you can crop in preview.
    Another option is you could use command + shift + 4 to do a screen shot of a specific part of of the screen.

  • How do I turn a PDF into a JPEG?

    How do I turn a PDF into a JPEG?

    Hi joeyk86530628,
    If you have Adobe PDF Pack, you can export that PDF file to JPEG format. You can use Acrobat, as well, if you have that.
    If you don't have Acrobat, you're welcome to give it a try. You can download a free 30-day trial from http://www.adobe.com/products/acrobat.html. In Acrobat, you choose File > Save as Other > Image > JPEG (or JPEG2000).
    Please let us know if you have additional questions.
    Best,
    Sara

  • How do I turn a jpeg into a PDF

    How do I turn a jpeg into a PDF

    Hitchell which Adobe software or service is your inquiry in reference too?

  • How do I turn a photo into a pencil sketch using Photoshop Elements 12?

    How do I turn a photo into a pencil sketch using photoshop elements 12?

    Filter> Sketch> Pen & Ink.
    Cheers,
    Neale
    Insanity is hereditary, you get it from your children
    If this post or another user's post resolves the original issue, please mark the posts as correct and/or helpful accordingly. This helps other users with similar trouble get answers to their questions quicker. Thanks.

  • How do i turn a ringtone into a call tone

    how do i turn a ringtone into a call tone

    Settings---> Sounds---> Ringtones---> select the tone you want.
    KOT

  • How do I turn a photo into a pop art poster

    how do I turn a photo into a pop art poster

    Filter> Sketch> Pen & Ink.
    Cheers,
    Neale
    Insanity is hereditary, you get it from your children
    If this post or another user's post resolves the original issue, please mark the posts as correct and/or helpful accordingly. This helps other users with similar trouble get answers to their questions quicker. Thanks.

  • How can i write a string into a specified pos of a file?

    How can i write a string into a specified pos of a file without read all file into ram and write the whole file again?
    for example:
    the content of file is:
    name=123
    state=456
    i want to modify the value of name with 789
    (write to file without read all file into ram)
    How can i do it? thank you

    take this as an idea. it actually does what i decribed above. you sure need to make some modifications so it works for your special need. If you use it and add any valuable code to it or find any bugs, please let me know.
    import java.io.*;
    import java.util.*;
    * Copyright (c) 2002 Frank Fischer <[email protected]>
    * All rights reserved. See the LICENSE for usage conditions
    * ObjectProperties.java
    * version 1.0, 2002-09-12
    * author Frank Fischer <[email protected]>
    public class ObjectProperties
         // the seperator between the param-name and the value in the prooperties file
         private static final String separator = "=";
         // the vector where we put the arrays in
         private Vector PropertiesSet;
         // the array where we put the param/value pairs in
         private String propvaluepair[][];
         // the name of the object the properties file is for
         public String ObjectPropertiesFileName;
         // the path to the object'a properties file
         public String ObjectPropertiesDir;
         // reference to the properties file
         public File PropertiesFile;
         // sign for linebreak - depends on platforms
         public static final String newline = System.getProperty("line.separator");
         public ObjectProperties(String ObjectPropertiesFileName, String ObjectPropertiesDir, ObjectPropertiesManager ObjectPropertiesManager)
         //     System.out.println("Properties Objekt wird erzeugt: "+ObjectPropertiesFileName);
              this.ObjectPropertiesFileName = ObjectPropertiesFileName;
              this.ObjectPropertiesDir = ObjectPropertiesDir;
              // reference to the properties file
              PropertiesFile = new File(ObjectPropertiesDir+ObjectPropertiesFileName);
              // vector to put the param/value pair-array in
              PropertiesSet = new Vector();
         //     System.out.println("Properties File Backup wird erzeugt: "+name);
              backup();
         //     System.out.println("Properties File wird eingelesen: "+PropertiesFile);
              try
                   //opening stream to file for read operations
                   FileInputStream FileInput = new FileInputStream(PropertiesFile);
                   DataInputStream DataInput = new DataInputStream(FileInput);
                   String line = "";
                   //reading line after line of the properties file
                   while ((line = DataInput.readLine()) != null)
                        //just making sure there are no whitespaces at the beginng or end of the line
                        line = cutSpaces(line);
                        if (line.length() > 0)
                             //$ indicates a param-name
                             if (line.startsWith("$"))
                                  // array to store a param/value pair in
                                  propvaluepair = new String[1][2];
                                  //get the param-name
                                  String parameter = line.substring(1, line.indexOf(separator)-1);
                                  //just making sure there are no whitespaces at the beginng or end of the variable
                                  parameter = cutSpaces(parameter);
                                  //get the value
                                  String value = line.substring(line.indexOf(separator)+1, line.length());
                                  //just making sure there are no whitespaces at the beginng or end of the variable
                                  value = cutSpaces(value);
                                  //put the param-name and the value into an array
                                  propvaluepair[0][0] = parameter;
                                  propvaluepair[0][1] = value;
                             //     System.out.println("["+ObjectPropertiesFileName+"] key/value gefunden:"+parameter+";"+value);
                                  //and finaly put the array into the vector
                                  PropertiesSet.addElement(propvaluepair);
              // error handlig
              catch (IOException e)
                   System.out.println("ERROR occured while reading property file for: "+ObjectPropertiesFileName);
                   System.out.println("ERROR CODE: "+e);
                   // System.out.println("in ObjectProperties");
         // function to be called to get the value of a specific paramater 'param'
         // if the specific paramater is not found '-1' is returned to indicate that case
         public String getParam(String param)
              // the return value indicating that the param we are searching for is not found
              String v = "-1";
              // looking up the whole Vector
              for (int i=0; i<PropertiesSet.size(); i++)
                   //the String i want to read the values in again
                   String s[][] = new String[1][2];
                   // trying to get out the array from the vector again
                   s = (String[][]) PropertiesSet.elementAt(i);
                   // comparing the param-name we're looking for with the param-name in the array we took out the vector at position i
                   if (s[0][0].equals(param) == true)
                        //if the param-names are the same, we look up the value and write it in the return variable
                        v = s[0][1];
                        // making sure the for loop ends
                        i = PropertiesSet.size();
              // giving the value back to the calling procedure
              return v;
         // function to be called to set the value of a specific paramater 'param'
         public void setParam(String param, String value)
              // looking up the whole Vector for the specific param if existing or not
              for (int i=0; i<PropertiesSet.size(); i++)
                   //the String i want to read the values in again
                   String s[][] = (String[][]) PropertiesSet.elementAt(i);
                   // comparing the param-name we're looking for with the param-name in the array we took out the vector at position i
                   if (s[0][0].equals(param) == true)
                        //if the param-names are the same, we remove the param/value pair so we can add the new pair later in
                        PropertiesSet.removeElementAt(i);
                        // making sure the for loop ends
                        i = PropertiesSet.size();
              // if we land here, there is no such param in the Vector, either there was none form the beginng
              // or there was one but we took it out.
              // create a string array to place the param/value pair in
              String n[][] = new String[1][2];
              // add the param/value par
              n[0][0] = param;
              n[0][1] = value;
              // add the string array to the vector
              PropertiesSet.addElement(n);
         // function to save all data in the Vector to the properties file
         // must be done because properties might be changing while runtime
         // and changes are just hold in memory while runntime
         public void store()
              backup();
              String outtofile = "# file created/modified on "+createDate("-")+" "+createTime("-")+newline+newline;
              try
                   //opening stream to file for write operations
                   FileOutputStream PropertiesFileOuput = new FileOutputStream(PropertiesFile);
                   DataOutputStream PropertiesDataOutput = new DataOutputStream(PropertiesFileOuput);
                   // looping over all param/value pairs in the vector
                   for (int i=0; i<PropertiesSet.size(); i++)
                        //the String i want to read the values in
                        String s[][] = new String[1][2];
                        // trying to get out the array from the vector again
                        s = (String[][]) PropertiesSet.elementAt(i);
                        String param = "$"+s[0][0];
                        String value = s[0][1];
                        outtofile += param+" = "+value+newline;
                   outtofile += newline+"#end of file"+newline;
                   try
                        PropertiesDataOutput.writeBytes(outtofile);
                   catch (IOException e)
                        System.out.println("ERROR while writing to Properties File: "+e);
              catch (IOException e)
                   System.out.println("ERROR occured while writing to the property file for: "+ObjectPropertiesFileName);
                   System.out.println("ERROR CODE: "+e);
         // sometimes before overwritting old value it's a good idea to backup old values
         public void backup()
              try
                   // reference to the original properties file
                   File OriginalFile = new File(ObjectPropertiesDir+ObjectPropertiesFileName);
                   File BackupFile = new File(ObjectPropertiesDir+"/backup/"+ObjectPropertiesFileName+".backup");
                   //opening stream to original file for read operations
                   FileInputStream OriginalFileInput = new FileInputStream(OriginalFile);
                   DataInputStream OriginalFileDataInput = new DataInputStream(OriginalFileInput);
                   //opening stream to backup file for write operations
                   FileOutputStream BackupFileOutput = new FileOutputStream(BackupFile);
                   DataOutputStream BackupFileDataOutput = new DataOutputStream(BackupFileOutput);
              //     String content = "";
                   String line = "";
                   // do till end of file
                   while ((line = OriginalFileDataInput.readLine()) != null)
                        BackupFileDataOutput.writeBytes(line+newline);
              // error handlig
              catch (IOException e)
                   System.out.println("ERROR occured while back up for property file: "+ObjectPropertiesFileName);
                   System.out.println("ERROR CODE: "+e);
                   System.out.println("this is a serious error - the server must be stopped");
         private String cutSpaces(String s)
              while (s.startsWith(" "))
                   s = s.substring(1, s.length());
              while (s.endsWith(" "))
                   s = s.substring(0, s.length()-1);
              return s;
         public String createDate(String seperator)
              Date datum = new Date();
              String currentdatum = new String();
              int year, month, date;
              year = datum.getYear()+1900;
              month = datum.getMonth()+1;
              date = datum.getDate();
              currentdatum = ""+year+seperator;
              if (month < 10)
                   currentdatum = currentdatum+"0"+month+seperator;
              else
                   currentdatum = currentdatum+month+seperator;
              if (date < 10)
                   currentdatum = currentdatum+"0"+date;
              else
                   currentdatum = currentdatum+date;
              return currentdatum;
         public String createTime(String seperator)
              Date time = new Date();
              String currenttime = new String();
              int hours, minutes, seconds;
              hours = time.getHours();
              minutes = time.getMinutes();
              seconds = time.getSeconds();
              if (hours < 10)
                   currenttime = currenttime+"0"+hours+seperator;
              else
                   currenttime = currenttime+hours+seperator;
              if (minutes < 10)
                   currenttime = currenttime+"0"+minutes+seperator;
              else
                   currenttime = currenttime+minutes+seperator;
              if (seconds < 10)
                   currenttime = currenttime+"0"+seconds;
              else
                   currenttime = currenttime+seconds;
              return currenttime;

  • How do you turn a List into a String?

    Hi,
    Have a simple question for all of you out there, hope you can help:
    How do you turn a list of words into a string?
    For example:
    List matchList = new ArrayList();
    matchList.add(word 1);
    matchList.add(word 2);
    matchList.add(word 3);
    String s = matchList(all of the words)How is this possible?

    but how would i go about doing that? Thats what i am
    askingIf you don't know how to iterate over a list, see this tutorial: http://java.sun.com/docs/books/tutorial/collections/If you don't know how to use StringBuffer, it should be somewhere in one of the links under Resources for Beginners at http://www.thejword.com/3.html#beginner_resources

  • How do I turn an image into a transparency?

    Hi,
    There's a question I've seen asked dozens or hundreds of times, and I thought I found an answer a few years ago. I've since forgotten how to do it:
    Turn an image into a transparency.
    Now, the obvious solution is to set the layer to "multiply." Yes, this is the effect I want. However, I want the layer to be transparent instead.
    Another solution is to draw an image on a transparent layer. This works great, but only applies if I create the image from scratch. I want to convert an existing image.
    Another solution is multiple steps:
    1) Copy image
    2) Click "edit in quick mask mode."
    3) Paste image
    4) Exit quick mask mode.
    5) Invert selection.
    6) Ctrl+backspace to fill selection with black, on a blank transparent layer.
    Result? The image is now transparent! However, this converts the image to greyscale! Great for linework and text, terrible for photos or art!
    In an older version of Photoshop 7, I believe I got the following to work:
    A) Follow the previous steps 1-6, creating a layer with transparency
    B) Copy the original picture to a layer above the transparency
    C) Group the layer with the transparency layer below. (Note, grouping doesn't work the same any more.)
    D) The top layer provides the colors, but the bottom layer provides the transparency.
    E) It is too light, so you take the top layer, and maximize "saturation" 100%.
    F) Merge the two layers. It retains the transparency of the bottom layer, with the hue of the top layer.
    This no longer works, because I don't know how grouping layers works anymore.
    So, I need white pixels to be transparent. Black pixels to have zero transparency. Red pixels to have zero transparency. Etc.
    Meanwhile, grey pixels are solid black, but partly transparent. Pink pixels are solid red, but partly transparent.
    All pixels are 100% saturated, and the "lightness" is determined by how transparent they are.
    So, an analogy would be printing a photo on a transparency. I need to convert an image to a transparency.
    If the image/layer were overlaid on white, it would look exactly like the original photo.
    Does anyone know how to accomplish this? Mathematically, it's easy. But I don't know about any filter, process, or method to make the conversion using CS5.
    Thanks,
    Ben

    Hello!
    I hope that I understand what you need.
    (One could just put the image on a layer, and lower its opacity, but you seem to be looking for an effect in wich the tranparency is not the same for all pixels.)
    Try this:
    1) Turn your image image as a layer (double-click it if it is a background layer) [In order to add a mask]
    2) Select all, Copy (CTRL+a, CTRL+C)
    3) add a layer mask (click the rectangle with a circle in the bottom of the layers panel) [to be able to change transparency]
    4) target the mask [so that you can past an image in it] ALT+click the mask, paste the image.
    5) Invert the colors of the mask (CTRL+I) [in order for the white to be transparent and the black opaque].
    You now have a layer whose transparency is based on the lightness of the pixels.
    Hope that's what you are after!
    Pierre

  • How do I convert a string into a decimal

    I've been tryin to convert a string into decimal but I can't get it to work at all. I keep getting an exception.
    I've tried all of these:
    double myDouble = Double.valueOf(string).doubleValue();
    int myint = Interger.parseInt(string);
    float myfloat = Float.parseFloat(string);
    I can't get anything to work.
    That string i'm reading in is an array containing values with decimals. ex: -45.09348
    All the above work fine if there is no decimal, but fails when it comes to one.
    How do I go about doing this?
    Thanks!

    This works for me:String s = "-45.6789";
    double d = Double.parseDouble(s);
    System.out.println(d);Mark

  • How to get an xml string into a Document w/o escaping mark-up characters?

    Hi,
    I am using one of the latest xerces using Java. I am pretty sure I am using xerces-2.
    I have an existing Document and I am trying to add more content to it. The new content itself is xml string. I am trying to insert this xml string into the document using document.createTextNode. I am able to insert, but somewhere it is escaping the mark-up characters (<,>,etc). When I convert the document into String, I can see, for example, <userData> instead of <userData>.
    There is an alternative option to accomplish this by creating a new document with this xml string, get the root element, import this element into my document. Execution time for this procedure is very high - means, this is very bad in terms of time-wise performance.
    Can any help on how to accomplish this (bringing an xml string into a document without escaping mark-up characters) in time-efficient way.

    So you want to treat the contents of the string as XML rather than as text? Then you have to parse it.
    Or if your reason for asking is just that you don't like the look of escaped text, then use a CDATA section to contain the text.

  • How to break up a String into multiple array Strings?

    How do I break up a string into a bunch of String arrays after the line ends.
    For example,
    JTextArea area = new JTextArea();
    this is a string that i have entered in the area declared above
    now here is another sting that is in the same string/text area
    this is all being placed in one text field
    Sting input = area.getText();
    now how do I break that up into an array of strings after each line ends?

    Ok I tested it out and it works.
    So for future refrence to those that come by the same problem I had:
    To split up a string when using a textfield or textarea so that you can store seperate sections of the string based on sperate lines, using the following code:
    String text = area.getText() ;
    String[] lines = text.split("\n") ;
    This will store the following
    this is all one
    entered string
    into two arrays
    Cheers{
    Edited by: watwatacrazy on Oct 21, 2008 11:06 AM
    Edited by: watwatacrazy on Oct 21, 2008 11:16 AM
    Edited by: watwatacrazy on Oct 21, 2008 11:16 AM

  • How do yu turn a powerpoint into a podcast?

    How does one turn a Mac Office Powerpoint into a podcast?

    Not that it has anything to do with iPad, but simple export the presentation and audio to a QuickTime movie.

Maybe you are looking for

  • Ssrs 2008 r2 black line incorrectly being displayed

    In an existing SSRS 2008 r2 report, I just added a page header so that I can display global values in the page header like run date, run time, and page number. Within the report header, I placed all the report header data within a rectangle. Now when

  • How to make AVCHD 1080i run smoothly in PE 12, I'm using a Canon HG10

    Hi I'm a nwbie on here and not sure how things work. I have a Canon HG10  AVCHD ,video camera and when I load it into PE12 it is really jerky, I'm downloading as 1080i if that helps, the program is rendering it as it uploads it, but still plays jerky

  • REG:How to move the data from one screen to another

    Hi Gurus, My requirement is ..... in the view1 we have the communication data     with fileds pernr voice extension, and     the display button ... when the display button is pressed we have to get the fields   pernr ,voice extension,mail, fields on

  • How To Get Around iCloud Terms

    How To Get Around iCloud Terms & Conditions Error After iOS 7 Upgrade It is being widely reported that some users are experiencing errors with iCloud after upgrading to iOS 7.  The issue has to do with the iCloud Terms & Conditions which were changed

  • Merge two Livecycle PDF files into one PDF?

    I have a new requirement where I need to take two different PDF forms I'm producing from LiveCycle and merge them together into a single PDF.  I have heard that Adobe Acrobat cannot edit LiveCycle files, does anyone know if it is possible to merge tw