java if nested loop [closed] - java

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 8 years ago.
Improve this question
It gives me following error: The operator == is undefined for the argument type(s)
boolean, int
Syntax error on tokens, delete these tokens
package javaproject;
public class NestedIFandIFandElse {
public static void main(String[] args) {
int vanus = 50;
if (vanus == 40) {
System.out.println("first if ");
} else {
System.out.print("first else");
if (vanus == 50 ∣∣ vanus == 20) {
System.out.println("second if");
} else {
System.out.println("second else");
}
}
}
}

You should change ∣∣ to ||. They look the same, but they aren't:
if (vanus == 50 || vanus == 20)

I'm not certain how you entered that symbol, but ∣∣ is not ||;
if (vanus == 50 || vanus == 20 ) { // <-- The || or works here.
I changed it as above, and it compiles here.

Related

error cannot convert int to boolean in the if else if statement in java [closed]

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 months ago.
Improve this question
package Fare;
import java.util.Scanner;
public class fare {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print(" Enter Class ");
System.out.println(" a. Senior b. Student c. Regular ");
System.out.println("Enter class : ");
int num = input.nextInt();
if (num = 1) {
double fare = 9 * 0.10;
System.out.println("Your fare will be " + fare + "▒. Thank you.");
}
else if (num = 2 ) {
double fare = 9 * 0.08;
System.out.println("Your fare will be " + fare + "▒. Thank you.");
}
else {
System.out.println("your fare will be 9▒. Thank you.");
}
}
}
Hi i can't seem to understand why there is an error in my if statement stating that it cannot convert int to boolean. I have already checked it and still can find any problem or mybe i just don't know.
You should change if(num=1) to if(num==1) and if else(num=2) to if else(num==2).

C to Java (binary numbers programm) [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 6 years ago.
Improve this question
I made this program on C,and I have no idea of Java (new-yοung developer).Can you help me to "translate" it on Java?
#include<stdio.h>
#include<stdlib.h>
#include<graphics.h>
void printbin(int num)
{
int binnum;
binnum=num%2;
num=num/2;
if (num!=0) printbin(num);
printf("%d",binnum);
return;
system("pause");
}
int main(void)
{
int posnumber,binnum;
printf("Give a number : ");
scanf("%d",&posnumber);
printbin(posnumber);
printf("\n");
return 0;
}
You can learn from this code, hope this can help you:
//here is comments in java
package help;
import java.util.Scanner;
//this is the way how you can all the libraries in java so
/*
#include<stdio.h>
#include<stdlib.h>
#include<graphics.h>
*/
//here name of your class
public class Help {
//here name of your methode
public static void printbin(int num) {
int binnum;
binnum = num % 2;
num = num / 2;
if (num != 0) {
printbin(num);
}
System.out.println(binnum);
}
//here is the main methode in java
public static void main(String args[]) {
int posnumber, binnum;
Scanner scan = new Scanner(System.in);
System.out.println("Give a number : ");
posnumber = scan.nextInt();
System.out.println(posnumber);
}
}
Good luck, and good start with java :)

Unreachable error [closed]

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 7 years ago.
Improve this question
I am trying to have the code print out the highest of the three grades; however, the if/else statement in the highest method is finding an error in return d. I have tried putting if(d>b && d>c) return d, and also else return d. But both times the program says it is unreachable. Can someone explain what I'm doing wrong? Thank you!
import java.util.Scanner;
public class Methods2 {
public static double average(double a){
double ave= a/3.0;
return ave;
}
public static double highest(double b, double c, double d){
if(b>c && b>d)
return b;
if(c>b && c>d);
return c;
return d;//unreachable code
}
public static void main(String[] args){
Scanner kb= new Scanner(System.in);
System.out.println("Enter your name.");
String name = kb.nextLine();
System.out.println("Enter your three grades.");
double b= kb.nextDouble();
double c= kb.nextDouble();
double d= kb.nextDouble();
double av= average(b+c+d);
System.out.println(av);
double high= highest(b,c,d);
System.out.println(high);
}
}
Yes. Because semicolon makes it an empty if body.
if(c>b && c>d);
return c;
should be
if(c>b && c>d)
return c;
or (the arguably better)
if(c>b && c>d) {
return c;
}
You could also use Math.max(double, double) to make this a one line method like
public static double highest(double b, double c, double d) {
return Math.max(d, Math.max(b, c));
}
because of the semicolon in the second if
if(c>b && c>d);
That terminates the conditional statement right there and return c; becomes a statement that will get executed regardless of condition, remove it

