Java operator cannot be applied to boolean

Java operator cannot be applied to boolean

I was experimenting with an homework assignment(it called for
if-then-else statements, which I finished thanks to some help from this
forum), and was trying to see if I could accomplish it using boolean
operators. However I get the following error operator applied to boolean, int. Any idea why.
import java.util.*;

public class activities2
public static void main(String[] args)
int temp;
String act=null;
boolean swim = (temp > 85);
boolean tennis = ( 70 < temp boolean golf = (32 < temp boolean skiing = (0 < temp boolean dancing = (temp Scanner keyboard= new Scanner(System.in);
System.out.println(«In order to decide what is the best activity at
the present time, please enter the current temperature «);
temp = keyboard.nextInt();
if (swim)
act = «Swimming»;
else if (tennis)
act = «Tennis»;
else if (golf)
act = «Golf»;
else if (skiing)
act = «Skiing»;
else if (dancing)
act = «Dancing»;
System.out.println(«The recommened activity for the current
temperature is » + act);

Advertisements

ddk

For example: 70 < temp <= 85
The result of » 70 < temp " is boolean, i.e. true or false.
What’s the result of » boolean <= 85 " ?
It’s obvious that the expression is wrong.

machoextreme

So your saying the reason why the expression doesn’t work is that I’m
trying to evaluate a result, and not a varaible. So if I change the
boolean to boolean tennis =(70 < temp) || (tempaccurate?

Читайте также:  Ajax POST request with JQuery and PHP - Tutsmake

ddk

Roedy Green

For example: 70 < temp <= 85
The result of » 70 < temp " is boolean, i.e. true or false.
What’s the result of » boolean <= 85 " ?
It’s obvious that the expression is wrong.

In math you can say this, and in COBOL, but Java insists you spell out
such expressions longhand.

Источник

operator && cannot be applied to boolean,int — error

send pies

posted 12 years ago

  • Report post to moderator
  • So I am compling a program that is due tonight and I cannot get past these errors.

    I really need help on this one!

    I keep getting the following errors.

    Can anyone help me PLEASE. Desperate! I am having to use an online complier at the moment as I cannot access the remote one I am supposed to use for school.

    Thanking everyone in advance.

    send pies

    posted 12 years ago

  • Report post to moderator
  • this tells you that there is no method called nextint(). Right, change it to nextInt();
    When you have doubts about names, properties of a class look in oracle/sun docs online. This is just 1 error less, more remain.

    PS : what is this code trying to do ? please describe it for ease of understanding.

    send pies

    posted 12 years ago

  • Report post to moderator
  • Main.java:31: operator && cannot be applied to boolean,int
    invalidEntry = (direction != left && Left && right && Right && down && Down && up && Up)
    ^

    && can only take boolean values ie, anything that can be true or false. Nothing else is allowed. Why are you giving it ints ?
    M&&N means both M and N must evaluate to a boolean. eg. M can be (4>5) which is false. So the part after && is not evaluated. Since , && is a short-circuit operator(google it!).

    PS : I would suggest that you refer your textbook for such theory. Which one do you use ?

    send pies

    posted 12 years ago

  • Report post to moderator
  • Surely you need to initiliaze your left and right ints, or your direction will never equal them, and you probably want to use the logical or operator || instead of &&.

    You’re on a gravy train with biscuit wheels Roy.

    send pies

    posted 12 years ago

  • Report post to moderator
  • My task is to write a program that calculates and displays a position of a point inside a square. Position of a point in the plane is specified by its coordinates, two numbers: x and y, often written as a pair (x,y). The position of the point is repeatedly changed by the user who enters a direction for the point to move: up, down, left or right. Then one of the point’s coordinates, x or y, will increase or decrease by one unit. Initially the point lies at (0,0) which is the centre of a square whose sides are 8 units long. If the user enters ‘l’ (for left) or ‘r’ (for right) then the first coordinate, x, will decrement or increment, respectively. Similarly, the second coordinate, y, will increment or decrement if the user enters ‘u’ (for up) or ‘d’ (for down).

    I am an external student and am using «Starting out with Java», trust me it is not my best subject.

    I have done some more work and changed things around and now only have 8 errors but they are all on the same line!

    send pies

    posted 12 years ago

  • Report post to moderator
  • The error is on line 48 and basically revolves around the try

    Main.java:48: illegal character: \8220
    System.out.print(“Invalid entry, try again”);
    ^
    Main.java:48: ‘;’ expected
    System.out.print(“Invalid entry, try again”);
    ^
    Main.java:48: not a statement
    System.out.print(“Invalid entry, try again”);
    ^
    Main.java:48: ‘;’ expected
    System.out.print(“Invalid entry, try again”);
    ^
    Main.java:48: ‘ System.out.print(“Invalid entry, try again”);
    ^
    Main.java:48: illegal character: \8221
    System.out.print(“Invalid entry, try again”);
    ^
    Main.java:48: not a statement
    System.out.print(“Invalid entry, try again”);
    ^
    Main.java:48: ‘try’ without ‘catch’ or ‘finally’
    System.out.print(“Invalid entry, try again”);
    ^
    8 errors

    send pies

    posted 12 years ago

    • 1
  • Report post to moderator
  • The double quotes on that line don’t look like regular double quotes but rather like fancy ones a word processor might use. Java does not recognize those.

    Источник

    Operator && cannot be applied to boolean int

    I’m currently doing a model, however its occurred that I’m getting 1 error that which is saying that the && operator. I don’t fully understand why that is. If there is a solution to this that would be great, also with a good explanation why I have made this mistake Thanks

    import java.awt.image.*; import javax.imageio.*; import java.io.File; public class SlidingBlockModel < BufferedImage[][] board; // the two '[][]' allows a 2-D array to be created - each element is an image int NUM_COLUMNS; int NUM_ROWS; int empty_col; // holds the column index of the one empty square int empty_row; // holds the row index of the one empty square int control[][]; SlidingBlockModel(int input_num_cols,int input_num_rows,String image_filename) < NUM_COLUMNS = input_num_cols; NUM_ROWS = input_num_rows; BufferedImage original_image = null; board = new BufferedImage[NUM_COLUMNS][NUM_ROWS]; control = new int [NUM_COLUMNS][NUM_ROWS]; try < original_image = ImageIO.read(new File(image_filename)); >catch (Exception e) < System.out.println("Sorry - couldn't load in file " + image_filename); >int block_width = original_image.getWidth()/NUM_COLUMNS; int block_height = original_image.getHeight()/NUM_ROWS; for (int i = 0;i empty_row = NUM_ROWS-1; empty_col = NUM_COLUMNS-1; board[empty_col][empty_row] = null; // removes the sub-image that was there > boolean go(int col, int row) < if (((Math.abs(row-empty_row) == 1)&&(Math.abs(col-empty_col) != 1))||((Math.abs(row-empty_row)!= 1)&&(Math.abs(col-empty_col)))) < board[empty_col][empty_row] = board[col][row]; control[empty_col][empty_row] = control[col][row]; board[col][row] = null; empty_col = col; empty_row = row; return true; >else < return false; >> public BufferedImage get(int col, int row) < if(col >= 0 || row >= 0) < return board[col][row]; >else < return null; >> public void randomize() < java.util.Random r = new java.util.Random(); for (int x = 0; x > boolean complete() < boolean current = true; for (int i = 0; i < NUM_COLUMNS; i++) < for (int j = 0; j < NUM_ROWS; j++) < if (control[i][j] != ((i * NUM_ROWS)+j)) < current = false; >> > return current; > >
    • 2 Contributors
    • 1 Reply
    • 3K Views
    • 7 Hours Discussion Span
    • Latest Post 13 Years Ago Latest Post by BestJewSinceJC
    ((Math.abs(row-empty_row)!= 1) [B]&& (Math.abs(col-empty_col))[/B]

    Why would that work? You are treating the returned value of Math.abs(), which is an integer, as if it can be tested for a truth value. In Java, only boolean types can be tested for truth. So you need to put something like this

    ((Math.abs(row-empty_row)!= 1) [B]&& (Math.abs(col-empty_col) > 0)[/B]

    Expressions such as the one I posted above evaluate to either «true» or «false». In other words, Java looks at the returned value of Math.abs(), then looks at the operator, then looks at the value «0» on the other side of the operator and decides whether the expression is «true» or «false». In your code, you never compared the return value of Math.abs() to anything, and since the integer that it returned was not evaluated as «true» or «false» (since Java doesn’t evaluate ints as true or false), it generated an error.

    Источник

    operator & cannot be applied to boolean.java.lang.String

    send pies

    posted 17 years ago

  • Report post to moderator
  • Hello.
    It is pointless to say i’m new to java because anyperson asking for help in the beginner section is. Anyway, i am writing a program to tell what type of triangle the length of 3 sides make. I keep getting operator & cannot be applied to boolean.java.lang.String when i try to compile. here is my code

    /* Determines type of triangle based on length of sides.
    */
    import javax.swing.JOptionPane;
    public class Triangles
    public static void main(String[] args)
    //get the length of the sides
    String sideOne;
    sideOne = JOptionPane.showInputDialog(«Please enter the length of Side One»);
    String sideTwo;
    sideTwo = JOptionPane.showInputDialog(«Please enter the length of Side Two»);
    String sideThree;
    sideThree = JOptionPane.showInputDialog(«Please enter the lenght of Side Three»);
    // decide what type of triangle it is.
    if (sideOne == sideTwo && sideThree)
    JOptionPane.showMessageDialog(null, «The Triangle is Equilateral»);
    break;
    >
    else if (sideOne == sideTwo || sideThree)
    JOptionPane.showMessageDialog(null, «The Triangle is Isosceles»);
    //break;
    >
    else if ((sideOne != sideTwo || sideThree) && (sideTwo != sideOne || sideThree))
    //break;
    JOptionPane.showMessageDialog(null, «The Triangle is Scalene»);
    //break;
    >

    Thanks to anyone who helps me.

    Источник

    cannot be applied to a boolean.

    send pies

    posted 18 years ago

  • Report post to moderator
  • Hey all,
    I have tried everything i can think of to make this go.
    I am trying to hide the mines with this one line.(it’s in bold)
    I keep getting error messages that state || cannot be applied to a boolean.
    any help would be appreciated.
    thanks

    public class App public static void main(String[] args) Game game = new Game();
    game.play();
    >
    >

    class Game private Minefield m = new Minefield();
    private Player p = new Player();
    public void play() m.hideMines();
    m.enterGrid(0, 0);
    m.drawField();
    System.out.println(«Game over»);
    >
    >

    class Minefield private char[] field = new char[4 * 4];
    public void hideMines() int mines = 0;
    do int loc = ((int)(Math.random() * 1000)) % (field.length — 1);
    if (loc != 0 && field[loc] != ‘M’) field[loc] = ‘M’;
    mines++;
    >
    > while (mines < 4);
    field[15] = ‘E’;
    >
    public boolean enterGrid(int pRow, int pCol) field[(pRow * 4) + pCol] = ‘P’;
    return false;
    >
    public void exitGrid(int pRow, int pCol) >

    public void drawField() for (int r = 0; r < 4; r++) for (int c = 0; c < 4; c++) ///THIS LINE RIGHT HERE ---> if (field[(r * 4) + c] ==0________________________________)

    class Player private int row;
    private int col;
    public void move() System.out.print(«(r)ight or (d)own: «);
    >
    public int getRow() return row;
    >
    public int getCol() return col;
    >
    >

    Источник

    Оцените статью