Subscribe to extremeblueness' Blog        RSS Feed
-----

I'm Back and Making Unbreakable Code

Icon 2 Comments
First of all, I'd like to say that I'm sorry that I haven't made an entry for two whole weeks, but I'm posting again.

Now for the core of the entry.

This past week I've been continuing to learn java outside of my class (currently at generics), but I've been primarily focusing on perfecting exception handling. My programming teacher had been continually trying to break (produce a run-time and/or logic error for my code). But I finally produced a program that he couldn't break using an exception handling system. Here are some examples:

If you have a variable that runs on Number types:
import java.io.*;
import java.util.*;

public class Example
{
   public static void main (String[] args)
      throws IOException
   {
      //main method goes here
   }
   static void value()
   {
      for( ; ; )
      {
         Scanner reader = new Scanner(System.in);
         double x;

         System.out.println("Please enter x.\n")

         try
         {
            x = scanner.nextDouble()
         }
         catch (InputMismatchException wrongType)
         {
            System.out.println("Error.\nNot a double.\nPlease try again.\n");
            continue;
         }
         return x;
      }
   }
}



If you have a variable that runs on positive Number types:
import java.io.*;
import java.util.*;

public class ExampleTwo
{
   public static void main (String[] args)
      throws IOException
   {
      //main method goes here
   }
   static void value()
   {
      for( ; ; )
      {
         Scanner reader = new Scanner(System.in);
         double x;

         System.out.println("Please enter x.\n")

         try
         {
            x = scanner.nextDouble()
         }
         catch (InputMismatchException wrongType)
         {
            System.out.println("Error.\nNot a double.\nPlease try again.\n");
            continue;
         }
         if (x < 0)
         {
            System.out.println("Error.\nNegative number not possible.\nPlease try again.\n");
            continue;
         }
         return x;
      }
   }
}



If you have a variable that runs on positive, not zero Number types:
import java.io.*;
import java.util.*;

public class ExampleThree
{
   public static void main (String[] args)
      throws IOException
   {
      //main method goes here
   }
   static void value()
   {
      for( ; ; )
      {
         Scanner reader = new Scanner(System.in);
         double x;

         System.out.println("Please enter x.\n")

         try
         {
            x = scanner.nextDouble()
         }
         catch (InputMismatchException wrongType)
         {
            System.out.println("Error.\nNot a double.\nPlease try again.\n");
            continue;
         }
         if (x <= 0)
         {
            System.out.println("Error.\nNegative or null values not possible.\nPlease try again.\n");
            continue;
         }
         return x;
      }
   }
}



Note that the return statements are INSIDE the for loop. This combined with the continues causes it to restart the secondary method.

2 Comments On This Entry

Page 1 of 1

Ryano121 Icon

01 December 2012 - 07:44 AM
Just for the sake of completeness, at the moment you are creating a new Scanner object every time around the loop - it's just a bit of a waste of resources as it only needs to be created once. Moving the declaration above the for( ; ;) solves that for you :)
0

extremeblueness Icon

01 December 2012 - 10:15 AM
Really? Thanks. I wasn't sure if I needed to create a new one or not. But at least it doesn't have any bugs no matter what your input is (unless you're trying to hack it)
0
Page 1 of 1

Trackbacks for this entry [ Trackback URL ]

There are no Trackbacks for this entry

July 2014

S M T W T F S
  12345
6789101112
13141516171819
20212223242526
2728 29 3031  

Tags

    Recent Entries

    Recent Comments

    Search My Blog

    0 user(s) viewing

    0 Guests
    0 member(s)
    0 anonymous member(s)

    Categories