Can someone help me with error trapping. For instance, if I asked the user to input only a numeric type, 0-9. Then how would I go about creating an error message if the user inputs "hey." I know that I could easily use the Try, Catch statements but I want to learn the other ways.

Recommended Answers

All 6 Replies

// get input up here into char variable 'userInput'
if (Character.isDigit(userInput)){
//do something
} else System.out.println("That wasn't 0-9...");

ok So I ran a little test program and it isnt working for me

import javax.swing.JOptionPane;

public class Test
{
	public static void main(String[] args)
	{
		String input;
		int hours;
		
		input = JOptionPane.showInputDialog("Hours?");
		hours = Integer.parseInt(input);
		

		if (Character.isDigit(input))
		{
			JOptionPane.showMessageDialog(null, "hey");
				
		}
			
		 else System.out.println("That wasn't 0-9...");
		 
		 }
		 
	}

It says it cannot find the smybol and is pointing to Character.isDigit

"input" as defined in your code is a String. The Character.isDigit() method takes a char as an argument, as my comment above the code I gave you earlier suggests.. not a String. Furthermore even if you did manage to read in the first character of what the user entered using something like the String class's charAt method, you would still not get the right results unless you made sure the user only entered one character. Try this:

import javax.swing.JOptionPane;

public class Test
{
	public static void main(String[] args)
	{
		String input;
		char hours;
		
		input = JOptionPane.showInputDialog("Hours?");
		hours = input.charAt(0);
		
		if (Character.isDigit(hours) && input.length() == 1)
		{
			JOptionPane.showMessageDialog(null, "hey");
				
		} else System.out.println("That wasn't 0-9...");
		 
	}
}

Instead of using Character.isDigit( char ), you could use the matches() method from the String class. This would save having to test the length of the data and having to convert it to a char.

import javax.swing.JOptionPane;


public class Test {
  public static void main(String[] args) 	{
		
    String input;
		
    input = JOptionPane.showInputDialog("Hours?");		

    if ( input.matches( "[0-9]" ) ) {
     JOptionPane.showMessageDialog(null,"hey");				
    }					
    else JOptionPane.showMessageDialog(null, "That wasn't 0-9...");
		 
  }
		 
}

The matches() method uses regular expressions.

So what if I wanted to allow negative numbers? but no characters?

I believe the regular expression for that would be: [0-9]|-[0-9]. A more readable test would be:

if ( input.matches( "[0-9]"  ) || input.matches( "-[0-9]" ) ) {
  JOptionPane.showMessageDialog(null, "Correct input.");				
} else JOptionPane.showMessageDialog(null, "Incorrect input!");

I am no expert in regex though, and there are better ways to do something like this than regex, but it was the first thing that came to mind!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.