• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Try and Catch statement not working for negative numbers

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This method checks if there is not anything besides a number in the showInputDialog called userInput. It works but when I enter a negative number like -1 it does not go to the catch statement. Shouldn't it hit the catch statement because Exception e covers it?

Here's the code.



 
Saloon Keeper
Posts: 10732
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You've put the user's input into "iUserInput" which is perfectly happy with negative numbers. Nowhere are you testing to see if it's a negative number, and nowhere are you throwing an exception that would be caught in the "catch" block.
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Steven Villarreal wrote:This method checks if there is not anything besides a number in the showInputDialog called userInput. It works but when I enter a negative number like -1  . . .

But you are not testing for negative numbers, nor throwing any exceptions. Make the option pane say, “must be in range 3...14,” and test for negative numbers.

Why are you using option panes for keybpard input? That is a very old‑fashioned sort of programming, which has been largely superseded by the use of Scanners.
Don't throw an exception and catch it in the same method. That is simply an inefficient implementation of if‑else. Do this, without using exceptions at all:-Formatting: make sure there is a space before each { and after if.
Don't prefix variable names with anything to denote their type of scope. Not iUserInput please, but something like sizeInput. Maybe requestedSize would be better still.
 
Carey Brown
Saloon Keeper
Posts: 10732
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Do this, without using exceptions at all:

You still need to catch a parse exception.
 
Steven Villarreal
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the feedback! All I am trying to do is popup a window that says "Enter the size of the board." Then if they enter something between 3 - 14 then they are good then a grid pops up with what ever value they typed in. Anything that is either not a number or does not fall into the specified range then another window will pop up telling them to enter a number in between 3 and 14. They can either press cancel to gui or type something in that is between 3 and 14 if they choose not to type a number or anything in that range and press ok, they will be prompted with the same window telling them to enter a number within that specified range.
 
Campbell Ritchie
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You don't need an option pane for input on a GUI; you simply need a text field to enter the size in. Give it an accompanying label, “Size: range 3...14 please,” and parse the size from that field.
You can still use a Scanner on such a field:-Put the response to no int input in line 470 and the response to out of range in line 468. If you can't understand line 464, ask again.
You may wish to use an option pane to signal the error.
 
Campbell Ritchie
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Another way you could write that listener is like this:-
 
Carey Brown
Saloon Keeper
Posts: 10732
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Steven Villarreal wrote:...then they are good then a grid pops up with what ever value they typed in.

This seems like it would imply that you are writing a GUI app. Is it a GUI app or are you just using OptionPane to get specific user inputs?
 
Steven Villarreal
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What I am trying to do is display a tic tac toe board but before it displays it will ask how big the board you want it to be. It's just a basic gui that's all.
 
Carey Brown
Saloon Keeper
Posts: 10732
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Steven Villarreal wrote:What I am trying to do is display a tic tac toe board but before it displays it will ask how big the board you want it to be. It's just a basic gui that's all.

Do you have a complete program yet even though it doesn't behave the way you want? Is it a Swing application?

"Display" could just mean an ASCII grid drawn on the console with System.print().
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Steven Villarreal wrote:What I am trying to do is display a tic tac toe board but before it displays it will ask how big the board you want it to be. It's just a basic gui that's all.



If you don't have a working program yet, why don't you just start simple with a regular 3x3 grid and console input? Also, assume "smart" users first and save the input checking for later once you have a program that works for all good input. Programming is hard enough for a beginner, don't make things harder on yourself by adding complexity before you've even tackled the simple.
 
It's fun to be me, and still legal in 9 states! Wanna see my tiny ad?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic