Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 error handling
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
cool dude




PostPosted: Tue Nov 21, 2006 7:33 pm   Post subject: error handling

i can catch errors but the problem i'm having is when i catch them the program just finishes. i tried putting all my code in a loop however once the error is caught it will repeat the very first question in the loop because it will just restart the loop. how can i get it to keep reasking for a valid input?
Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: Tue Nov 21, 2006 10:11 pm   Post subject: (No subject)

Show us the code you have.
cool dude




PostPosted: Wed Nov 22, 2006 3:01 pm   Post subject: (No subject)

wtd wrote:
Show us the code you have.


well the code is kinda long so i don't think anyone wants to be looking through all the code. so i'll make a very small sample program right now so you guys can show me how this can be done so then i can implement the same thing into my program.
cool dude




PostPosted: Wed Nov 22, 2006 3:07 pm   Post subject: (No subject)

Here is the sample program i just finished making. This is not my program its just an example to learn how to keep reasking the same question until valid input is received.

code:

import java.io.*;

public class Sample
{
   public static void main(String[] args)
   {
             int num1, num2;
               try
               {
                      BufferedReader myInput = new BufferedReader(new InputStreamReader(System.in));
                      //asks for input from user - 2 numbers and operation
                System.out.println("Enter first number");
                num1 = Integer.parseInt(myInput.readLine());
                System.out.println("Enter second number");
                num2 = Integer.parseInt(myInput.readLine());
               }
               catch (NumberFormatException nfe) {
                System.out.println("can't convert: ");
        }
        catch (IOException f) {
                System.out.println("error reading input");
        }         
   }
}
HellblazerX




PostPosted: Wed Nov 22, 2006 3:35 pm   Post subject: (No subject)

Why not use a flag? When your program gets the data it wants, just use the flag to exit out of the loop. Otherwise, just stay in the loop if the wrong data is given.
wtd




PostPosted: Wed Nov 22, 2006 3:53 pm   Post subject: (No subject)

My suggestion would be to use "break" and "continue."

If break is encountered in a loop, it will cause control flow to exit that loop.

If continue is encountered, it will cause the loop to skip straight to the next iteration.

If you have a parsing operation, and then directly afterwards a "break" statement, and the whole thing is wrapped up in a try...catch construct, then if an exception is encountered parsing the string, control flow will skip straight to the appropriate "catch" block, bypassing the "break" entirely.

A further note: You have essentially the same operation repeated twice. You want to prompt the user repeatedly for a number until the input they give you is a valid number. You also wish to display an error message when bad input is given.

This is best factored out into a method which you could call twice.
cool dude




PostPosted: Wed Nov 22, 2006 5:09 pm   Post subject: (No subject)

HellblazerX wrote:
Why not use a flag? When your program gets the data it wants, just use the flag to exit out of the loop. Otherwise, just stay in the loop if the wrong data is given.


that is exactly wat i did but the problem with that is that when the wrong data is given it will stay in the loop but it will cycle through the start of the loop so it will ask the very first question. however what if the wrong input is asked on the second question. then it will repeat the first question which won't make sense.

wtd i'm a little confused on what you said. care to elaborate

Thanks
cool dude




PostPosted: Wed Nov 22, 2006 5:40 pm   Post subject: (No subject)

i just checked out your tutorial. Thank you for making one! i do appreciate it. i am kinda confused on things you did. for example:

Quote:

class UtilityClass {
public static int getInt(Scanner input, String prompt) {
int result = 0;

while (true) {
try {
System.out.print(prompt + " ");
result = input.nextInt();
break;
}
catch (InputMismatchException e) {
}
}

return result;
}

public static int getInt(Scanner input, String prompt, String errorMessage) {
int result = 0;

while (true) {
try {
System.out.print(prompt + " ");
result = input.nextInt();
break;
}
catch (InputMismatchException e) {
System.out.println(errorMessage);
}
}

return result;
}
}


