Results 1 to 6 of 6

Thread: Using Java Exceptions, want to catch the user from entering a negative number

  1. #1
    Join Date
    Aug 2006
    Posts
    235

    Using Java Exceptions, want to catch the user from entering a negative number

    I have recently learned how to do try and catch statements. I have got some tutorials on doing the same. I have to catch the user from enter a negative number or a number above 100. All this I have to do in Java programming language. I have tried to do the many things, but not able to get the exact code. Does anyone know how to do this coding.? Please help me soon.
    3.2 (northwood)
    2gig ram
    ATI AIW X800xt 256mb
    Gigabyte GA-8knxp 875p Chipset
    Optiwrite 8X DVD Burner
    Win XP PRO Sp2 (Works Perfectly)
    2 SATA Raptor 74gig Raid 0
    2 7200 IDE 320gig HD

  2. #2
    Join Date
    Apr 2008
    Posts
    2,005

    Re: Using Java Exceptions, want to catch the user from entering a negative number

    The following code should help you to get the desired output.

    import java.io.*;
    import javax.swing.JOptionPane;
    public void findbuttonActionPerformed (ActionEvent event) throws IOException
    {
    try
    {
    value1 = Double.parseDouble (Test1_Text.getText ());
    value2 = Double.parseDouble (Test2_Text.getText ());
    value3 = Double.parseDouble (Test3_Text.getText ());
    value4 = Double.parseDouble (Exam_Text.getText ());

    }
    catch (NumberFormatException e)
    {
    JOptionPane.showMessageDialog (null, "Please Enter Right Value!!!");
    value1 = Double.parseDouble (Test1_Text.getText ());
    value2 = Double.parseDouble (Test2_Text.getText ());
    value3 = Double.parseDouble (Test3_Text.getText ());
    value4 = Double.parseDouble (Exam_Text.getText ());
    }

    }

  3. #3
    Join Date
    Nov 2005
    Posts
    1,323

    Re: Using Java Exceptions, want to catch the user from entering a negative number

    I think that you should clear the basic of the Exceptions in Java before moving to the coding. Because after clearing the concepts, you can use your logic for making the program of exceptions in Java. A method should throw an exception whenever it is unable to fulfill its specification, not as an alternate way of returning normally from a function. The only objects that can be thrown or caught by Java's exception handling mechanism are instances of classes in the Throwable class hierarchy. Classes that extend Exception are known as checked exceptions because the compiler checks to see whether two things occur in a program using these classes:
    • Every method that calls a method that advertises a checked exception must either handle that exception (with try and catch) or must in turn advertise that exception in its own throws clause.
    • Every method that throws a checked exception must advertise it in the throws clause in its method definition

  4. #4
    Join Date
    Oct 2005
    Posts
    2,393

    Re: Using Java Exceptions, want to catch the user from entering a negative number

    The runtime mechanism searches back up the call stack for a catch block that matches the type of the exception, when the exception occurs. Here the match means any type for which the instance of operator returns true when applied to the exception. You can simply catch an instance of exception, if you want to catch all checked exceptions like :
    try
    {
    // whatever
    }
    catch (Exception e)
    {
    // Catch all unchecked exceptions
    System.out.println(e);
    }

    You will have to be careful because your catch blocks are inspected in the order in which they appear in the code when looking for a match. Also the compiler will "catch" your error, if you try to place a handler for a superclass before a subclass handler, since the second block is unreachable.

  5. #5
    Join Date
    May 2008
    Posts
    2,389

    Re: Using Java Exceptions, want to catch the user from entering a negative number

    According to me, you can use the following command code to satisfy the queries that you have asked :
    • to catch the user from enter a negative number
    • to catch the user from enter a number above 100

    Here is the code :

    import java.io.*;
    public class number {

    public static void main (String[] args) {

    System.out.print("Enter a number between 0 and 100: ");

    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    String inputString = null;
    int inputInteger = 0;

    try {
    inputString = br.readLine();
    inputInteger = Integer.parseInt(inputString);
    } catch (IOException ioe) {
    System.out.println("Error!");
    System.exit(1);
    }
    if (inputInteger < 0 || inputInteger > 100) {
    System.out.println("Please enter a valid number!");
    } else {
    System.out.println(inputInteger);
    }
    }
    }

  6. #6
    Join Date
    May 2008
    Posts
    2,012

    Re: Using Java Exceptions, want to catch the user from entering a negative number

    An exception object is created when an exception is thrown and is passed to the catch block much like an parameter to a method :
    • The exception object is passed to the code designated to catch the exception, which may then use methods of the exception object to help handle the situation.
    • Occurrence of an exception generates an object containing information about the exception.
    There is an API class called Exception
    • another class, Error, also inherits from Throwable
    • all exception classes inherit from Exception, which inherits from Throwable
    • your code must handle most exceptions, but generally should not attempt to handle Error subtypes (like OutOfMemoryError or StackOverflowError)

Similar Threads

  1. How to Throw Exceptions in Java?
    By Bigga Lexx in forum Software Development
    Replies: 4
    Last Post: 18-02-2010, 05:57 AM
  2. How to catch an Exception in Java?
    By SKREECH in forum Software Development
    Replies: 4
    Last Post: 18-02-2010, 04:50 AM
  3. What are the different built-in Exceptions available in java?
    By ScarFace 01 in forum Software Development
    Replies: 5
    Last Post: 22-01-2010, 09:06 AM
  4. What are user defined exceptions in Java?
    By Migueel in forum Software Development
    Replies: 5
    Last Post: 30-11-2009, 07:27 PM
  5. Explain Try Catch Block in java
    By cyber-noob in forum Software Development
    Replies: 3
    Last Post: 16-11-2009, 02:13 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,982,117.23953 seconds with 17 queries