Try-Catch Block not handeling the exception the way I tend to

101 Views Asked by At

Ok So my program is to receive a URL from the User to a database and process this data Bases by calculating the average and displaying that. My Algorithm for that works perfectly fine the issue I am having is with the Handling of the URL exception. If a User enters an invalid URL i want them to try again and enter a correct one, but when the Users enter the URL again none of the data from the URL is processed and i keep getting "NaN" in the output. Below is the try catch blocks as i believe that the problem lies there, please help out if you can.

//Test URL //URL salary = new URL("http://cs.armstrong.edu/liang/data/Salary.txt");

    System.out.println("Enter The URL to the File ");
    Scanner UserInput = new Scanner(System.in);


    try
    {

        //Ask The User to Input the URL

        URL salary = new URL(UserInput.nextLine());



        Scanner Read = new Scanner(salary.openStream());



        while(Read.hasNextLine())
        {

            for(int i = 0; i < Faculty.length; i++)
            {



                String Firstname = Read.next();


                String Lastname = Read.next();

                String rank = Read.next();

                double  Salary = Read.nextDouble();


                if(rank.matches("assistant")) 

                {
                   Faculty[i] = new AssistantProfessor(Firstname, Lastname, rank, Salary);
                   allAssistantProff[i] = new AssistantProfessor(Firstname, Lastname, rank, Salary);
                   AssistantProfessors++;
                }

                if(rank.matches("associate"))

                {
                   Faculty[i] = new AssociateProfessor(Firstname, Lastname, rank ,Salary);
                   allAssociateProff[i] = new AssociateProfessor(Firstname, Lastname, rank ,Salary);
                   AssociateProfessors++;
                }

                if(rank.matches("full"))
                {

                    Faculty[i] = new FullProfessor(Firstname, Lastname, rank, Salary);
                    allFullProff[i] = new FullProfessor(Firstname, Lastname, rank, Salary); 
                    FullProfessors++;
                }

                    //System.out.println(Faculty[i]);   
                    //System.out.println(allAssistantProff[i]);
                    //System.out.println(allAssociateProff[i]);
                    //System.out.println(allFullProff[i]);



        }


        }


    }



     catch(MalformedURLException ex)
    {

        System.out.println("invalid URL" + " Try Again ");
        URL salary = new URL(UserInput.nextLine());
    }
2

There are 2 best solutions below

0
On

Have you tried running the code by entering 2 valid urls after each other?

I ask this as I don't see an outer while, or "jump-back" that would restart the "Scanner Read = new Scanner(salary.openStream());" line

if you have a outer jumpback, you are declaring a local variable salary in the exception handling, that will be deleted after the "}" in the catch block this might not be a problem tho

0
On

Move your try-catch into a loop, like this:

System.out.println("Enter The URL to the File ");
Scanner UserInput = new Scanner(System.in);
Scanner Read;
while (true) {
  try {
    URL salary = new URL(UserInput.nextLine());
    Read = new Scanner(salary.openStream());
    break;
  } catch(IOException ex) {
    System.out.println("Couldn't open URL. Try again.");
  }
}
while(Read.hasNextLine()) {
  /* Process your file as usual. */
}