Help in Making something false in Java while keeping it as an int, not a boolean

Hi. So I was doing something as an assignment. I need to make something false without converting to a boolean, or vice versa. I try changing it between each of them, as I was suggested to do. I Each time I change it to the way it wants me to change it, it goes to the other thing and says it is wrong

I know I said before that I didn't want direct answers, cause I need to learn this on my own as much as possible, but I need the answer. Most of the time, yeah, just hints and suggestions, but I can't think of a way around this.

Here is my code for you:

package Lesson;

import java.util.Scanner;

public class RectanglePrinter {

	public static void main(String[] args) {

		//Create a Scanner
		Scanner input = new Scanner(System.in); 
		
		//Prompt the user to enter a height
		System.out.println("Please enter a height. No Fractions or Decimals: "); 
		int height = input.nextInt();
		boolean fraction = false;
		boolean decimal = false;
		if (height = fraction || decimal) {
			System.out.println("That is not a valid number. Please enter a valid numbers without fractions or decimals.");
			
		}
		//Prompt the user to enter a width
		System.out.println("Please enter a width. No Fractions or Decimals"); 
		int width = input.nextInt();
	}

}

I can't think of a way around without trying to say something completely different. How do I fix this issue?

Looks like that throws exceptions if an illegal value is entered.
https://www.javatpoint.com/post/java-scanner-nextint-method#:~:text=The%20nextInt()%20method%20of,the%20input%20as%20an%20int.

Looks like there’s a hasNextInt in there which could be leveraged or you could use a try/catch

Well no, you are trying to compare a value in an if statement. I think. At which point you would just check the falsy value: if(height != 0)

Well, i’m not sure what the question is, so I dont know I can give a direct answer.

That said, right now what your code does is read an Int from the scanner, and then throws that value away, assigns false to height, and then asks for a width.

It’s worth saying that you are reading nextInt from the scanner, at which point… you know it’s not a fraction or a decimal, because it’s an Int. As Dave points out, if it’s not an int, the nextInt function would have thrown a InputMismatchException. So i’m not sure what the rest of the code is meant to be doing?

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.