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());
}
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