These are constructors i'm assuming. however i don't understand why you make a loop and then you break to get out of the loop?
Also you made two constructors one which will go into if there is no error and one will go into if there is an error (i think). why can't you just assume there is an error and just make one constructor. if no error is found then the program will just continue so why is there two constructors?

Also does this mean i will have to make a class with constructors and all that work just to catch an error and reask the question? i don't really want to sound lazy but thats a lot of work just to catch an error which only motivates me to throw exceptions. Is there another way?[/code]
Sponsor
Sponsor
Sponsor
sponsor
cool dude




PostPosted: Wed Nov 22, 2006 6:03 pm   Post subject: (No subject)

wtd wrote:
My suggestion would be to use "break" and "continue."

If break is encountered in a loop, it will cause control flow to exit that loop.

If continue is encountered, it will cause the loop to skip straight to the next iteration.

If you have a parsing operation, and then directly afterwards a "break" statement, and the whole thing is wrapped up in a try...catch construct, then if an exception is encountered parsing the string, control flow will skip straight to the appropriate "catch" block, bypassing the "break" entirely.

A further note: You have essentially the same operation repeated twice. You want to prompt the user repeatedly for a number until the input they give you is a valid number. You also wish to display an error message when bad input is given.

This is best factored out into a method which you could call twice.


by what i understood you meant something like this:
code:

import java.io.*;

public class Sample
{
   public static void main(String[] args)
   {
       int num1, num2;
       while(true)
       {
               try
               {
                  BufferedReader myInput = new BufferedReader(new InputStreamReader(System.in));
                  //asks for input from user - 2 numbers and operation
                   System.out.println("Enter first number");
                   num1 = Integer.parseInt(myInput.readLine());
                   System.out.println("Enter second number");
                   num2 = Integer.parseInt(myInput.readLine());
                   break;
               }
               catch (NumberFormatException nfe) {
                   System.out.println("can't convert: ");
                   continue;
               }
               catch (IOException f) {
                 System.out.println("error reading input");
                 continue;
               }       
       }
   }
}


This will work if the error is on the first input however, if the error is on the second input then it will reask the first question and then the second question.
wtd




PostPosted: Thu Nov 23, 2006 12:34 am   Post subject: (No subject)

cool dude wrote:
These are constructors i'm assuming.


In UtilityClass, they are not constructors. They are simply static methods. You would call them as "UtilityClass.getInt(...)".
wtd




PostPosted: Thu Nov 23, 2006 12:35 am   Post subject: (No subject)

cool dude wrote:
wtd wrote:
My suggestion would be to use "break" and "continue."

If break is encountered in a loop, it will cause control flow to exit that loop.

If continue is encountered, it will cause the loop to skip straight to the next iteration.

If you have a parsing operation, and then directly afterwards a "break" statement, and the whole thing is wrapped up in a try...catch construct, then if an exception is encountered parsing the string, control flow will skip straight to the appropriate "catch" block, bypassing the "break" entirely.

A further note: You have essentially the same operation repeated twice. You want to prompt the user repeatedly for a number until the input they give you is a valid number. You also wish to display an error message when bad input is given.

This is best factored out into a method which you could call twice.


by what i understood you meant something like this:
code:

import java.io.*;

public class Sample
{
   public static void main(String[] args)
   {
       int num1, num2;
       while(true)
       {
               try
               {
                  BufferedReader myInput = new BufferedReader(new InputStreamReader(System.in));
                  //asks for input from user - 2 numbers and operation
                   System.out.println("Enter first number");
                   num1 = Integer.parseInt(myInput.readLine());
                   System.out.println("Enter second number");
                   num2 = Integer.parseInt(myInput.readLine());
                   break;
               }
               catch (NumberFormatException nfe) {
                   System.out.println("can't convert: ");
                   continue;
               }
               catch (IOException f) {
                 System.out.println("error reading input");
                 continue;
               }       
       }
   }
}


This will work if the error is on the first input however, if the error is on the second input then it will reask the first question and then the second question.


This is best factored out into a method which you could call twice.
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 11 Posts ]
Jump to:   


Style:  
Search: