I am learning java but stuck with this issue, I am writing a simple code but this error is throwing up again
Can some one help me with what wrong i am doing?
public String alarmClock(int day, boolean vacation) {
if (day >= 1 && day <= 5) {
if (vacation = true) {
return "10:00";
}
else (vacation = false) {return "7:00";}
}
else {
if (vacation = true) {
return "off";
}
else (vacation = false) {return "10:00";}
}
}
the error which it is giving is-
Error: else (vacation = false) {return "7:00";}
^
Syntax error, insert "AssignmentOperator ArrayInitializer" to complete ArrayInitializerAssignement
What the output i want is this -
alarmClock(1, false) → "7:00"
alarmClock(5, false) → "7:00"
alarmClock(0, false) → "10:00"
I know this may be simple but i am just new to java so i want to learn this.
Thanks in advance !
In a condition use == (comparison), not = (assignment) :
change
if (vacation = true)
to
if (vacation == true)
or even better
if (vacation)
Beside that, else (vacation = false) is invalid syntax, and you don't need it anyway. Just write else.
if (day >= 1 && day <= 5) {
if (vacation) {
return "10:00";
} else {
return "7:00";
}
} else {
if (vacation) {
return "off";
} else {
return "10:00";
}
}
Related
the below is the code I made for a codingbat problem. I think my code looks fine. the problem is below:
When squirrels get together for a party, they like to have cigars. A squirrel party is successful when the number of cigars is between 40 and 60, inclusive. Unless it is the weekend, in which case there is no upper bound on the number of cigars. Return true if the party with the given values is successful, or false otherwise.
cigarParty(30, false) → false
cigarParty(50, false) → true
cigarParty(70, true) → true
it keeps saying compile issue, "(" expected at line 6. Am i doing something wrong that I can't see?
public boolean cigarParty(int cigars, boolean isWeekend) {
if (cigars>=40 || cigars <= 60 && isWeekend){
return true;
} if else(cigars<40){
return false;
} else {
return false;
}
}
if else is wrong you should correct it to else if. This is the correct code :
public boolean cigarParty(int cigars, boolean isWeekend) {
if (cigars>=40 || cigars <= 60 && isWeekend){
return true;
}else if(cigars<40){
return false;
} else {
return false;
}
}
There is no if else, you have to use else if.
public boolean cigarParty(int cigars, boolean isWeekend) {
if (cigars>=40 || cigars <= 60 && isWeekend){
return true;
}else if(cigars<40){
return false;
} else {
return false;
}
}
So while I was typing this question, I found a workaround for a "missing return statement" error. But I still don't think this is the proper way of doing it. As you can see I have some nested if statements. I want both conditions to be met before returning anything, but I had to place the return statements outside of the nested if statements. If the last condition isn't met doubt this should cause much problem since an empty string will be returned, but I just feel as if this isn't the best way of going about doing things.
1st Edit: with my current update, i'm still missing a return statement. I could do the same fix i applied but I feel as if it is innapropriate.
public String findProtein(String dna) {
int start = dna.indexOf("atg");
int stop1 = dna.indexOf("tag", start + 3);
int stop2 = dna.indexOf("tga", start + 3);
int stop3 = dna.indexOf("taa", start + 3);
String subStr1 = dna.substring(start, stop1);
String subStr2 = dna.substring(start, stop2);
String subStr3 = dna.substring(start, stop3);
boolean geneFound = false;
if (subStr1.length() % 3 == 0) {
geneFound = true;
return subStr1;
}
if (geneFound == false) {
if (subStr2.length() % 3 == 0) {
geneFound = true;
}
return subStr2;
}
if (geneFound == false) {
if (subStr3.length() % 3 == 0) {
geneFound = true;
}
return subStr3;
}
if (geneFound == false) {
return "";
}
}
2nd Edit: additional code
private void stopCodon(String gene){
//This prints out the last 3 characters of the gene
String stopCodon = gene.substring(gene.length() - 3);
System.out.println(stopCodon);
}
public void testing() {
String a = "ataaactatgttttaaatgt";
String result = findProtein(a);
stopCodon(result);
}
If it were me, I would edit the following logic
if( subStr1.length() % 3 ==0 ){
geneFound = true;
return subStr1;
}
if(geneFound == false){
if(subStr2.length( )% 3 ==0 ){
geneFound = true;
}return subStr2;
}
if(geneFound == false){
if(subStr3.length( )% 3 ==0 ){
geneFound = true;
}
return subStr3;
}
if (geneFound == false){
return "";
}
To the following using else if statements:
if( subStr1.length() % 3 ==0 ){
return subStr1;
} else if (substr2.length()%3==0){
return substr2;
} else if (substr3.length()%3 == 0) {
return substr3;
} else {
return null;
}
I'm also not sure if String subStr1 = dna.substring(start,stop1); is something you want since an Exception will be thrown if the stop codon doesn't exist, but it would be hard to judge without you giving us additional information.
Added
Kind of saw this coming, but if you look at the description for indexOf
https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#indexOf(int)
the index of the first occurrence of the character in the character sequence represented by this object, or -1 if the character does not occur.
If you want to check if the substring exists, you check if the index is -1
I'm only gonna go through an example for the first substring
int stop1 = dna.indexOf("tag", start + 3);
if(stop != -1) {
return dna.substring(start, stop1);
}
You should start by checking if the start codon exists at all and return null if it doesn't exist immediately, since locations of stop codons are useless without start codons.
Hopefully this helps
if( subStr1.length() % 3 ==0 ){
geneFound = true;
result = subStr1;
}else if(geneFound == false){
if(subStr2.length( )% 3 ==0 ){
geneFound = true;
}
result = subStr2;
}else if(geneFound == false)
if(subStr3.length( )% 3 ==0 ){
geneFound = true;
}
result = subStr3;
}
if (geneFound == false){
result = "";
}
return result;
result is of type String.
However any one of three if statements will return the value. If not fourth if statement will return the value.
You can assign the result to a variable and return it at the end
Why don't you return something like this ?
public String findProtein(String dna) {
String valueToBeReturned = "";
if(condition 1){
valueToBeReturned = "value1"
}
if(condition 2){
valueToBeReturned = "value2"
}
//Rest of the conditions
return valueToBeReturned; //Finally return the specific value
}
How about remove unnecessary block of code?
if (geneFound == false) {
return "";
}
Since you return a value and the boolean is a local variable, it doesn't really matter if you change the boolean value or not in this code. I really don't see a use for it at the time. I simplified the code following your logic!
public String findProtein(String dna) {
int start = dna.indexOf("atg");
int stop1 = dna.indexOf("tag", start+3);
int stop2 = dna.indexOf("tga",start+3);
int stop3 = dna.indexOf("taa",start+3);
String subStr1 = dna.substring(start,stop1);
String subStr2 = dna.substring(start,stop2);
String subStr3 = dna.substring(start,stop3);
if(subStr1.length() % 3 == 0 ) {
return subStr1;
}
if(subStr2.length() % 3 == 0 ){
return subStr2;
}
if(subStr3.length( )% 3 ==0 ){
return subStr3;
}
return "";
}
public class CodingBat {
public static void main(String[] args){
CodingBat object = new CodingBat();
System.out.print(object.parrotTrouble(true,20));
}
public boolean parrotTrouble(boolean talking, int hour) {
if(talking == false){
return false;
}
else if(hour > 7 || hour >20){
return true;
}
}
}
I am confused, why I am getting an error where the public method parrotTrouble is underlined saying it must return a boolean, which I currently have?
Compiler says you need to return some value since your method return type is boolean.
You have returned false in If condition, and return true in else if condition.
you need to return something outside of if/else if as well.
something as below, as per comments from #Andreas, code can be reduced to below
Original as per OP
public boolean parrotTrouble(boolean talking, int hour) {
if (talking == false) {
return false;
} else if (hour > 7 || hour > 20) {
return true;
}
return booleanValue; // can be true/false as per your logic
}
Edit
public boolean parrotTrouble(boolean talking, int hour) {
return (talking && (hour > 7 || hour > 20));
}
As pointed by #Codebender, wither use if else condition, then no
need to return boolean value at the last, but if you are using if -
else if you have to return boolean value at the last. As compiler is
also not sure that it will go in one of the conditions surely.
public boolean parrotTrouble(boolean talking, int hour) {
boolean answer = false;
if(talking == false){
answer = false;
}
else if(hour < 7 || hour >20){
answer = true;
}
return answer;
}
Thank you for your help, I changed my code to this and it worked correctly and answered the question <3
Because the eclipse was error:
else delete token????
the code is this:
public void go(View v)
{
String a= url.getText().toString();
if (a.contains("www.") == true); {
webView.loadUrl(url.getText().toString());
}
else {
String search = "https://www.google.com/search?q="+url.getText();
webView.loadUrl(search);
}
}
PLease help me
An if statement requires a body - remove the semi-colon
if (a.contains("www.") == true); {
^
if (a.contains("www.") == true);
==
if (a.contains("www.") == true) { }
Which does nothing. So webView.loadUrl(url.getText().toString()); will be always executed since it's in an inner block.
If you're using an IDE, you can make it warn you of things like that (empty if statements).
You're getting an error because your code is:
if (a.contains("www.") == true) { }
{
//some code
}
else {
}
But the else has no if before it..
Replace this
if (a.contains("www.") == true); {
with this
if (a.contains("www.") == true) {
I am working through the JavaBat questions and am confused about my logic.
Here's the task:
Given a day of the week encoded as 0=Sun, 1=Mon, 2=Tue, ...6=Sat, and
a boolean indicating if we are on vacation, return a string of the
form "7:00" indicating when the alarm clock should ring. Weekdays, the
alarm should be "7:00" and on the weekend it should be "10:00". Unless
we are on vacation -- then on weekdays it should be "10:00" and
weekends it should be "off".
alarmClock(1, false) → "7:00" alarmClock(5, false) → "7:00"
alarmClock(0, false) → "10:00"
Here's my code:
public String alarmClock(int day, boolean vacation) {
if ( (day >=1 && day <=5) && (!vacation)) {
return "7:00";
} else if ( (day >=1 && day <=5) && (vacation)) {
return "10:00";
} else {
return "off";
}
}
Why do these two tests fail?
alarmClock(0, false) → "10:00" "off" X
alarmClock(6, false) → "10:00" "off" X
Surely, this line covers it?
if (day >=1 && day <=5) && (!vacation))
how about this?
public String alarmClock(int day, boolean vacation) {
if (day >=1 && day <=5) {
return vacation ? "10:00" : "7:00";
} else {
return vacation ? "off" : "10:00";
}
}
Note it does depend if your coding convention allows the use of the turnary operator. But in this case I think the logic is easier to read.
Surely, this line covers it?
if ((day >=1 && day <=5) && (!vacation))
No, that line doesn't cover it. If the day is Sunday or Saturday (0 or 6), the first part of your "and" expression (day >=1 && day <=5) will be false, since 0 and 6 are not between 1 and 5 inclusive.
The only branch that handles days 0 and 6 is your else branch: "off".
This is a great time to use helper methods to express your logic closer to the English description:
if ( isWeekday(day) ) {
if ( vacation ) {
//what to return here?
} else {
//what to return here?
}
} else {
if ( vacation ) {
//what to return here?
} else {
//what to return here?
}
}
Then you just need to implement isWeekday:
private boolean isWeekday(int day) {
return /*fill this in*/;
}
Although the ternary operator is useful here, as this problem can be done in a single return statement, the following code below allows readability.
public String alarmClock(int day, boolean vacation) {
if(vacation){ //if we are on vacation
if(day > 0 && day < 6){ //if it is weekday and we are on vacation
return "10:00";
}
else return "off"; //it must be the weekend!
}
//from here on out all the cases where vacation is true have been weeded out
if(day > 0 && day < 6){
return "7:00";
}
else return "10:00";
}
if ((day == 0 || day == 6) && (!vacation) || (day >= 1 && day <= 5) && (vacation)) {
return "10:00";
}
if ((day >= 1 && day <= 5) && (!vacation)) {
return "7:00";
}
return "off";
public String alarmClock(int day, boolean vacation) {
if (((day==0)||(day==6))&&(!vacation)){
return "10:00";
}
else if (((day!=0)||(day!=6))&&(!vacation)){
return "7:00";
}
else if (((day==0)||(day==6))&&(vacation)){
return "off";
}
else{
return "10:00";
}
}