Creating a simple calculator using strings in Java [closed] - java

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
I'm trying to learn Java. My current assignment is to build a simple four function calculator..... this would be easy given if/else and/ or switch statements, but I'm supposed to build this using methods.
The original input has to be put in as a single string, so, in my mind, I'm going to have to take the single string and create substrings, then somehow convert these substrings into double values, while deleting whatever whitespace could possibly be between characters. My current idea is to somehow identify the "+,-,*, or /" within the string and divide into substrings before and after these values, using the appropriate defined method for whichever operator to do the calculations....
The problem is that I can't see a good way to divide these up into substrings or how to convert the numbers involved into double values. Anyone got any advice for me? Keep in mind, what we have gone through is pretty limited and I feel like I'm missing something REALLY simple out there.

You can split a string based on a particular character using str.split("\\+"), for example. You can convert the split pieces of the string to doubles by using Double.parseDouble(str);

Related

I have a string with a number and a letter, is there a way to move the integer into a separate int variable? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
So I'm working on a school project and it includes a Combined Gas Law calculator where the user may input the temperature in °celsius (e.g. 1°C) and the code converts it to kelvin; if the user's input is in kelvin already, it does not do that and continues with the equation. So does anyone know how I can separate the two data types into two different variables in Java?
You could split over a specific string (degree character for example), store in a String array and parse the first element. Something like this:
String str = "47°C"
String[] strArray = str.split("°");
int number = Integer.parseInt(strArray[0]);
Congratulations, you are working on something but not writing it's code. I can tell you a few tips about how to implement it's code.
You have a string that has some numbers in it, and also has unit. Try searching 'How to extract numbers from a string'. Now you have a number.
Find the unit in the string by looking the last character in the string.
If the condition is ok for your homework, calculate the new value.
Print the result.

Sorting String based on similarities [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
consider the following Strings:
he llo
goodbye
hello
= (goodbye)
(he)(llo)
good bye
helium
I'm trying to sort these in such a way that similar words comes together, I know
alphanumerical sorting is not an option
removing special chars ",-_ and etc then comparing is certainly helpful but results won't be as good as I hope for.
NOTE :
there might be few different desired ouput for this, one of which is :
DESIRED OUTPUT:
hello
he llo
(he)(llo)
helium
goodbye
good bye
= (goodbye)
so my question is that if there is a java package that compares strings and ultimately sort them based on it .
I've heard of terms such as n-gram and skip-gram but didn't quite understand them. I'm not even sure if they can be useful for me at all.
UPDATE:
finding similarities is certainly part of my question but the main problem is the sorting part.
Here's one possible approach.
Calculate the edit distance/Levenshtein distance between each pair of strings and then you use view the strings as a complete graph where the edge weights come from the edit distance. Choose a threshold for those weights and remove all the weights that to high. Then find the cliques in this graph. If your threshold is fairly low perhaps even finding connected components would be an option.
Note:
Perhaps it would be better to substitute some edit distance with one of the similarity measures in the link that #dognose posted.
Also, note that finding cliques will be very slow if you have a large numbers of strings

Using StringTokenizer to do This [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I need to use StringTokenizer to search through a string, and if they find this two words in the string, they will print "Yes.", if not, it'll print "No."
These two words are "eat", and "yet", and the string is "Did you eat yet?"
If it finds both words in that string it's suppose to print out yes, and if not, it's suppose to print out no. I have no idea how to do this. If you do, then please help.
StringTokenizer st = new StringTokenizer("Did you eat yet?");
This is how you initialize a StringTokenizer. Then your implementation should use the .hasMoreTokens() method to step through the tokens and check if they are equal to the words you are searching for.
Not sure why would want to use this method however, as..
StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. It is recommended that anyone seeking this functionality use the split method of String or the java.util.regex package instead.
From the documentation at http://docs.oracle.com/javase/7/docs/api/java/util/StringTokenizer.html
As others have suggested, using StringTokenizer is discouraged and in this case is over complicating the procedure in the first place.
Getting familiar with the Java String class is the right start. Here we find it's possible to determine if a string contains() the target string with yourString.contains(yourSubString).
You can combine this with another call to contains() in a conditional with...
if (yourString.contains(someSubString) &&
yourString.contains(someOtherSubString)) { ... }

How do you add JTextFields? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am making a GUI in Java, and I was wondering how do you add user inputs (integers) in a JTextField? If so, please post with some code so I can understand. (I am just a beginner programmer)
What all you need to do:
jTextField.setText(String.valueOf(intValue));
Because JTextField takes a String to set its value you have to convert the int to String.
See This for more details.
A JTextField contains text. You get this text using textField.getText(), which returns a String. This String might happen to represent a valid integer. If you want to get the value of the integer, you need to transform the String into an int. That's done using the Integer.parseInt() method:
int sum = Integer.parseInt(textField1.getText()) + Integer.parseInt(textField2.getText());
Of course, if one of the text fields contains text that doesn't represent an integer, you'll get an exception, as documented in the Integer.parseInt() javadoc.
Given your question, it seems you don't really understand basic notions such as types, and basic classes such as String and Integer. My advice would be to forget about Swing for the moment, and exercise with basic programs not involving any UI.

how to solve calculations in java [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I'm working on a calculator, it should receive the input as a String and then perform the calculation, outputting the result.
For example, the input could be
((23+17) mod 7 × 4 AND 13
and the output would be 4, as expected.
How can I parse the input, to extract all the operands and perform the calculation ?
The other answers are just "how to set a variable to this result" but if you're actually looking to parse input, you should refer to this:
Equation (expression) parser with precedence?
There are a number of ways to go about solving this kind of problem, and a number of algorithms for doing so. Some of them are stack based, some perform a descent of the "tree", and I can even think of a (somewhat) convoluted way to OOP-ize it. I would start with the link above.
You can use regular expressions to parse the string.
http://docs.oracle.com/javase/1.4.2/docs/api/java/util/regex/Pattern.html
First you have to look for the most important arguments like [() - parentheses], then less [*/] and [+-]. You have to divide the whole string into parts.
Examples:
Simple calculator (bottom of the page)
Another calculator
Both in Java.
see this page for your reference.
http://www.tutorialspoint.com/java/java_basic_operators.htm
all operators in java are explained very clearly.

Categories