Results 1 to 8 of 8
  1. #1
    Mash is offline Member
    Join Date
    Apr 2010
    Posts
    4
    Rep Power
    0

    Default Reading from a JTextPane

    Hi All,

    I an new to Swing and I have a simple question:

    I am writing a console which acts as a very simple calculator where the user types a math question (e.g. 7 + 6 ) and the program returns the answer. The console would like some thing as the following:


    enter Input: 7 + 6
    Output is: 13

    What is the best way to read the input line (i.e 7 + 6). The expression "7+6" could appear any where in the input line.

    Thanks in advance

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    First of all think about your design.

    When we talking about the math equations, users can deals with large number of such as addition, multiplication, division, and more seriously the combination of those. So how you going to handle that? Do you have any limitation?

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,436
    Blog Entries
    7
    Rep Power
    20

    Default

    Starting from Java 1.6 the distribution comes bundled with a Javascript interpreter; you can use that for the runtime interpretation of expressions:

    Java Code:
    import javax.script.ScriptEngine;
    import javax.script.ScriptEngineManager;
    import javax.script.ScriptException;
     
    public class ScriptDemo {
     
        public static void main(String[] args) {
            
            ScriptEngineManager manager = new ScriptEngineManager();
            ScriptEngine engine = manager.getEngineByName("JavaScript");
            String expr= "2*(3+4)";
            
            try {
            	System.out.println(expr+": "+engine.eval(expr));
            } catch(ScriptException se) {
                se.printStackTrace();
            }
        }
    }
    kind regards,

    Jos

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,436
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Eranga View Post
    Giving a hint I think much better. :rolleyes:
    That was a hint; as soon as the OP wants to use variables or define functions s/he has to read the API documentation and study the stuff.

    kind regards,

    Jos

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,193
    Rep Power
    19

    Default

    Cross post
    Swing - JTextPane

    db

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. [SOLVED] jTextPane to xml
    By er_zhong in forum New To Java
    Replies: 9
    Last Post: 04-15-2009, 04:46 AM
  2. StyledDocument for JTextPane
    By nadia in forum AWT / Swing
    Replies: 2
    Last Post: 12-28-2008, 02:41 AM
  3. GetIcon from jTextPane
    By Gudradain in forum AWT / Swing
    Replies: 4
    Last Post: 11-26-2008, 03:26 AM
  4. JTextPane (reading char and its attributes)
    By Defero in forum New To Java
    Replies: 0
    Last Post: 07-19-2008, 08:09 PM
  5. Transparent JTextPane
    By Ada in forum AWT / Swing
    Replies: 1
    Last Post: 05-31-2007, 09:50 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •