Showing posts with label catch. Show all posts
Showing posts with label catch. Show all posts

Wednesday, 14 January 2015

Division by Zero in Java

Java does not stop you dividing by zero but, if you try, it gives you a run-time error:
 
Java > cat prog81.java
public class prog81
{
public static void main (String args[])
  {
  int x = 1/0;
  System.out.println("x = " + x);
  }
}
Java > javac prog81.java
Java > java prog81
Exception in thread "main" java.lang.ArithmeticException: / by zero
        at prog81.main(prog81.java:5)
Java >
 
You can trap these errors and handle them tidily as shown below. This stops your program falling over:
 
Java > cat prog82.java
public class prog82
{
public static void main (String args[])
  {
  try
    {
    System.out.println("Dividing 1 by 0");
    int x = 1/0;
    System.out.println("x = " + x);
    }
  catch (ArithmeticException e)
    {
    System.out.println("Division by zero not allowed");
    }
  try
    {
    System.out.println("Dividing 1 by 1");
    int y = 1/1;
    System.out.println("y = " + y);
    }
  catch (ArithmeticException e)
    {
    System.out.println("Division by zero not allowed");
    }
  }
}
Java > javac prog82.java
Java > java prog82
Dividing 1 by 0
Division by zero not allowed
Dividing 1 by 1
y = 1
Java >
 
However, if you divide 1.0 by 0.0, the answer is infinity:
 
Java > cat prog83.java
public class prog83
{
public static void main (String args[])
  {
  double x = 1.0/0.0;
  System.out.println("x = " + x);
  }
}
Java > javac prog83.java
Java > java prog83
x = Infinity
Java >

... and, if you divide 0.0 by 0.0, the answer is NaN (not a number):

andrew@UBUNTU:~/Java$ cat prog84.java
public class prog84
{
public static void main (String args[])
  {
  double x = 0.0/0.0;
  System.out.println("x = " + x);
  }
}
andrew@UBUNTU:~/Java$ javac prog84.java
andrew@UBUNTU:~/Java$ java prog84
x = NaN
andrew@UBUNTU:~/Java$

Saturday, 25 May 2013

User I/O in Java

This example does console input/output in Java. It asks the user to type his / her name then says Hello.

At the start of the program, the java.io package is imported. This contains code to do system input / output. The asterisk which follows means that the program may want to use any of the classes in java.io.

The program creates an InputStreamReader class variable called isr to read what the user types. It also creates a BufferedReader class variable called br to store it temporarily.

The try block reads the user's input and the catch block handles exceptions: 

UBUNTU > cat prog31.java
import java.io.*;
public class prog31
{
public static void main(String[] args)
  {
  System.out.print("What is your name? ");
  InputStreamReader isr = new InputStreamReader(System.in);
  BufferedReader br = new BufferedReader(isr);
  try
    {
    String your_name = br.readLine();
    System.out.println("Hello " + your_name);
    }
  catch (IOException test)
    {
    System.out.println("I/O error");
    }
  }
}
UBUNTU > javac prog31.java
UBUNTU > java prog31
What is your name? Andrew
Hello Andrew
UBUNTU >