Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: java program  (Read 5053 times)

0 Members and 1 Guest are viewing this topic.

wagpofafi

    Topic Starter


    Beginner

    java program
    « on: August 11, 2011, 06:42:20 AM »
    does anyone here know how tom make a computing program like this
    >user will be asked to input a math problem like 5+5
    >and when user press enter it will show output
    >the answer is = 10

    the problem is i dont know how to make it comput the math problem with just a 1 input,
    i can program it asking the user with 2 input.

    i hope someone knows it how to do it in java thanks in advance

    umbra



      Beginner
    • Thanked: 2
      • Experience: Familiar
      • OS: Windows 7
      Re: java program
      « Reply #1 on: August 14, 2011, 07:45:30 AM »
      You can use only one input and have the other operand as a constant.
      So let's say you input 8 and always do the calculations with a constant like 5.
      And you will have for example:
      8+5

      input 4

      4+5

      and so on.
      Trying to don't waste my time.  :)

      Geek-9pm


        Mastermind
      • Geek After Dark
      • Thanked: 1026
        • Gekk9pm bnlog
      • Certifications: List
      • Computer: Specs
      • Experience: Expert
      • OS: Windows 10
      Re: java program
      « Reply #2 on: August 14, 2011, 10:00:25 AM »
      Java tutorial was recently updated.
      http://download.oracle.com/javase/tutorial/
      The file API is new
      http://download.oracle.com/javase/tutorial/essential/io/fileio.html
      Passing stuff from the command line:
      http://download.oracle.com/javase/tutorial/essential/io/cl.html
      Hope this is of some help.

      The conventional way to pass two numbers from console input is to separate them with a comma.

      Sidewinder



        Guru

        Thanked: 139
      • Experience: Familiar
      • OS: Windows 10
      Re: java program
      « Reply #3 on: August 14, 2011, 12:39:28 PM »
      I may have misinterpreted, but I suspect the OP wants to enter a math expression and have the Java do the computes.

      My Java skills are a bit lower than zero, but with a little help from Google, NetBeans, and a little luck, I came up with this:

      Code: [Select]
      import java.io.*;
      import javax.script.ScriptEngineManager;
      import javax.script.ScriptEngine;

      public class JavaApplication66 {

        public static void main (String[] args) throws Exception {
          String strExp = null;
          String strQuit = "quit";
          while (true) {
            System.out.print("Enter expression: ");
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            try {
              strExp = br.readLine();
              if (strExp.equals(strQuit)) {
                System.exit(0);
              }
              ScriptEngineManager mgr = new ScriptEngineManager();
              ScriptEngine engine = mgr.getEngineByName("JavaScript");
              System.out.println(engine.eval(strExp));     
            }
            catch (IOException e) {
              System.out.println("Error!");
              System.exit(1);
            }
          }
        }
      }

      This will handle the arithmetic stuff, but other things like exponents and such probably require something more sophisticated. Program runs in an infinite loop...Enter quit to exit program.

      Good luck.  8)

      The true sign of intelligence is not knowledge but imagination.

      -- Albert Einstein

      reddevilggg



        Expert

        Thanked: 69
      • Experience: Beginner
      • OS: Windows 7
      Re: java program
      « Reply #4 on: August 14, 2011, 03:22:07 PM »
      I also may have misinterpreted, but i think you've just this guys homework for him.  ::)
      11 cheers for binary !

      wagpofafi

        Topic Starter


        Beginner

        Re: java program
        « Reply #5 on: August 15, 2011, 04:07:41 AM »
        I also may have misinterpreted, but i think you've just this guys homework for him.  ::)

        yeah,it's a home work,but before posting here i already pass my homework,the question that i post here was a plus points if done,and after passing it still can't do it so i decided to ask here,

        I may have misinterpreted, but I suspect the OP wants to enter a math expression and have the Java do the computes.

        My Java skills are a bit lower than zero, but with a little help from Google, NetBeans, and a little luck, I came up with this:

        Code: [Select]
        import java.io.*;
        import javax.script.ScriptEngineManager;
        import javax.script.ScriptEngine;

        public class JavaApplication66 {

          public static void main (String[] args) throws Exception {
            String strExp = null;
            String strQuit = "quit";
            while (true) {
              System.out.print("Enter expression: ");
              BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
              try {
                strExp = br.readLine();
                if (strExp.equals(strQuit)) {
                  System.exit(0);
                }
                ScriptEngineManager mgr = new ScriptEngineManager();
                ScriptEngine engine = mgr.getEngineByName("JavaScript");
                System.out.println(engine.eval(strExp));     
              }
              catch (IOException e) {
                System.out.println("Error!");
                System.exit(1);
              }
            }
          }
        }

        This will handle the arithmetic stuff, but other things like exponents and such probably require something more sophisticated. Program runs in an infinite loop...Enter quit to exit program.

        Good luck.  8)



        thanks your code worked!! ;D

        Java tutorial was recently updated.
        http://download.oracle.com/javase/tutorial/
        The file API is new
        http://download.oracle.com/javase/tutorial/essential/io/fileio.html
        Passing stuff from the command line:
        http://download.oracle.com/javase/tutorial/essential/io/cl.html
        Hope this is of some help.

        The conventional way to pass two numbers from console input is to separate them with a comma.


        thanks you for the links! ;D

        reddevilggg



          Expert

          Thanked: 69
        • Experience: Beginner
        • OS: Windows 7
        Re: java program
        « Reply #6 on: August 15, 2011, 04:43:33 AM »

        yeah,it's a home work,but before posting here i already pass my homework,the question that i post here was a plus points if done,and after passing it still can't do it so i decided to ask here,

        So, what have you learned from this ???
        11 cheers for binary !

        wagpofafi

          Topic Starter


          Beginner

          Re: java program
          « Reply #7 on: August 15, 2011, 05:12:19 AM »
          study more  ;D

          and this
          import javax.script.ScriptEngineManager;
          import javax.script.ScriptEngine;  ;D