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

Username:   Password: 
 RegisterRegister   
 Primitive Data Exception Prevention
Index -> Programming, Java -> Java Submissions
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
randint




PostPosted: Thu Jan 05, 2012 9:54 am   Post subject: Primitive Data Exception Prevention

Once in a while, you come across some programs that need to use Integer.parseInt (String) and other methods that converts a String into an int, but all of these methods have the risk of throwing some form of Exception if the String is not parseable. try...catch blocks cannot be looped, so you have no solution to this? What about the programs that you are using? The ones that tell you that something is not a valid number and lets you try again? Well, here is a Java program I made to do this trick (You do need to use this class every time the problem occurs, and use a do...while loop so that only if the boolean named "validate" is set to true can your loop exit and move on).
An implementation of this class can be as the follows:
Java:

import javax.swing.*;
public class Exponent
{
    public static void main (String args[])
    {
        String base = "0", power = "0";
        boolean b [] = new boolean [2];
        double i [] = new double [2];
        do
        {
            base = JOptionPane.showInputDialog (null, "Enter the base of the exponent", "Base", 1);
            power = JOptionPane.showInputDialog (null, "Enter the power of the exponent", "Power", 1);
            DataTypeValidation.ValidateDouble (base);
            b [0] = DataTypeValidation.validate;
            i [0] = DataTypeValidation.DOUBLE;
            DataTypeValidation.ValidateDouble (power);
            b [1] = DataTypeValidation.validate;
            i [1] = DataTypeValidation.DOUBLE;
        }
        while (!b [0] || !b [1]);
        JOptionPane.showMessageDialog (null, "The answer is " + Math.pow (i [0], i [1]), "Exponent", 1);
    }
}

Note that the method
Java:

ValidateBoolean (String valid)
{
}

can only be used if Java version is 1.5 or higher.



DataTypeValidation.java
 Description:
Primitive Data Type Validator

Download
 Filename:  DataTypeValidation.java
 Filesize:  1.68 KB
 Downloaded:  267 Time(s)

Sponsor
Sponsor
Sponsor
sponsor
DemonWasp




PostPosted: Thu Jan 05, 2012 10:20 am   Post subject: RE:Primitive Data Exception Prevention

Err, try-catch blocks can be looped just fine.

Java:

boolean valid = false;
int value;

while ( ! valid ) {
    try {
        // get input here...
        value = Integer.parseInt ( input );
        valid = true;
    } catch ( NumberFormatException e ) {
        System.out.println ( "Invalid input: " + input );
    }
}


Where'd you get an idea like that?

But yes, there are plenty of ways of validating input, and you've found another one that certainly works.
randint




PostPosted: Thu Jan 05, 2012 11:25 am   Post subject: Implementation

Here is a thing, if you are getting dozens or hundreds of inputs every single time, you'd need a lot of booleans...yes, my way of using do loop validation is brutal, it abuses the programmer to use a do loop for every single input.
DemonWasp




PostPosted: Thu Jan 05, 2012 11:38 am   Post subject: RE:Primitive Data Exception Prevention

No, you would need one boolean, wrapped in a method. Ideally, to prompt for multiple integers, you'd do something like:

Java:

int foo = UserPrompt.integer ( "Enter foo value: " );
int bar = UserPrompt.integer ( "Enter bar value: " );
int baz = UserPrompt.integer ( "Enter baz value: " );


...or whatever, it's up to you and your application. But you should only have to write the loop once, with one flag variable; anything more is lunacy.
Display posts from previous:   
   Index -> Programming, Java -> Java Submissions
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 4 Posts ]
Jump to:   


Style:  
Search: