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 6713147
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:20:42+00:00 2026-05-26T08:20:42+00:00

I am trying to write an Assertion to check if the size the user

  • 0

I am trying to write an Assertion to check if the size the user gives is a positive value, if not then make it positive, this statement is inside the class constructor which takes the size value and then makes an array[size]. I have written the below code which i believe to be correct.

    public Grid(int size) {


    try{
        assert size > 0 ;
    }
    catch(AssertionError e){
        size = Math.abs(size);
    }

    setLayout(new GridLayout(size, size));
    grid = new JButton[size][size];
}

Though I never seems to evaluate my assertion and continues the program then causes the NegativeArraySize error( which i am trying to avoid)

I also tried just

assert size>0;

And the program fails to stop for negative values..

I have had a few problems with running java on mac recently, so i don’t know if my code is right or if it is just one of those odd mac quirks!! and should just use

size=Math.abs(size);

Thanks Sam,

  • 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-05-26T08:20:42+00:00Added an answer on May 26, 2026 at 8:20 am

    You need to run your program with the -ea switch (enable assertions), otherwise no assert instructions will be run by the JVM at all. Depending on asserts is a little dangerous. I suggest you do something like this:

    public Grid(int size) {
        size = Math.max(0, size) 
        setLayout(new GridLayout(size, size));
        grid = new JButton[size][size];
    }
    

    Or even like this:

    public Grid(int size) {
        if(size < 0) {
            throw new IllegalArgumentException("cannot create a grid with a negative size");
        } 
        setLayout(new GridLayout(size, size));
        grid = new JButton[size][size];
    }
    

    The second suggestion has the benefit of showing you potential programming errors in other parts of your code, whereas the first one silently ignores them. This depends on your use case.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a blog post which includes a code segment inside a
Trying to write this small program to help me in my Stats class, everything
I'm trying to write code like this: assert_throws(:ExtractionFailed) { unit.extract_from('5 x 2005')} ExtractionFailed is
I want to write a few unit tests that do not make any changes
i trying to write a comment in each PHP file, then the comment will
I'm trying write a java program to send live microphone data over UDP, then
I'm trying write a code that can the set property value through the lambda
I'm trying write a Ruby script that checks if user credentials are valid using
I'm trying write a query to find records which don't have a matching record
Trying to write a PowerShell cmdlet that will mute the sound at start, unless

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.