• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Jj Roberts
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Himai Minh
  • Carey Brown
  • salvin francis

help with user input

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm in the process of writing a small battleship program.
The battleships are randomly mapped out along an index of 20.
I'm required to create a cheat sheet that displays a map of the battleship coordinates if the user enters 42 but I get an index out of bounds error.
I'm not allowed to use a try catch method to deal with this issue.  
Can anyone help me figure out how to enter a number larger than the array size to display the cheat sheet?

 
Bartender
Posts: 7431
66
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You'll need to test the input for '2' and '42' and take appropriate actions. ELSE enter the for() loop.
 
Kory Perry
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've tried using an if statement that checks to see if the user enters '42' and if they did then it would display the cheat sheet.  However I still get a array exception error.  Is there another way of testing that will bypass the exception?
 
Marshal
Posts: 71047
291
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Why have you marked everything static? There are sometimes good reasons to make something static, but because it won' compile otherwise is a bad reason. The correct souiton is to get all that code out of the main() method.
 
Marshal
Posts: 26128
77
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Kory Perry wrote:I've tried using an if statement that checks to see if the user enters '42' and if they did then it would display the cheat sheet.  However I still get a array exception error.  Is there another way of testing that will bypass the exception?



So far you don't even know if the if-statement you wrote affects that or not. (At least we don't.) So you need to investigate the exception and find out what's causing it.

Look at the stack trace, it has useful information like what line of code threw the exception.
 
Rancher
Posts: 3968
36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where is the code that checks for user input of 42?
 
I have always wanted to have a neighbor just like you - Fred Rogers. Tiny ad:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic