0

Im a beginner at java programming and this is from our intermediate java programming class. How do i add a validation that does not accept alphabets and restarts only the selected operation??`

Here is my code:

package Calcu;
import java.util.Scanner;

//Assignment
//1.  What is Abstraction in OOP
//2. Give an example of program where abstraction is presented

class Add{
    void AddM(double num) {
        System.out.print("Enter Number for Addition: ");
    }
}

class Sub{
    void SubM(double num) {
        System.out.print("Enter Number for Subtraction: ");
    }
}

class Mult{
    void MultM(double num) {
        System.out.print("Enter Number for Multiplication: ");
    }
}

class Div{
    void DivM(double num) {
        System.out.print("Enter Number for Division: ");
    }
}

public class Main {
    static Scanner s = new Scanner(System.in);
    public static void main(String[] args) {
        while(true){
            System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~");
            System.out.println("~~Calculator~~");
            System.out.println(" ");
            System.out.println("Select Operation: ");
            System.out.println("1 for Addition");
            System.out.println("2 for Subtraction");
            System.out.println("3 for Multiplication");
            System.out.println("4 for Division");
            System.out.println("Type s to terminate program");
            System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~");
            String in = s.next();

            try {
                if (in.equals("1")) {
                    Add calcuA = new Add();
                    calcuA.AddM(0);
                    double a = s.nextDouble();
                    calcuA.AddM(0);
                    double b = s.nextDouble();
                    System.out.println("-->The Sum is: " + (a + b) + "<--");
                    System.out.println(" ");
                }


                else if (in.equals("2")) {
                    Sub calcuS = new Sub();
                    calcuS.SubM(0);
                    double c = s.nextDouble();
                    calcuS.SubM(0);
                    double d = s.nextDouble();
                    System.out.println("-->The Difference is: " + (c - d) + "<--");
                    System.out.println(" ");

                }

                else if (in.equals("3")){
                    Mult calcuM = new Mult();
                    calcuM.MultM(0);
                    double e = s.nextDouble();
                    calcuM.MultM(0);
                    double f = s.nextDouble();
                    System.out.println("-->The Product is: " + (e * f) + "<--");
                    System.out.println(" ");
                }

                else if(in.equals("4")) {
                    Div calcuD = new Div();
                    calcuD.DivM(0);
                    double g = s.nextDouble();
                    calcuD.DivM(0);
                    double f = s.nextDouble();
                    System.out.println("-->The Quotient is: " + (g / f) + "<--");
                    System.out.println(" ");
                }
                else if(in.equals("s")) {
                    System.out.println("Thank you for using this calculator. Console Terminating...");
                    break;
                }

                else {
                    System.out.println("Choose only from the numbers represented above...");
                }
            }catch(Exception letters){
                System.out.println(" ");
                System.out.println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
                System.out.println("Only numbers are allowed! Try again!!");
                
            }
        }
    }
}

I've tried using try catch but the results prints out the whole program and not the "enter number for the selected operation(addition, subtraction, division, multiplication)" ive tried changing the conditions of while, adding other validation and moving them from places but i couldn't achieve the results i was looking.

~~Calculator~~
 
Select Operation: 
1 for Addition
2 for Subtraction
3 for Multiplication
4 for Division

2 Enter Number for Subtraction: jk

!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Only numbers are allowed! Try again!!

~~Calculator~~
 
Select Operation: 
1 for Addition
2 for Subtraction
3 for Multiplication
4 for Division

Choose only from the numbers represented above...

~~Calculator~~
 
Select Operation: 
1 for Addition
2 for Subtraction
3 for Multiplication
4 for Division
apophis
  • 1
  • 1
  • You can't stop a user entering letters. Read the entire line and check if it's a digit or not. If it's not, ask again. – Bohemian Mar 29 '23 at 10:22

0 Answers0