how to control the value of an input?

Hi guys,

The case is: prompts an input for user to enter an integer value that is must be greater than 0 and of course is not an alphabet.

What I have done: I could use Handling Exceptions to control the value of the input not to be an alphabet or be greater than 0 but seperately. But the problem is I do not know how to combine them.

Anyone can tell me?

Thx

Message was edited by:

Emotions

[436 byte] By [Emotionsa] at [2007-11-27 6:36:18]
# 1
Why combine? Leave it in two steps. Integer.parseInt() (mind the value range) and a simple "if" on the result...
CeciNEstPasUnProgrammeura at 2007-7-12 18:03:50 > top of Java-index,Java Essentials,Java Programming...
# 2
rethrow as something else and catch that, or (better) have a method that prints/shows the error message and call that from both catch blocks.
jwentinga at 2007-7-12 18:03:50 > top of Java-index,Java Essentials,Java Programming...
# 3

If you have a JTextField you can add an actionListener and then check in actionperformed the value. If not valid throw some Exception.

class MyClass implements ActionListener {

JTextField field;

public MyClass(){

//.... your code

field = new JTextField();

field.addActionListener(this);

//your code

}//end of ctor

public void actionPerformed(ActionEvent event){

if( event.getSource() == field ){

//do some validation

if( notValid ){

throw new Exception("invalid value for field");

}

}//end of class

ps: Make sure to subclass your own exception, catch it an display some validation message

check out the java tutorials on swing

http://java.sun.com/docs/books/tutorial/uiswing/index.html

and events (part of swing tutorials)

http://java.sun.com/docs/books/tutorial/uiswing/events/index.html

cappelleha at 2007-7-12 18:03:50 > top of Java-index,Java Essentials,Java Programming...
# 4
Things are a bit different :if an invalid message appears then the InputDialog must be redisplayed.
Emotionsa at 2007-7-12 18:03:50 > top of Java-index,Java Essentials,Java Programming...
# 5

Here are my codes, but I dont know where I can put a condition "if" to check whether an integer is greater than 0 or not.

import javax.swing.*;

public class Try

{

public static void main (String[] args)

{

boolean invalidNumber=true;

while(invalidNumber)

{

String input=JOptionPane.showInputDialog("Enter an integer");

try

{

int userInput=Integer.parseInt(input);

invalidNumber=false;

}

catch (NumberFormatException e)

{

JOptionPane.showMessageDialog(null,"Invalid input");

}

}

}

}

Emotionsa at 2007-7-12 18:03:50 > top of Java-index,Java Essentials,Java Programming...
# 6

I just get rid of the boolean instead of int j=1; while (j>0)

mport javax.swing.*;

public class Try

{

public static void main (String[] args)

{

int j=1;

while (j>0)

{

String input=JOptionPane.showInputDialog("Enter an integer");

try

{

int num=Integer.parseInt(input);

if (num>0)

{

JOptionPane.showMessageDialog(null,num);

break;

}

else

{

JOptionPane.showMessageDialog(null,"Invalid number");

}

}

catch(NumberFormatException e)

{

JOptionPane.showMessageDialog(null,"Invalid number");

}

}

}

}

Emotionsa at 2007-7-12 18:03:50 > top of Java-index,Java Essentials,Java Programming...