codetoad.com
  ASP Shopping CartForum & BBS
  - all for $20 from CodeToad Plus!
  
  Home || ASP | ASP.Net | C++/C# | DHTML | HTML | Java | Javascript | Perl | VB | XML || CodeToad Plus! || Forums || RAM 
Search Site:
Search Forums:
  How can I avoid accepting a null value from user input?  EvilObsidian at 21:50 on Monday, May 01, 2006
 

Hello and thanks for taking a look at my problem.

I am somewhat a beginner and I'm working on a program for fun that builds a maze and lets the user navigate through it by entering an integer to indicate which direction they want to move (through a 2D array).

My problem is this:

In an input box I have it printing what can be seen so far of the maze and gives options that look like this:

Which direction would you like to go?
8 = north
6 = east
4 = west
2 = south

I get the input from a:

String input = JOptionPane.showInputDialog( chooseDir );
choice = Integer.parseInt( input );

When the user is playing through the maze and accidently hits enter or clicks OK with no input in the space, the program crashes. I beleive it's because it is trying to compare an integer and null value, but I can't figure out how to avoid it.

Can someone help me?
Thanks a million in advance :-)

Evil


  Re: How can I avoid accepting a null value from user input?  crwood at 22:53 on Monday, May 01, 2006
 


import javax.swing.JOptionPane;

public class NullTest
{
public static void main(String[] args)
{
int value = -1;
boolean tryItAgain = true;
while(tryItAgain)
{
String retVal = JOptionPane.showInputDialog(null, "enter integer");
if(retVal == null || retVal.equals(""))
continue;
try
{
value = Integer.parseInt(retVal);
}
catch(NumberFormatException nfe)
{
continue;
}
tryItAgain = false;
}
JOptionPane.showMessageDialog(null, "you entered " + value);
}
}


  Re: How can I avoid accepting a null value from user input?  EvilObsidian at 23:07 on Monday, May 01, 2006
 

Thanks crwood!

Works great, I never thought of comparing the string to null. You've been a great help :-)

Evil








CodeToad Experts

Can't find the answer?
Our Site experts are answering questions for free in the CodeToad forums
//








Recent Forum Threads
•  Beginners question on building Bento4 SDK
•  Scrolling Images
•  Re: Please can someone show me how to make a java box pop upand display personal information!
•  Does anyone know to create or implement a diary into Java??
•  jslider - paint track
•  Re: convert minutes into hours and minutes
•  File handle problem
•  Link Capture, help needed
•  Re: java app auto web update..


Recent Articles
ASP GetTempName
Decode and Encode UTF-8
ASP GetFile
ASP FolderExists
ASP FileExists
ASP OpenTextFile
ASP FilesystemObject
ASP CreateFolder
ASP CreateTextFile
Javascript Get Selected Text


© Copyright codetoad.com 2001-2007