Hi, so I'm new with Java and the programming thing. Here's my problem. I need to do an errortrap without using a loop and creating a method solely for errortrapping so I figured i need to use try and catch statements but I'm not sure how. Basically my problem is that I need to get a user input to proceed to another method and to exit. Say for example, the program requires you to input 'e' to exit and 'p' to proceed. Anything else would be an error. Can anyone give me an example?

you've mentioned everything you need to do.
is it necessary you throw an exception, do you just have to show an error message, ...
be a bit more clear, and show what you've tried so far.

public void intro ()
{


try
{


int proc;
c.println ("This program will allow you to convert one amount to either teaspoons, tablespoons or cups. If you want to continue press 'p'. If you want to exit press 'x'");
proc = c.readChar ();
if (proc == 'p')
askData ();
if (proc == 'x')
goodbye ();
}
catch (Exception e)
{
c.println ("Sorry, that does not belong to one of the options. Please input 'p' to continue or 'x' to exit. ");
}

That's what I have so far... What I need it to do is that whenever there's an input other than 'p' and 'x' I need to ask the user to try again and just choose between the two options.

what is c?
why are you putting a char into an integer?
why do you think an Exception will be thrown if you enter another value than 'p' or 'x'?

Well, c is basically like system.out. Im using Ready to program java ide by holtsoft. and i'm not sure myself why i use int but it was the example i was given so i just used it because it works. C is the instance variable for my class

ah, ok .. can't say I've ever seen it, but it's propably somewhere declared in the rest of your code.

you must understand that integer and char is not the same, an int(eger) is a numeric value, where a char holds a character.

the code as you've written it, I assume it'll compile and run, but your input will be converted to a numeric value, which is not what I think you want.
anyway, your code will never throw an exception.
if you want an exception to be thrown when another value is entered, you can do the following:
and this is just an example, not an entire solution to your code

public void intro() throws InvalidCharException{
  char proc = readChar();
  if ( proc != 'x' && proc != 'p' )
      throw new InvalidCharException("You've entered an invalid value.");
  // ... rest of the code
}

you don't put the try{} catch{} block in there, but on the place where you call this method, also, you may have to create your own Exception class.

When I used InvalidCharException it says it wasn't found, does that mean that the Ready to Program Java IDE doesn't have that exception?

When I used InvalidCharException it says it wasn't found, does that mean that the Ready to Program Java IDE doesn't have that exception?

You either have to create your own InvalidCharException or just throw a regular Exception. The Exception class is sort of the super-class to all other exceptions, totally generic. Still don't really see the benefit of using exceptions here though, but that's up to you.

How do I error check a user input? And to ask for the user again to either input e or x if the user input something else without using a loop and making a class or method for it.

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.