3

I am checking if number the user entered is Zeckendorf and I want to display an exception if it is not, how do i do that? Also how do I convert the Zeckondorf to its decimal equivalent?

import java.util.Scanner;


public class IZeckendorfNumberConvertor {
static String number;
int zeckonderEquivalent;
static Scanner scanner = new Scanner(System.in);

public static void main(String[] args) {

    convertToZeckonderNumber();
    isTrueZeckonderNumber();
}

private static boolean isTrueZeckonderNumber() {
    System.out.println("Enter a Zeckonder Number:");
    number = scanner.nextLine();
    if (number.equals("10100")) 
    {
        return true; } 
    else if (number.equals("010011") || number.equals("010100")) 
    { 
        return false; }
    return false;
}

private static void convertToZeckonderNumber() {

}}
Jim Tim
  • 31
  • 1

7 Answers7

6

I advise you not to display an exception (i.e. trace and such) as it is very user Unfriendly. You can use the throw syntax to throw a proper exception :

 throw new Exception("Given number is not a Zeckendorf number");

but be sure to catch it and display a nice and clean message :

try {
   // Your input code goes here
} catch (Exception e) {
    System.out.println(e.getMessage());
}

Another easier option will be to just check the return value of the method and print the results accordingly.

I will recommend to use the latter solution as exceptions are used when something bad and unexpected happens in your program and you want to handle it gracefully. In your case the behavior is expected (i.e. user giving a wrong number) so checking the return value will be much clean and easier.

giorashc
  • 13,691
  • 3
  • 35
  • 71
4

Use try catch block for catch an exception

            try {

            } catch (Exception e) {

                e.printStackTrace();
            }

Also use throw for throw a new exception

enter image description here

2

Assuming to really do want to display the exception, and not a more user friendly message, the first step is probably to get the exception as a string. Then you can do what you like with that string (echo to console, place in a javax.swing.JTextArea, email it, etc).

If you just want the message from the exception, then getMessage() will do:

try { ... }
catch(FooException e) {
    String msg = e.getMessage();
}

However, if you want the whole exception, stack trace and all, you'll want something like this:

public static String stackTrace(Exception e) {
    StringWriter w = new StringWriter();
    e.printStackTrace(new PrintWriter(w));
    return w.toString();
}

// ...

try { ... }
catch(FooException e) {
    String st = stackTrace(e);
}

If you just want to echo the full stack trace to the console, there is the printStackTrace(), no-arg method:

try { ... }
catch(FooException e) {
    e.printStackTrace();
}

If you want to take more control of the presentation of the stack trace you can get the details with:

try { ... }
catch(FooException e) {
    StackTraceElement[] stes = e.getStackTrace();
    // Do something pretty with 'stes' here...
}     
Paul
  • 3,009
  • 16
  • 33
1

What do you mean you want to display an exception? I would suggest just giving the user feedback instead, as exceptions are used more commonly for EXCEPTIONAL actions that are not supposed to happen.

However if you do want to, you can print a message explaining what happened.

try {

} catch (Exception e) {
    System.out.println(e.getMessage());
}
Daniel Larsson
  • 6,278
  • 5
  • 44
  • 82
1

You can just print a error message to the user saying that the input is wrong using a simple if.

if(yourCondition){
    // Happy scenario
    // Go shead
}else{
    // Error Scenario
    System.out.println("Error. Invalid Input.");
    // If you persist to throw an exception, then you can do something like this
    // throw new Exception("Exception Explanation"); // I've commented this, but you can uncomment it if needed
    // But the advice is to display an error message, than throw a exception.
}

And regarding the conversion, you can convert binary to decimal like this

int i = Integer.parseInt(binaryString, 2); // 2 indicates the radix here, since you want to convert from binary.
Rahul
  • 44,383
  • 11
  • 84
  • 103
1

With this code snippet you can convert the String into an integer :

int numberAsInt;
    try {
          numberAsInt = Integer.parseInt(number);
    } catch (NumberFormatException e) {
          //Will throw an Exception
    }

If you want to create your own Exception class, you can do it like shown here or just throw a RuntimeException with

throw new RuntimeException("Your Message");
Community
  • 1
  • 1
Akkusativobjekt
  • 2,005
  • 1
  • 21
  • 26
1

My opinion, you can try some thing like following

 public static void main(String[] args) {    
  if(!isTrueZeckonderNumber()){
      // your message should goes here
       System.out.println("Your message");
    }
 }

If you really want to throws an exception do following

 private static boolean isTrueZeckonderNumber() throws Exception{
    System.out.println("Enter a Zeckonder Number:");
    number = scanner.nextLine();
    if (number.equals("10100")) {
        return true;
    } else{
        throw new Exception("your message");
    }        
}
Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115