I'm in my first semester of programming as a first year college and we were tasked to make a calculator GUI. I'm almost done but I need to make the "Error" appear if the denominator is 0 but it outputs 0.0. My other problem is that I need the gui to restart after showing the final answer but what happens is that after I clicked equals then clicked the number it just continues. So if I press 1+1 then clicked =, it outputs 2 but when I clicked a number for example 1, it just becomes 21.
Also, how do I remove the .0 at the end of every answer? I tried endsWith and replace after every equation but it's not working.
#Override
public void actionPerformed(ActionEvent e){
for(int i=0;i<10;i++) {
if(e.getSource() == numbers[i]) {
text.setText(text.getText().concat(String.valueOf(i)));
}
}
if(e.getSource()==dec) {
if (text.getText().contains(".")) {
return;
} else {
text.setText(text.getText() + ".");
}
}
if(e.getSource()==add) {
num1 = Double.parseDouble(text.getText());
operator ='+';
label.setText(text.getText() + "+");
text.setText("");
}
if(e.getSource()==sub) {
num1 = Double.parseDouble(text.getText());
operator ='-';
label.setText(text.getText() + "-");
text.setText("");
}
if(e.getSource()==mult) {
num1 = Double.parseDouble(text.getText());
operator ='*';
label.setText(text.getText() + "*");
text.setText("");
}
if(e.getSource()==div) {
num1 = Double.parseDouble(text.getText());
operator ='/';
label.setText(text.getText() + "/");
text.setText("");
}
if(e.getSource()==neg) {
Double neg = Double.parseDouble(text.getText());
neg*=-1;
text.setText(String.valueOf(neg));
}
if(e.getSource()==per) {
num1 = Double.parseDouble(text.getText())/100;
label.setText(text.getText() + "%");
text.setText(String.valueOf(num1));
label.setText("");
}
if(e.getSource()==equ) {
num2=Double.parseDouble(text.getText());
switch(operator) {
case'+':
ans=num1+num2;
break;
case'-':
ans=num1-num2;
break;
case'*':
ans=num1*num2;
break;
case'/':
if (num2==0)
text.setText("Error");
else
ans=num1/num2;
break;
}
label.setText("");
text.setText(String.valueOf(ans));
}
}
}
}
Regarding your first question about displaying "Error" when denominator is zero.
When you press "=" button you first get into '/' branch of the switch(operator) statement. There you check that num2 is equal to zero and set the text field to "Error". However, after that you exit the switch statement and continue with label.setText(""); followed by text.setText(String.valueOf(ans));. Because the current value of ans is zero, the last statement sets the value of text field to "0.0" overwriting the previous value of "Error". There are different ways to deal with that. Now when you understand the cause of the problem you can try to find the solution yourself.
Regarding your second question about how to reset the state of the calculator. For example you can create a boolean state variable, which will be true if the calculator is ready for new input and false if it is in the middle of some input. You initialize this state variable to true and then every time you press some button you set it to false. If the state variable is true, then reset the text field to an empty string before you append a number to it. Finally, set the state variable back to true after the user has pressed '=' button. So, you can do something like this:
// We reset the text field to an empty string at the start of each calculation
if (state) {
text.setText("");
}
// Here is your code
.....
// And at the very end:
// Change the state to false if user pressed anything except the equality sign
state = e.getSource()==equ;
Regarding the third question how to display the answer without unneccessary precision. Check this thread: Best way to convert a double to String without decimal places. There is an answer to your question there, even if it is not the first answer in the thread.
Related
Good evening,
I'm at a loss as to why this loop isn't working at all. It's ruining my entire application. below is the code:
System.out.println("Please tell me what to count till?");
do
{
try
{
newEndingValue= input.nextInt();
if(newEndingValue >= 0 || newEndingValue <= 0)
{
break; //breaks the loop
}
}
catch (InputMismatchException e)
{
System.out.println("My Apologies, but COMMAND NOT RECOGNIZED!" + "\nPlease tell me what to count till?");
input.next();
}
}
while(!input.hasNextInt());
v.setEndingValue(newEndingValue);
System.out.println("Please tell me what to count from?");
if(increasingOrDecreasing.equalsIgnoreCase("Increasing")|| increasingOrDecreasing.equals("++") || increasingOrDecreasing.equals("+"))
{
do
{
try
{
newInitialValue = input.nextInt();
if(newInitialValue < v.getEndingValue())
{
break;
}
else{
System.out.println("My Apologies, but starting point value must be smaller than ending point value!" + "\nPlease tell me what to count from?");
newInitialValue = (v.getEndingValue()+10);//overrides the value to something that forces the loop back
}
}
catch (InputMismatchException e)
{
System.out.println("My Apologies, but COMMAND NOT RECOGNIZED!" + "\nPlease tell me what to count from?");
input.next();
}
}
while(!input.hasNextInt() || newInitialValue > v.getEndingValue());
}
else
{
do
{
try
{
newInitialValue = input.nextInt();
if(newInitialValue > v.getEndingValue())
{
break; //breaks the loop
}
else{
System.out.println("My Apologies, but starting point value must be larger than ending point value!" + "\nPlease tell me what to count from?");
newInitialValue = (v.getEndingValue()-10);//overrides the value something that forces the loop back
}
}
catch (InputMismatchException e)
{
System.out.println("My Apologies, but COMMAND NOT RECOGNIZED!" + "\nPlease tell me what to count from?");
input.next(); //consumes the erroneously typed string value
newInitialValue = (v.getEndingValue()-10);
}
}
while(!input.hasNextInt() || newInitialValue < v.getEndingValue());
}
So the output is when entered a no, and then 1000 as follows:
Please tell me what to count till?
no
My Apologies, but COMMAND NOT RECOGNIZED!
Please tell me what to count till?
1000
Please tell me what to count from?
My Apologies, but starting point value must be smaller than ending point value!
Please tell me what to count from?
Why is it going straight to the second written else statement?
Why is it skipping user entry for newInitialValue?
Please note that if edit code after ending value block to below after entering a string for newEndingValue and then correctly enter a number, this rids me of my error but generates another one if ran again and the user cooperates:
...
newInitialValue = input.nextInt(); //essentially gets skipped over by compiler only when previous catch statement is triggered
System.out.println("Please tell me what to count from?");
if(increasingOrDecreasing.equalsIgnoreCase("Increasing")|| increasingOrDecreasing.equals("++") || increasingOrDecreasing.equals("+"))
{...
additionally since its printing out "but starting point value must be smaller than ending point value" we can deduce its working with if(incre...) loop and the do and try loops respectively. But its skipping the (newI... = input...) and the if(newIntia...) lines of code. i know this cause even manually entering in newInitialValue = 2 (within paramenter) it still goes to this else clause.
-_- So the issue is within the while statements:
while(!input.hasNextint())
this looks ahead and checks the next user input, but since the catch consumed one it looks to the next next and it gets murky... essentially if i don't use this it works.
instead i used:
while(isError == true)
and under the do loop with the nested if statement i have:
if(blah blah blah){
isError=false;
break;
and before the next loop block i simply override the isError
isError=true;
//next block of code
The problem here is that you say to java do something if value 1 is true or value2 is true or value3 is true,and then you say on the else to do the same if using or.You got to understand that you need to use and && in order to help java to understand the else parameter.
So everything is working fine for this calculator besides for the askCalcChoice1. Since askCalcChoice1 is a string, I am calling it wrong (obviously). The error says it cannot convert string to int, as well as convert int to boolean. However, when i make the inputOperation as a string, it breaks the other 2 calls below askCalcChoice1. (it breaks displayRedults and askTwoValues because those are not strings). I do not know how to format askCalcChoice in order to call for this method that is written in another class wihtout breaking anything. askCalcChoice is written as a string which i pasted below the oopCalculator code. Is there any way and can someone please show me how to write that portion of that code in oopCalculator?
int inputOperation; // user to choose the function
askCalcChoice1 myAskCalcChoice1 = new askCalcChoice1();
//menu becomes a complete string below
String menu = "Welcome to Hilda Wu's Calculator\t\t"
+ "\n1. Addition\n"
+ "2. Subtraction\n"
+ "3. Multiplication\n"
+ "4. Division\n"
+ "5. Exit\n\n";
calculatorCommands.pickNewSymbol(menu); //complete menu will be picked up as a string and display
calculatorCommands.putDownSymbol();
while (inputOperation = myAskCalcChoice1.calcChoice()) { //this will call for myAskCalcChoice1 class
calculatorCommands.pickNewSymbol("\n"); //pick up the class
calculatorCommands.putDownSymbol(); //display the class
askTwoValues myAskTwoValues = new askTwoValues();
float[] myFloats = myAskTwoValues.inputFloats(inputOperation);
displayResults myDisplayResults = new displayResults();
float result = myDisplayResults.showResults(inputOperation, myFloats);
String strFormat = "The answer is: " + result + "\n\n"; //print out The answer is as a string
calculatorCommands.pickNewSymbol(strFormat); //pick up string from above
calculatorCommands.putDownSymbol(); //display string
calculatorCommands.pickNewSymbol(menu); // pick up menu from the beginning of code, loop to calculator menu
calculatorCommands.putDownSymbol(); //display menu as loop
}
calculatorCommands.pickNewSymbol("\nThank you for using Hilda Wu's Calculator\n"); //when user choose to exit calculator
calculatorCommands.putDownSymbol();
}
String calcChoice() {
String input;
do { //do loop will continue to run until user enters correct response
System.out.print("Please enter a number between 1 and 5, A for Addition, S for Subtraction, M for Multiplication, or D for Division, or X for Exit: ");
try {
input = readInput.nextLine(); //user will enter a response
if (input.equals("A") || input.equals("S") || input.equals("M") || input.equals("D") || input.equals("X")) {
System.out.println("Thank you");
break; //user entered a character of A, S, M, or D
} else if (Integer.parseInt(input) >= 1 && Integer.parseInt(input) <= 5) {
System.out.println("Thank you");
break; //user entered a number between 1 and 5
} else {
System.out.println("Sorry, you have entered an invalid choice, please try again.");
}
continue;
}
catch (final NumberFormatException e) {
System.out.println("You have entered an invalid choice. Try again.");
continue; // loop will continue until correct answer is found
}
} while (true);
return input;
}
}
To start with, you are calling showResults with two arguments:
int choice
and
float [] f
Choice is never used.
You use input variable instead in your switch but on default you return the error showing choice.
Better pass choice as an argument in the function and be sure it is char and not other type.
Also this is not the form of a good stated question. I will not rate it down but please remake it so the whole code is correctly shown. I can not make sense of it easily. I might misunderstood it already. Please do not add comments between, be sure you have correct indentation and you got all the code in.
If you need to comment do it afterwards. It's not very complicated, just show us the code and ask what is wrong later ;)
If choice was meant to pass in the switch... then do it, but not as int but as char.
I am writing a BMI calculator application. Currently an error happens which causes the program to stop working if I do not enter data into one field. For instance, there are two JTextFIelds for 'height', one being feet and the other inches. If I just input '6' into the feet JTextField and enter nothing into inches JTextField, then enter my weight in the weight JTextField and click on calculate, it does not work.
What I want to do is display a message dialog saying "Please make sure all fields are filled in" if one field does not contain data.
Below is the ActionHandler code that is added to my 'Calculate' button.
public void actionPerformed(ActionEvent e) {
double heightFT = ((Double.parseDouble(heightFt_TF.getText()));
double heightIn = (Double.parseDouble(heightIn_TF.getText()));
double weight = (Double.parseDouble(weight_TF.getText()));
double totalHeight = (heightFT*12) + heightIn;
BMI = (weight / (totalHeight*totalHeight)) * 703;
String s = BMI+"";
s = s.substring(0,4);
BMI_TF.setText(s);
}
Solved
I have now fixed the problem. What I did was add 'throws NumberFormatException' in the method and did a try catch. In the try code block I wrote the code I want to execute if all data fields are entered. In the catch clause I wrote code that uses the NumberFormatException and simply displays the message dialog with the error message. Now, if one field is not entered, the message dialog appears!
Just check if your JTextField objects contain text.
E.g:
if (heightFt_TF.getText() == null || heightIn_TF.getText() == null || weight_TF.getText() == null) {
JOptionPane.showMessageDialog(null, "Please make sure all fields are filled in");
}
Of course you also have to make sure, that the content of the textfields really contains a number.
Download Apache Commons Lang library and use StringUtils.isBlank(myTextField.getText()); to validate your fields.
public boolean validateFields() {
if (StringUtils.isBlank(heightFt_TF.getText()) {
// show message
return false;
}
if (StringUtils.isBlank(weight_TF.getText()) {
// show message
return false;
}
return true;
}
Only run your calculation if validateFields() returns true.
public boolean validate(JTextField field) {
boolean result = field.getText() != null;
if (result) {
try {
Double.parseDouble(field.getText()));
} catch(NumberFormatException e) {
result = false
}
}
return result;
}
I'm trying to input 3 different variables into an array inside a while loop, as long as i don't enter stop for any of the variables. the while loop is only suppose to let me input a second variable value if the 1st variable isn't stop, and likewise with inputting a third variable value
Right now, the first loop goes fine and i can input all 3 variables, but the 2nd and 3rd time, the for loop outputs the first variable, but doesn't allow me to input a value before skipping to the 2nd variable.
ex of what i mean:
name:afasdf
extra info:afdsaf
unit cost:123123214
name: extra info: adflskjflk
also, entering Stop isn't ending the loop either
unit cost:123217
i know that this loop works when there's only one variable, and i've tried using a for loop instead of a while loop, and adding tons and tons of else statements, but it seems to stay the same
is there something wrong with the way i set up my breakers?
is the way i set up the last breaker(the one that stops even when i put stop for a double variable) messing up the rest of hte loop?
thank you so much
here is my code
ArrayItem s = new ArrayItem();
String Name = null, ID = null;
double Money = 0;
boolean breaker = false;
while(breaker ==false)
{
System.out.print("Name:" + "\t");
Name = Input.nextLine();
if(Name.equals("Stop")) //see if the program should stop
breaker = true;
System.out.print("Extra Info:" + "\t");
Details = Input.nextLine();
if(ID.equals("Stop"))
breaker = true;
System.out.print("Unit Cost:" + "\t");
Money = Input.nextDouble();
// suppose to let me stop even if i input stop
// when the variable is suppose to be a double
if(Input.equals("stop") || Input.equals("stop"))
breaker = true;
else
s.SetNames(Name);
s.SetInfo(Details);
s.SetCost(Money);
}
A couple of things about the code: "Name:" + "\t" can be simplified ot "Name:\t". This is true for the rest of the code. In Java, it's customary to use camelcase where the first word is lowercase. For example, s.SetMoney would be s.setMoney. Also, variables follow the same rules where Money would be money, and ID would be id. If your teacher is teaching you otherwise, then follow their style.
The loop should also be a do-while loop:
do
{
// read each value in sequence, and then check to see if you should stop
// you can/should simplify this into a function that returns the object
// that returns null if the value should stop (requiring a capital D
// double for the return type)
if ( /* reason to stop */)
{
break;
}
s.setNames(name);
s.setId(id);
s.setMoney(money);
} while (true);
private String getString(Scanner input)
{
String result = input.nextLine();
// look for STOP
if (result.equalsIgnoreCase("stop"))
{
result = null;
}
return result;
}
private Double getDouble(Scanner input)
{
Double result = null;
// read the line is a string looking for STOP
String line = getString(input);
// null if it's STOP
if (line != null)
{
try
{
result = Double.parseDouble(line);
}
catch (NumberFormatException e)
{
// not a valid number, but not STOP either!
}
}
return result;
}
There are a lot of concepts in there, but they should help as you progress. I'll let you put the pieces together.
Also, you did need to fix the brackets, but that's not the only issue. Because Money is a double, you must read the value as a String. I suspect that Input is a Scanner object, so you can check Input.hasNextDouble() if it's not, then you can conditionally check the String value to see if it's "stop" (note: you are checking for "Stop" and "stop", which are not equal). Your last, no-chances check compares the Scanner to "stop", which will never be true. Check
System.out.print("Unit Cost:\t");
if (Input.hasNextDouble())
{
Money = Input.nextDouble();
// you can now set your object
// ...
}
// it's not a double; look for "stop"
else if (Input.nextLine().equalsIgnoreCase("stop"))
{
// exit loop
break;
}
// NOTE: if it's NOT a double or stop, then you have NOT exited
// and you have not set money
breaker = true;
while(breaker){
Name = readInput("Name");
Details = readInput("Details");
Money = Double.parseDouble(readInput("Money"));
if(Name.equals("stop") || Details.equals("stop"))
breaker = false;
else {
// set ArrayItem
}
}
private static String readInput(String title){
System.out.println(title+":");
//... read input
// return value
}
I have a seperate JFrame where there is a text box (jTextArea) that takes numbers as inputs, each separated with a new line. Upon closing the JFrame with the text box, the data is supposed to be stored in an ArrayList of integers. The ArrayList is checked when clicking a button in the main JFrame and errors are logged if they happen.
The code for the JFrame with the jTextArea looks like this:
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
boolean success = false;
try{
selectedTime = Long.parseLong(jTextField1.getText());
if(selectedTime >= 10000){
success = true;
if(!jTextArea1.equals("") && !jTextArea1.equals(null)){
try{
for(int i = 0; i < jTextArea1.getLineCount(); i++){
n = Integer.parseInt(jTextArea1.getText(jTextArea1.getLineStartOffset(i),jTextArea1.getLineEndOffset(i)));
if(n <= 172){
worldsChosen.add(n);
}
}
}catch(Exception e){
errorsHappened = true;
}
}
}else{
javax.swing.JOptionPane.showMessageDialog(null,"The specified time was not above or equal to 10000 ms. Please try again.");
success = false;
}
}catch(Exception e){
javax.swing.JOptionPane.showMessageDialog(null,"The specified time was not set in numbers exclusively. Please try again.");
success = false;
}
if(success){
gui.hideWorlds();
}
}
Note: it also checks whether a text box has a number input equal to or above 10000 (this works).
The code for the main JFrame:
if(jCheckBox5.isSelected()){
checkWorld = true;
if(!worldsChosen.isEmpty()){
changeWorlds = true;
}else{
log("You've selected the option for automatic world switching,");
log("but all of your inputs weren't formatted correctly.");
errorsHappened = true;
}
}else{
errorsHappened = false;
}
if(errorsHappened == true){
log("One or multiple worlds weren't added due to incorrect formatting.");
log("Retry to make script automatically change worlds.");
}
Whenever I run the script with the check box selected and something formatted correctly in the text area
(like this):
1
2
3
4
5
etc.
It outputs all of the log messages (as if the check box had been selected but none of the inputs were formatted correctly).
I've tried to the best of my knowledge to fix this, but I just can't see how it messes up.
Any help appreciated :).
Mike Haye.
Read the api doc of getText(int, int): the second argument is not an offset. It's a length.
Side note 1: it should probably be easier to get all the text as a single string and split on newline chars, and the parse every string into an integer.
Side note 2: The test if (jTextArea1.equals("")) can't succeed. A JTextArea instance will never be equals to a String instance.
I didn't check the complete program, but this is wrong:
if(!jTextArea1.equals("") && !jTextArea1.equals(null)){
Did you forget to add the call of getText() ? The line as it is will always be evaluated to true, because the instance object of JTextArea is never equal to "" or null. The latter would imply that the jTextArea1 object was null itself. Which would give you a NPE when you call the equals method.
Do you reset the flag before checking the conditions? Consider the following case:
//suppose errorsHappened is true here
if(jCheckBox5.isSelected()){ //we get true here
checkWorld = true;
if(!worldsChosen.isEmpty()){ //not empty, so this branch is taken
changeWorlds = true;
}else{ //worldsChosen is not empty, so this would not be logged
log("You've selected the option for automatic world switching,");
log("but all of your inputs weren't formatted correctly.");
errorsHappened = true;
}
}else{ //checkbox is selected, so no reset to false here
errorsHappened = false;
}
//due to the checkbox being selected and worldsChosen not being empty,
//errorsHappend is still true (which is wrong)