Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need an example of a Boolean expression in Java that will only accept numbers

ID: 3558539 • Letter: I

Question

I need an example of a Boolean expression in Java that will only accept numbers between 10 and 100 and if a letter or symbol is provided, a response "you have entered a non-digit" will display.

(This is part of a homework assignment that has four exceptions and I tried creating another exception for non digits, but I couldn't get the response above to display properly. So I thought I could add a Boolean expression to make only numbers true. I was just going to add the Boolean to the main program I will run. If anyone has a better idea on how to add this, I would greatly appreciate it)

With help from another Chegger this is the code I have thus far:

[Code]

import java.util.Scanner;

import java.util.InputMismatchException;

public class Program5 {
public static void main(String args[]) throws Exception
{
System.out.println("Enter a number between 10 to 100");
Scanner sc = new Scanner(System.in);
try
{
int number = sc.nextInt();

Verify v = new Verify(10,100);
v.Validate(number);
}
catch(InputMismatchException e)
{
throw new BadDataException("BadDataException");
}
}
}

import java.util.Scanner;
import java.util.InputMismatchException;


class BadDataException extends Exception
{

BadDataException(String str) { super(str); }

public String toString() {
return "BadDataException"; } }

//NumberHighException - Exception class that is thrown when number is too large
class NumberHighException extends Exception {
public NumberHighException(String message) {
super(message);
}
public String toString() {
return "NumberHighException: number > 100"; }
}

class NumberLowException extends Exception {
public NumberLowException(String message) {
super(message);
}
public String toString() {
return "NumberLowException: number < 10"; }
}

class NumberNegativeException extends NumberLowException
{
public NumberNegativeException(String message) {
super(message);
}
public String toString() {
return "NumberNegativeException: number < 0"; }
}

class Verify
{
int min,max;
Verify(int min,int max)
{
this.min = min;
this.max = max;

}
public void Validate(int number) throws Exception
{

if(number > 0 && number < min)
{
throw new NumberLowException("NumberLowException: number < 10");
}

if(number > max)
{
throw new NumberHighException("NumberHighException: number > 100");
}

if(number < 0)
{
throw new NumberNegativeException("NumberNegativeException: number < 0");
}

if(number >= min && number <= max)
{
System.out.println("Successfull," + number + " is within range");
}

}
}

[CODE]

Explanation / Answer

3) If you want only main() and that dont want to throw any User Defined Exceptions( As Your Requirement)

import java.util.Scanner;
import java.util.InputMismatchException;

public class Driver11 {
public static void main(String args[]) throws Exception
{
  
try
{

boolean result = true;
int number = 0;
while(result)
{
System.out.println("Enter number b/w 10 to 100");
Scanner sc = new Scanner(System.in);
number = sc.nextInt();
if(number > 10 && number < 100)
{

System.out.println("Entered Required Number");
result = false;
break;
}
}


}
catch(InputMismatchException e)
{
System.out.println("You entered a Non-Digit");
}
  
}
}

Output: Prints All Requirements As you want

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote