How to properly throw an exception/call a method?

Hello,
Here is my prompt:

. (NumberFormatException) Write the bin2Dec(String binaryString) method to convert a binary
string into a decimal number. Implement the bin2Dec method to throw a NumberFormatException if
the string is not a binary string. Write a test program that prompts the user to enter a binary number
as a string and displays its decimal equivalent. If the method throws an exception, display “Not a
binary number”.

Here is my code:

//import java.util library
import java.util.*;
//class
public class Problem2 {
    //main method
    public static void main(String[] args){
        //scanner
        Scanner sc = new Scanner(System.in);
        //prompt user to enter string
        System.out.println("Enter a binary number as a string: ");
        String binaryString = sc.nextLine();
        
        //try/catch
        try{
        int inputToDecimal = Integer.parseInt(binaryString, 2);
        System.out.println("Decimal Equivalent: " + inputToDecimal);
        }
        catch(NumberFormatException ex){
            System.out.println(ex.getMessage());
        }

        //call method
        bin2Dec(binaryString);
        

    }
    //write new method
    public static String bin2Dec(String binaryString){
        if (/* binaryString != binaryString */){
            throw new NumberFormatException("Not a binary number: " + binaryString);
        }
        


    }
    
}

Here are my errors:

Exception in thread “main” java.lang.Error: Unresolved compilation problem:
Syntax error on token “(”, Expression expected after this token

    at Problem2.bin2Dec(Problem2.java:34)
    at Problem2.main(Problem2.java:28)

In the bin2Dec method, I am not sure what to put in the ‘if()’ to say that the user’s input was not a binary string.
Also, the last few times I’ve tried to call methods in other programs, VSC is not happy with me and I get errors. Any help is appreciated.
A straight up answer rather than a link would be nice. I’m on the forums because I’ve exhausted any links already lol.

Hi,

In general straight up answers are less useful than answers that contextualize the problem, I think that’s why people tend to prefer giving those.

The precise answer to your issue here is in the interpretation of the error message. A missing token is just syntax, your try/catches should work perfectly. I can’t tell if that error was the same as the code you posted since when I run that code I just got 2 errors (one for the if statement being commented out, and for the bin2Dec() method not having a return value). Upon dealing with those your code ran thus:

So it seems to be running well.

As to the missing token syntax type of errors, they usually (not always) imply that something right before the marked lines is missing a close bracket or something of that sort.

1 Like

Thank you for your reply.
Yes, the try-catch part of the code is running just fine. From what I understand of the prompt, in addition to the try-catch, my professor wants us to create a new method called String bin2Dec(String binaryString), which I have done here:

 //write new method
    public static String bin2Dec(String binaryString){
        if (/* binaryString != binaryString */){
            throw new NumberFormatException("Not a binary number: " + binaryString);
        }
        


    }

And that is where my problems come from.

  1. I’m not sure what to put in the ‘()’ after ‘if’ to signify that the user has not entered a binary string.
  2. Typically when I call a method in main it doesn’t really work out for me, despite following similar steps from previous projects. I called it here:

[quote="arianna.pogact, post:1, topic:546589"]

//call method
bin2Dec(binaryString);

[/quote]

And that is where I am looking for some help.

Whoops, formatted that wrong. It should be:

//call method
bin2Dec(binaryString);

That’s the second place I’m looking for help lol

This looks fine for a method call (assuming the method is static, which it is).


I’m not sure if this would meet the exact requirements of the task, but take a step back and reevaluate what the problem is asking you to do. Do you need to use an if statement? How else could you display the error message if necessary after checking if binaryString is a valid binary string?

Hint --- only look at this if you're really stuck. Try reevaluating the problem first yourself.

If the following can display a custom error message if a NumberFormatException occurs, how could you implement this into the bin2Dec method?

Further Hint -- only use if necessary.

If I were to solve this problem, all the code you have written (except for possibly a few very simple lines) would be enough to solve it (just need to rearrange).

Even Further Hint -- again, only use if necessary.

Keep the following in mind. It looks like you might be approaching the problem in a different way than your professor expected. Try implementing the following instruction, along with the error message if binaryString is invalid.

1 Like