When I compile my code it says error '{' expected in the 4th line where I have my class, but I have a '{' there [closed]

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 8 years ago.
Improve this question
I am using java and having problem with my code. So for some reason when I compile my project on the 4th line it says error: '{' expected. resource says proj 2.java. Any help would be appreciated! thanks.
import java.io.*;
import java.util.*;
class proj 2 {
public static void main(String[] args)throws IOException {
BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in));
int a=0, b=0, c=0, d=0, f=0, totalNumber=0;
stars();
System.out.println("Enter a list of exam scores from 0-100 ");
System.out.println("use a negative one if you're done with your list:");
stars();
for(int grade=0;grade>=0;totalNumber++)
{
try
{
grade=Interger.parseInt(dataIn.readLine());
if(grade>=90 && grade<=100)
a++;
else if (Grade>=80 && grade<=89)
b++;
else if (grade>=70 && grade>=79)
c++;
else if (grade>=60 && grade<=69)
d++;
else if (grade>=0 && grade<=59)
f++;
else if (grade>100)
{
System.out.println("Invalid Grade");
totalNumber--;
}
}
catch(NumberFormatException e)
{
System.out.println("Invalid input. Please enter your score in number form.");
totalNumber--;
continue;
}
}
System.out.println("");
System.out.println("Total number of grades: " +(totalNumber-1));
System.out.println("A grades: " + a);
System.out.println("B grades: " + b);
System.out.println("C grades: " + c);
System.out.println("D grades: " + d);
System.out.println("F grades: " + f);
}
public static void stars()
{
System.out.println("**********************************************************************");
}
}
Change
class proj 2 {
to
class proj2 {
A class name can't have spaces.
Also change Interger to Integer and Grade to grade.

syntax error on else, delete this token error [closed]

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 8 years ago.
Improve this question
My if, else statement is giving me an error that is telling me to delete the word else. What should I do?
import java.util.Scanner;
public class Test5 {
public static void main(String[] args) {
double GradeAverage;
{
System.out.print("Welcome! Please enter in your mid-year grade average.");
}
Scanner kbdln = new Scanner(System.in);
{
GradeAverage = kbdln.nextDouble();
{
if (GradeAverage > 60 && GradeAverage <= 100);
{
System.out.print("You're passing your class!");
}
else if (GradeAverage > 0 && GradeAverage <= 60)
{System.out.print("Hook up with a smart classmate and STUDY!");}
}
}
}
}
There should be no semi-colon after the if statements.
The if statement should be as follows:
if(GradeAverage > 60 && GradeAverage <= 100)
First, I would get rid of all those { } you are using. It is way too much. Although it is correct to use them to declare a local namespace and absolutely necessary to use them with your if-else statements. But it is better not to use local namespaces if you are not absolutely sure what you are doing.
The second thing is you have a ; after your if statement. Which will be interpreted as an ; = empty statement. So your follow up else is completely alone – and that's why your IDE tells you to remove the else.
Here is a version of your code a little cleaned up :)
public class Test5 {
public static void main(String[] args) {
double GradeAverage;
System.out.print("Welcome! Please enter in your mid-year grade average.");
Scanner kbdln = new Scanner(System.in);
GradeAverage = kbdln.nextDouble();
if (GradeAverage > 60 && GradeAverage <= 100) {
System.out.print("You're passing your class!");
} else if (GradeAverage > 0 && GradeAverage <= 60) {
System.out.print("Hook up with a smart classmate and STUDY!");
}
}
}

Categories

Resources