• 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
  • Liutauras Vilda
  • Bear Bibeault
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Piet Souris
  • salvin francis
  • Stephan van Hulst
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Jj Roberts

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: 7440
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: 71103
292
  • 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: 26137
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?
 
"Don't believe every tiny ad you see on the internet. But this one is rock solid." - George Washington
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic