Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 7 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
Eclipse is giving me the error "The left-hand side must be a variable" at this part of my code:
else
{ for(int i=0; i>=cellphoneArr.length; i++)
{if (cell_1.equals2(cellphoneArr[i]))
System.out.println(cellphoneArr[i]);
else
(cell_1.equals3(cellphoneArr[i])); ---> this is the error
System.out.println(cellphoneArr[i]);
}
The method equals3 is the following:
public boolean equals3(Cellphone phone)
{ if (brand.equals(phone));
}
I've been trying to figure this one out, but the way I invoked my two other methods equals 1 and 2 both worked with the object cell_1.
Try it as:
else
{ for(int i=0; i>=cellphoneArr.length; i++)
{if (cell_1.equals2(cellphoneArr[i]))
System.out.println(cellphoneArr[i]);
else if(cell_1.equals3(cellphoneArr[i]))
System.out.println(cellphoneArr[i]);
}
and the method equals3 must return a boolean values as:
public boolean equals3(Cellphone phone)
{ if (brand.equals(phone))
return true;
else
return false;
}
Remove ; at the end of else, Just like you did for if.
else(cell_1.equals3(cellphoneArr[i]));
^
Return a Boolean value from you function, instead of if
public boolean equals3(Cellphone phone)
{
return (brand.equals(phone));
}
You need to add an if:
else if (cell_1.equals3(cellphoneArr[i]))
You need an if following the else
EDIT
The code should be
if (cell_1.equals2(cellphoneArr[i])) {
System.out.println(cellphoneArr[i]);
} else if (cell_1.equals3(cellphoneArr[i])) {
System.out.println(cellphoneArr[i]);
}
Formatting allows you to pin point these kind of errors easily. If you are using Eclipse, do ctrl + shift + f
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
This is my code:
if(weight<=5)
cost=(2.8*weight);
if(weight<=20)
cost=(2.8*5)+(5.2*(weight-5));
if(weight<=50)
cost=(2.8*5)+(5.2*15)+(7*(weight-20));
if(weight>50)
cost=(2.8*5)+(5.2*15)+(7*30)+(8.6*(weight-50));
when weight is lesser than 6.9, why is the answer all negative?
when you write an IF nested statement, you must use the ELSE statement to prevent your scenario.
The right code:
if(weight<=5) {
cost=(2.8*weight);
} else if(weight<=20) {
cost=(2.8*5)+(5.2*(weight-5));
} else if(weight<=50) {
cost=(2.8*5)+(5.2*15)+(7*(weight-20));
} else if(weight>50) {
cost=(2.8*5)+(5.2*15)+(7*30)+(8.6*(weight-50));
}
In alternative, you must defined better your ranges, as follow:
if(weight<=5) {
cost=(2.8*weight);
} if(weight > 5 && weight<=20) {
cost=(2.8*5)+(5.2*(weight-5));
} if(weight > 20 && weight<=50) {
cost=(2.8*5)+(5.2*15)+(7*(weight-20));
} if(weight>50) {
cost=(2.8*5)+(5.2*15)+(7*30)+(8.6*(weight-50));
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I am a beginner in Java and I am working right now with a small projekt, a hangman game. One of the functions I am working on right now is a method where it takes a char input, check if the input is already added to the list or not, if it is, a message will show up saying "You have already used that character!" and the user will have to guess again, otherwise the input will be added to the list. My issue right now is that nothing is happening, inputs are not added to the list at all.
This is what I have done so far:
Any advice/help would be appreciated!
public static ArrayList<Character> getGuesses(ArrayList<Character> allGuesses, char input){
for (int i = 0; i < allGuesses.size(); i++) {
if (allGuesses.get(i) == input) {
System.out.println("You have already used that character!");
}else {
allGuesses.add(input);
}
}
return allGuesses;
}
You are adding the input character on each iteration as you search the collection. You should only add it after you have searched the collection and not found it.
for (int index = 0 ; index < allGuesses.size() ; ++index) {
if (allGuesses.get(index) == input) {
System.out.println("You already used that character!");
return allGuesses;
}
}
allGuesses.add(input);
return allGuesses;
However, this can be simplified by using the Collection contains method such that you do not employ a loop.
if (allGuesses.contains(input)) {
System.out.println("You already used that character!");
return allGuesses;
}
allGuesses.add(input);
return allGuesses;
If possible, consider switching the type of allGuesses to a Set implementation (e.g. HashSet). A Set seems to better match how you are using your collection and allows you to reduce this method to...
if (! allGuesses.add(input)) {
System.out.println("You already used that character!");
}
return allGuesses;
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 4 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
I tried to write an primenumber generator. The method calcall() should return prime numbers (2,3,5,7...). Unfortunately I get the error, that the method doesn't returns an integer, wich I don't understand. Here is my code:
package primenumber;
public class primecalc {
public static int calcall(int a) { //actual generator
int konstante = a; //is this number a prime num?
int divisor = a-1; //divisor
int var1 = 0; //variable = 0
while(divisor>1) {
int quotient = konstante%divisor; //calc modulo
if(quotient == 0) { //if modulo==0 switch var1 to
var1++; //1 -> no primenumber
break; //stop calculating
} else { //else keep calculculating
divisor--; //until divisor <= 1
}
}
if(var1==0) { //if var1 still 0;
return konstante; //is a primnumber ->
} //return konstante
}
public static void main(String[] args) { //main function
int number = 3; //start with 3
while(True) { //(i'll add 2 manually)
System.out.println(calcall(number)); //print the prime number
number++; //increase number by one
}
}
}
The error is:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
This method must return a result of type int
at primenumber/primenumber.primecalc.calcall(primecalc.java:5)
at primenumber/primenumber.primecalc.main(primecalc.java:28)
What is wrong?
The gray lines on the code you posted are being ignored by the compiler.
The use of /* and */ makes everything between these seen by the compiler as comments. And that is why those lines are grayed out. If you want to comment on the same line as the code, I'd advise you to use //.
Also, it is common practise to use multi-line comments only to describe functions and place them just above the header of the function. Any other comments should be short, concise and describe functionality. Good variable names and well written code should do most of the explaining, and single line comments should be used when it's a bit harder toperceive what's going on.
Cheers
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
I'm trying to set up a while loop as follows:
while(!data.equals("String");
Whenever I run this code, I get a NullPointerException.
Why is this?
If I change the code to:
while(data.equals("String");
I get no such exception, so there must be data in data, correct?
EDIT: Adding real code at the behest of commentors.
The following code is a method that attempts to convert infix notation into postfix notation.
public static Queue infixConvert (LinkedListTest infix){
Stack stack = new Stack();
Queue postfix = new Queue();
while(infix.head.data != "EOF"){
if(isNumber(infix.head.data)){
postfix.insert(infix.head.data);
System.out.println("Insert Queue");
System.out.println("Operator");
}
else if (infix.head.data.equals("(") || infix.head.data.equals(")")){
if(("(").equals(infix.head.data)){
stack.push(infix.head.data);
System.out.println("Open paren");
}
else {
infix.delete(")");
while(!"(".equals(stack.head.data)){
stack.delete(")");
postfix.insert(stack.pop());
System.out.println("Insert Queue");
}
stack.delete("(");
System.out.println("Close Paren");
}
}
else{
if(!(highPrec(stack.head.data, infix.head.data))){
stack.push(infix.head.data);
System.out.println("Push onto Lesser Operand");
}
else if(highPrec(stack.head.data, infix.head.data)){
while(stack.head.data != null){
if (stack.head.data != "("){
postfix.insert(stack.pop());
}
else break;
}
stack.push(infix.head.data);
System.out.println("Push onto Greater Operand");
}
if (infix.head.data == "EOL"){
while(stack.head.data != "EOL"){
postfix.insert(stack.pop());
System.out.println("End Line");
}
}
}
System.out.println(infix.head.data);
infix.head = infix.head.next;
System.out.println("loop\n");
}
return postfix;
}
}
EDIT: Added stack trace
at Calculator.infixConvert(Calculator.java:57)
at Test.main(Test.java:7)
You could do it "Yoda-style":
while(!"String".equals(data)) {
//writing code here you must!
}
because for the case data is null then it would not cause NPE because you call equals method of the "String"
I have resolved the issue.
There was some other unintentional behavior that caused the "(" String to be removed from the stack, so when the while loop ran it would run through the entire stack until it hit null, and provided me with a NPE.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
if(check !=0){
if (wee[i]>wee[i+1])
{
System.out.println("False");
check = 0;
else()
System.out.println("True!");
}
}
The Code snippet contains a nested if loop. The second set keeps returning "else without if" and i don't understand why. I've tried it with and without the parenthesis.
to be more clear: Why is the compiler returning an "else" without "if" error.
Change what you have to this:
if(check !=0){
if (wee[i]>wee[i+1])
{
System.out.println("False");
check = 0;
}
else
{
System.out.println("True!");
}
}
You were missing the closing paranthesis for the inner if statement.
Try:
if(check !=0)
{
if (wee[i]>wee[i+1])
{
System.out.println("False");
check = 0;
}
else
{
System.out.println("True!");
}
}
Or for fun:
System.out.println(wee[i] > wee[i+1] ? "False" : "True");