• Math in Java
    10 replies, posted
Is there any function that can do something like this? [CODE]crappy = THEFUNCTION(777/111-3); System.out.println(crappy);[/CODE]And then comess out as: [CODE]4[/CODE]
[QUOTE=Kamrua;19664701]Is there any function that can do something like this? [CODE]crappy = THEFUNCTION(777/111-3); System.out.println(crappy);[/CODE]And then comess out as: [CODE]4[/CODE][/QUOTE] System.out.println(777/111-3);
[QUOTE=nullsquared;19664717]System.out.println(777/111-3);[/QUOTE] Lets say a user can input their own calculation. EDITED: Blahh, I'm stupid. Got it.
You want to either write or use a mathematical equations parser / evaluator Like [url=http://www.singularsys.com/jep/]JEP[/url] Or take a look at these: [url]http://www.fileguru.com/Math-Expression-Parser---JbcParser-For-Java/info[/url] [url]http://www.jguru.com/faq/view.jsp?EID=480122[/url]
[QUOTE=Borsty;19664831]You want to either write or use a mathematical equations parser / evaluator Like [URL="http://www.singularsys.com/jep/"]JEP[/URL] Or take a look at these: [URL]http://www.fileguru.com/Math-Expression-Parser---JbcParser-For-Java/info[/URL] [URL]http://www.jguru.com/faq/view.jsp?EID=480122[/URL][/QUOTE] Alright thanks. I have another question though.. How can I add one letter to a string one at a time? Let's say a button is pressed... If this button is pressed once, String lol = "f"; if it is pressed twice, String lol = "ff";... etc. You know what I mean?
Use Scanner [code] Scanner input= new Scanner(System.in); int first = input.nextInt(); int second = input.nextInt(); System.out.println(first/second); [/code]
lol += "f";
Thanks @above, worked great.
You should use a StringBuilder instead. String creates a new object everytime you concatenate, while StringBuilder was made for what you want to do.
-whoops, my bad-
[QUOTE=Robber;19666259]You should use a StringBuilder instead. String creates a new object everytime you concatenate, while StringBuilder was made for what you want to do.[/QUOTE] This is very true; although only recommended for 3+ concatenations. That's about the magic number by most benchmarks. Also you have to admit for just 2 concatenations it's a lot of boilerplate.
Sorry, you need to Log In to post a reply to this thread.