好所以我的程序是從用戶接收到一個URL到一個數據庫,並通過計算平均值並顯示它來處理這些數據。我的算法對此非常好,我遇到的問題是處理URL異常。如果用戶輸入無效的URL,我希望他們再次嘗試並輸入正確的URL,但是當用戶再次輸入URL時,URL中的數據都不會被處理,並且我一直在輸出中收到「NaN」。下面是try catch塊,因爲我相信問題在那裏,如果可以的話請幫忙。Try-Catch Block不處理異常我傾向於的方式
//測試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());
}
那麼,你有什麼異常?我的意思是控制達到攔截? – tesnik03
這不是一個調試服務。自己使用調試器。 – Raedwald