Java error how (Cannot convert double to bolean) [closed] - java

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 1 year ago.
Improve this question
I have been getting back in to Java after not using it for a while and tried to make a calculator one that has more then just +. However, I don't have a lot of experience coding, so I don't understand what I did wrong. It says I cant turn a double into a boolean, but that's not what I'm doing. Here is the code I get an error on.
if (One = UserInput.nextDouble()) {
System.out.println("Your answer is "+ Plus +"!");
}else if (Two = UserInput.nextDouble()) {
System.out.println("Your answer is "+ Sub +"!");
}else (Three = UserInput.nextDouble()) {
System.out.println("Your answer is "+ Multi +"!");

You should use "==" for comparison in if blocks.

Single = sign is an assignment operator and does not return the boolean (logic) value that is necesery to check condition in the if ... else statement. Insteed = you should use == operator to compare values or Objects.equals method.

Related

Java: Is it possible to have variables of the same name in one method seperated by an if statement [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 1 year ago.
Improve this question
I have a piece of code which sets a String to a value depending on an if statement:
if (a == 1) {
String txt = "Yes";
}
else if (a == 2) {
String txt = "No";
}
I realise I could define the txt variable outside of the if-else statement but I would like to know if this is possible and won't cause any issues.
Thanks in advance.
It will work, but only if you use that value within the statement only. You can't reference variables declared within an if-statement outside of the if-statement. To get around this you can declare the variable before the if-statement:
String txt;
if (a == 1) {
txt = "Yes";
}
else if (a == 2) {
txt = "No";
}
Then you can continue to use the variable txt elsewhere.
yes, it is possible but will it cause any issues? it depends on where you want to use 'txt' value what you want to do with value
you can check detail on scope of declaration here https://docs.oracle.com/javase/specs/jls/se8/html/jls-6.html#jls-6.3

Need Advice: Java Anti If Statement Anti Loops [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 2 years ago.
Improve this question
This is only my third program in java. I'm stuck on this part of the problem.
"The formula is not valid if T > 50 in absolute value or if v > 120 or < 3"
I'm not sure how to translate this into code while restricted to use the following:
no, if statements
no, loops
no, importing
no, new classes to be added
Thank you!
public boolean validateFormula(int T, int v) {
return !(abs(T)>50 || v > 120 || v < 3);
}
In giving you a direct answer to this problem, you might not be able to replicate it next time, or to be able to think for yourself.
In import java.util.* , Math.abs(int n) will return the absolute value of n. The || operator means 'or'. I will not assume that you know the basic structure of an if loop so here:
if(condition || condition || condition){
//execute code here
}
You have everything that you need to solve this, good luck. Furthermore, if you do have any more simple questions like this, I would suggest doing a google search instead.

How do I copy the entire literal text of a string in Java? [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 2 years ago.
Improve this question
I'm new to coding, so this may sound like a dumb question, but here:
String yes = "Yes. ";
String no = "But no.";
String whole = yes + no;
When you print whole, you get "Yes. But no."
yes = "No."
String whole2 = whole;
I want whole2's value to be "yes + no;" because the variable "yes" has been edited and I want it to display the new data. However, whole2 only gets "Yes. But no." Is there any way I can do this? If not, please describe another way that gets the result I prefer.
The simple way to do it is just to assign it to the same variables again.
For example:
String whole2 = yes + no;
It saves the hassle of creating redundant methods etc,.

Trimming String in Java [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 need to perform some operation on string.
Scenario is I need to compare that a[i] = b[i].
now say a[i] = Set Temperature
Now since a[i] contains the word Temperature, b[i] will be set to "Set Temerature (C)". (This is the business rule).
In this case a[i] will not be equal to b[i].
For my testing purpose how can trim value of b[i] to set to Set Temprature?
You could use String#contains instead.
String first = "Set Temperature";
String second = "Set Temperature (C)";
if(second.contains(first)) {
// logic
}
Ordering is important - note that if you reverse the call between first and second, it will fail, since first does not contain any characters such as " (C)".
Use String.contains(). See the java docs
http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#contains(java.lang.CharSequence)
Not sure if I got your question right.
But based on what I understood - instead of checking for equality you can check
if(b[i].startsWith(a[i])

How do I fix these incompatible data types? [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 9 years ago.
Improve this question
I'm adding two random numbers (generated by my program). The operation variable is a String that the user inputs what type of math problem they wish to have(+,-, or *). This is only one section of my full program.
public static double getCorrectAnswer(int operand1, int operand2, String operation){
double correctResponse;
correctResponse =(operand1 + (operation) + operand2);
return correctResponse;
}
Use if:
if (operand.equals("+")) {
...
}
else if (operand.equals("*")) {
...
}
...
You can also use a switch.
Note that you should return an int, since adding and multiplying integers will return an integer.
This may be too much for you but you could have a look at a formal language parser such as ANTLR

Categories

Resources