Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By jim829

Thread: Implementing try/catch with BinaryToDecimal program

  1. #1
    javaStooge is offline Senior Member
    Join Date
    Jan 2014
    Posts
    106
    Rep Power
    0

    Question Implementing try/catch with BinaryToDecimal program

    I've been assigned to write a program that will convert binary to decimal that uses the try/catch block. In the program that I have written, I was wondering if it is possible to write an addition catch statement that will present an error if any number other than a 0 or 1 is entered by the user. I have already done this in the binaryToDecimal method, but I am just messing around to see if it is, in fact, possible.

    As always, thanks for any critiques and advice.

    Java Code:
    import java.io.IOException;
    import java.util.Scanner;
    
    public class BinaryToDecimal {
    
    	public static void main(String[] args){
    		Scanner input = new Scanner(System.in);
    		try{
    		System.out.print("Enter binary value: ");
    		String binaryString = input.next();
    		int i = binaryToDecimal(binaryString);
    		System.out.println("Decimal: " + i);
    		}
    		catch(NumberFormatException ex){
    			System.out.println("Error Occurred");
    			System.out.println(ex.getMessage());
    		}
    //I was thinking that I could enter similar if/else statements within this catch as I have done in the binaryToDecimal method. 
    		catch(Exception ex){
    			System.out.println("Error! Entry must be 0 and 1.");
    		}
    	}
    	
    	public static int binaryToDecimal(String binaryString){
    		int decimalValue = 0;
    		
    		for(int i = 0; i < binaryString.length() ; i++){
    			Character binaryCharacter = binaryString.charAt(i);
    			String binString = binaryCharacter.toString();
    			int tempInt = Integer.parseInt(binString);
    			if(tempInt != 0 && tempInt!=1){
    				System.out.println("Invalid Entry! Try Again.");	
    				System.exit(0);
    			}
    			else
    			decimalValue = (int) Math.pow(2, binaryString.length());
    		}
    		return decimalValue;
    	}
    }

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Implementing try/catch with BinaryToDecimal program

    Yes. You can either construct your own Exception and then just throw it or use an already existing one. Like,
    Java Code:
    throw new IllegalArgumentException("Number must be 0 or 1");
    In this case, you check for input and throw the exception. It is up to the user to catch it and process it appropriately.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    javaStooge is offline Senior Member
    Join Date
    Jan 2014
    Posts
    106
    Rep Power
    0

    Default Re: Implementing try/catch with BinaryToDecimal program

    If I follow correctly, would I just be adding a throw Exception to my binaryToDecimal method?

    If that is the case, it is just a matter of programming style, rather than any significant value in one or the other.

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Implementing try/catch with BinaryToDecimal program

    Well, when you throw the exception, it provides some feedback to the user. Also, if you don't declare that the method throws an exception, the user has a choice whether to use a try catch block. If you put a throws clause after the method signature, then the user is obligated to handle the exception.

    See --> Lesson: Exceptions (The Java™ Tutorials > Essential Classes)

    It does a pretty good job of explaining it.

    Regards,
    Jim
    javaStooge likes this.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    javaStooge is offline Senior Member
    Join Date
    Jan 2014
    Posts
    106
    Rep Power
    0

    Default Re: Implementing try/catch with BinaryToDecimal program

    I will check it out. Thanks for clearing that up.

Similar Threads

  1. BinaryToDecimal
    By rajeev in forum New To Java
    Replies: 8
    Last Post: 08-23-2013, 04:54 AM
  2. Implementing a counter for recursive program
    By wtbarr in forum New To Java
    Replies: 3
    Last Post: 04-20-2013, 11:41 AM
  3. Help with BinarytoDecimal programme
    By Thompson in forum New To Java
    Replies: 3
    Last Post: 02-23-2011, 10:43 PM
  4. how to catch two exceptions in one catch()?
    By arnab321 in forum New To Java
    Replies: 1
    Last Post: 11-06-2008, 10:54 AM
  5. try catch!?
    By Joe2003 in forum Advanced Java
    Replies: 2
    Last Post: 01-28-2008, 07:51 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •