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 2 years ago.
Improve this question
I'm a beginner at java and I've been trying to make a calculator using Swing. So far everything is good, but I'm having trouble with how the number pad will work. I want the calculator's output to be a float value, but I want it to be so that if you click "1", the output will display "1", not "1.00". How could I go about this?
Also, I cannot think of a way to append a number to another number. For example, if I input 1 then input 2, the output would be 1.02, not 12. How do I get the program to make the output be a whole number when possible?
For the first question: You first want to check that there are no digits after the decimal point when just displaying 1 instead of 1.00. To do so you can check that this actually is the case with this if statement:
if(number % 1.0f == 0)
next, you want to convert this number into a string that just displays the number without the decimal points, to do so you can cast number to an int and then convert it into a string with:
String.valueOf((int) number)
For the second question: To append a new digit to the number I would suggest generating a string with the number the user enters using StringBuilder. In practice it would look something like:
StringBuilder sb = new StringBuilder();
// when user presses "2" button for example:
sb.append("2");
In the end you want to convert this generated String into a float in order to perform calculations on it, which you can do with:
float value = Float.valueOf(sb.toString());
Related
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 4 years ago.
Improve this question
I'm building a calculator app which take up math expressions, parses it and displays the results, for that I use Javaluator.
Something like:
String expression = "(2^3-1)";
Double result = new DoubleEvaluator().evaluate(expression);
I use two textView for this: one for displaying user input (expression) and the other for displaying the results which are a Double.
Everything works fine but I would like to get rid of the float that is returned after each operation: e.g: 10 * 10 = 100.0. I tried something like finResult = result.intValue(); works but is broken for division operations. e.g: 2 / 3 = 0.
Is there a way to fix that?
For getting rid of trailing zero, you can use DecimalFormat and its method setMinimumFractionDigits(0).
DecimalFormat dc = new DecimalFormat();
dc.setMinimumFractionDigits(0);
String finResult = dc.format(result);
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 7 years ago.
Improve this question
I want to take a sum, quotient, remainder of two numbers using an array in java.
123456789012345+7654321, 123456789012345/7654321. What is a simplest way to calculate it using Java?(I am new to Java.)
Since you are new to java I recommend reading up on some tutorials. As it seems you are not familiar with java in general. An example, which I have not used myself, is http://www.javaworld.com/blog/java-101. It may be worth your time to read this over.
As for your actual question, you would create a variable in java. Then assign your first number to this variable. After doing this, you can perform some operations on the number.
An example in sudo code to give you an idea while not doing the work for you.
void method
var number = 100
number = number + 200
number = number / 20
print("result" . number)
If you plan to use an array its the same process in a loop.
http://www.tutorialspoint.com/java/java_loop_control.htm
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
Lets say I have program that will generate 2 random doubles, and then asks the user what is the answer.
An example would be the code below, given that num1 and num2 is randomly generated, and userAnswer is the answer that the user has keyed in.
if(num1 * num2 == userAnswer)
{
System.out.Println("You are right!");
}
I understand that multiplying doubles would never be accurate, but how can I change this code to allow errors up to 3 decimal points? I want the console to still display "You are right", even though the answer is 2.1453000001 and the user answers 2.145.
If you're trying to check for up to one percent error you could use
quotient = userAnswer / (num1 * num2);
if (quotient > 0.99 && quotient < 1.01) {
This of course assumes that the correct answer is not 0.0.
You can try with DecimalFormat.
Eg:
double d=2.1453000001;
double formatted= Double.parseDouble(new DecimalFormat("#.###").format(d));
System.out.println(formatted);
Output:
2.145
You can try
public static String format(String format,Object... args)
if you don’t want to print out the String and just want to format it for later use, you can use the static format method of the String
class (sort of like sprintf in C). It works in exactly the same way as
printf as far as formatting is concerned, but it doesn’t print the
String, it returns a new formatted String.
Floating point formatting
%f : will print the number as it is.
%15f : will pint the number as it is. If the number has less than 15 digits, the output will be padded on the left.
%.8f : will print maximum 8 decimal digits of the number.
%9.4f : will print maximum 4 decimal digits of the number. The output will occupy 9 characters at least. If the number of digits is not enough, it will be padded
For Example:
double d=12.23429837482;
String s = String.format("%.3f", d);
System.out.println(s);
Output:
12.234
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 9 years ago.
Improve this question
I have to solve the following problem:
-find flowchart or algorithm and show it in java languange
"Write a Java program that prompts the user to input a four-digit positive integers between 1001 and 9999. The program then finds the reverse of that integers. For example, if the input integer i2 3245, its reverse is 5423."
My problem is that I don't know what formula to use. I have also asked my demonstrator, but he just said that the formula uses a percentage and divide. How should I approach a solution to this problem?
Since this is a learning assignment, I will give you only hints:
To get the last digit in base N, use x % N; for base ten, that would be x % 10
To drop the last digit in base N, integer-divide by N; for base ten, that would be x /= 10
Repeating this process four times and printing the digits in reverse order will give you the desired result. Since you know that the value has exactly four digits, you do not need a loop.
This might not be what the teacher accepts/wants/expects, but for educational purposes, this would be the easiest way to do it:
String input = "1234";
int result = Integer.parseInt(new StringBuilder(input).reverse().toString());
System.out.println(result):
prints
4321
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'm rather sure I haven't found an answer to this because I'm not sure of the correct term for what I'm trying to do, so apologies in advance if it's very straight forward/well documented.
I have a set of numbers which I need to output a number in a specific position (hundreds), IE:
For 1302, I need to output 3
For 1802, I need to output 8
etc
How can I accomplish this with Java?
I should note that this is easy with 100's ( / 100), however I can't seem to figure out how to do this when the number is > 100.
a easy way is use % (modulo) and then /
like :
(input % 1000) / 100
Edit It works if you use only int number.
If you want to keep the number,
(num / 100) % 10
should generally work.
If you want to output the second number of your number you could convert it to a String and then use the substring method (doc) to get the character you want. (of course it only works if the number you want is always located at the same position).
An example of one solution, to get you on your way :)
int i = 1302;
String hundreds = Integer.toString(i).substring(1, 2);
System.out.println(hundreds);