0

I'm currently having an issue with this function. I'm getting an error for the numSongs in getSongsArray(numSongs); saying that I need to throw an exception (unreported exception Exception; must be caught or declare to be thrown). Below is the instructions and the code I currently have. I'm just a girl whose been struggling on this for about 3 hours now and at this point I'm desperate.

Instructions: Write a method called getSongsArray that takes an integer parameter (numSongs), returns an array of Strings, and is declared to throw an Exception. If the parameter passed is a negative value, throw an Exception. Otherwise, loop numSongs times prompting the user to enter another name each pass through the loop. Return this array of names.

Call getSongsArray passing the size of the array (any integer) that you want returned

public static void main(String[] args) 
    {
        Scanner scnr = new Scanner(System.in);
        System.out.println("How many songs would you like to enter?");
        int numSongs = scnr.nextInt();

        getSongsArray(numSongs);
    } //end main

    public static int getSongsArray(int numSongs) throws Exception 
    {
        if (numSongs <= 0) 
        {
            throw new Exception("Invalid Number");
        }
    } //end getSongsArray
Elizabeth
  • 1
  • 1
  • Can you add the exact error-message? – itzFlubby Apr 30 '20 at 22:05
  • Just added what the exact error message says :) – Elizabeth Apr 30 '20 at 22:09
  • 1
    Does this answer your question? [Java unreported exception](https://stackoverflow.com/questions/2091589/java-unreported-exception) – UnholySheep Apr 30 '20 at 22:11
  • @Elizabeth, you've successfully completed 1/3 of your assignment. In your existing method `getSongsArray` you need to change return type to int[]. You should keep on working and you'll succeed. You may look [here](https://stackoverflow.com/questions/58326653/how-to-fill-an-array-with-int-numbers-using-a-loop-in-java) for the answer – Nowhere Man Apr 30 '20 at 22:12
  • @UnholySheep sadly no :( ive tried doing try catch exceptions and it results in more errors – Elizabeth Apr 30 '20 at 22:14
  • @AlexRudenko can you clarify where to change return type to int[] im a little confused – Elizabeth Apr 30 '20 at 22:14
  • Sorry, I meant change method signature to `private static String[] getSongsArray(int num)` – Nowhere Man Apr 30 '20 at 22:18

1 Answers1

0

You're problem is that getSongsArray can throw an exception and that this isn't caught in your main-function.
The solution to this is simple: catch the error-message;

public static void main(String[] args) 
    {
        Scanner scnr = new Scanner(System.in);
        System.out.println("How many songs would you like to enter?");
        int numSongs = scnr.nextInt();
        try {
            getSongsArray(numSongs);
        } catch (Exception e){ 
            System.out.println("Error!");
        }
    } //end main

But you're still going to have other problems, as you aren't completely done with your task. This post here only answeres the specific question about the unreported exception error!

itzFlubby
  • 2,269
  • 1
  • 9
  • 31