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:
If you have a variable that runs on positive Number types:
If you have a variable that runs on positive, not zero Number types:
Note that the return statements are INSIDE the for loop. This combined with the continues causes it to restart the secondary method.
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
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
Page 1 of 1
Trackbacks for this entry [ Trackback URL ]
Tags
My Blog Links
Recent Entries
-
-
-
-
-
I'm Back and Making Unbreakable Codeon Dec 01 2012 06:47 AM
Recent Comments
Search My Blog
0 user(s) viewing
0 Guests
0 member(s)
0 anonymous member(s)
0 member(s)
0 anonymous member(s)
Categories
|
|



2 Comments









|