Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7981749
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T10:28:16+00:00 2026-06-04T10:28:16+00:00

My assignment says to throw a try and catch for this: If the user

  • 0

My assignment says to throw a try and catch for this:

  • If the user does not enter at least 3 tokens
  • If the user does not enter valid numbers for tokens one and three.

I tried researching it and I can’t seem to find the exception that would go by this.
Now my exception seems to work for the two conditionals above, but my instructor wishes for me to have two catches, not one.

I want to be able to crash this program so I can finally get another throw and catch or modify this program so that the exception would catch one thing specifically (like only if the user does not enter at least 3 tokens).

If I try do either of the things above to crash this program, by the way, it will always give me my error message so it clearly seems to cover the two things I have above.

My exception covers the two things I need to catch, but I need two catches, not one.

import java.util.NoSuchElementException;
import java.util.Scanner;
import java.util.StringTokenizer;
public class Driver {
    public static void main (String[] args) {
        Scanner sc = new Scanner(System.in);
        StringTokenizer st;
        String input, tok1, tok2, tok3;
        double dtok1, dtok3;
        boolean loop = true;

        //input
        System.out.println("Simple calculator program");
        System.out.println("This program will only except a math expression with");
        System.out.println("2 operands and 1 operator. Exceptable operators");
        System.out.println("are +, -, *, /, and %. For example, 2*3 or 6%4.");
        while (loop) {
            System.out.print("\nplease enter an expression or q to quit: ");
            try {
                input = sc.next();

                if (input.equalsIgnoreCase("q")) {
                    loop = false;
                } else {
                    //calculations
                    st = new StringTokenizer(input,"+-*/%",true);
                    tok1 = st.nextToken();
                    tok2 = st.nextToken();
                    tok3 = st.nextToken();
                    dtok1 = Double.parseDouble(tok1);
                    dtok3 = Double.parseDouble(tok3);
                    System.out.println(input + "=" + 
                    singleExpression(dtok1,dtok3,tok2));
                }
            } catch (Exception NoSuchElementException) {
                System.out.println ("Error! Please enter 2 operands and 1 operator!");
            }
        }
    }

    static double singleExpression (double num1, double num2, String operator) {
        double output = 0.0;
        if (operator.equals("+")) {
            output = num1 + num2;
        } else if (operator.equals("-")) {
            output = num1 - num2;
        } else if (operator.equals("*")) {
            output = num1 * num2;
        } else if (operator.equals("/")) {
            output = num1 / num2;
        } else if (operator.equals("%")) {
            output = num1 % num2;
        } else {
            output = 0;
        }
        return output;
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T10:28:17+00:00Added an answer on June 4, 2026 at 10:28 am

    I think that your using the exception class as the variable name, it should be Class var.

    Exception will caught anything (both number and few elements)

    try{
    ..
    }  catch (NoSuchElementException nse) {
        System.out.println ("Exception for the String Tokenizer");
    }catch (NumberFormatException nfe) {
        System.out.println ("Exception for the Number format");
    }
    catch (Exception otherException) {
        System.out.println ( "Something else.. " + otherException.getMessage() );
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This D specification table says that assignment overloading is only possible for structs, not
I have to do an assignment for my class and it says not to
Assignment of NSFileOwnerAccountName and NSFileGroupOwnerAccountName does not work? The directory created defaults to my
Manually Migrated to Super User: Random keyboard key assignment corruption in Windows XP This
This is an assignment that I've been given, it says that if an hash-set
When implementing move constructors and move assignment operators, one often writes code like this:
IntelliJ highlights the 'foo' variable in gray, and says assignment is not used. What
The assignment at my first year uni computing course says my program should read
My understanding of C# says (thanks to Jeff Richter & Jon Skeet) that assignment
Does an assignment of a real to a variable starting with I convert to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.