當我試圖運行它並檢查錯誤時,我注意到在我的一個方法中允許在非法輸入後重新嘗試的do-while循環不起作用當該方法被調用時。在do-while循環中使用JSoup,仍然拋出異常
我的問題是: 1)爲什麼PageScanner()方法中的do-while循環不起作用? 2)你能否提出一種更好的方法來組織我的代碼?
這是它目前的樣子:
第一類:「BulkSpellChecker」(其中我試圖組織代碼)
import java.io.*;
public class BulkSpellChecker extends ParseCleanCheck {
public static void main(String[] args) throws IOException {
System.out.println("Let's get started!");
PageScanner(); // Scan the page and clean it first
System.out.println("Thanks for using the spell checker!");
}}
第二課堂「ParseCleanCheck」(這裏所有的醜陋的發生)
import java.io.*;
import java.util.*;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.safety.Whitelist;
public class ParseCleanCheck {
static Scanner urlInput = new Scanner(System.in);
public static String cleanString = "";
public static String url = "";
public static boolean correct = true;
/**
* PARSER METHOD
*/
public static void PageScanner() throws IOException {
System.out.println("Pick a website to scan.");
System.out.println("Enter a URL, starting with http://");
do { // This do-while loop allows the user to try again after a mistake
try {
url = urlInput.nextLine();
correct = false;
} catch (Exception e) {
System.out.println("Incorrect format for a URL.");
}
} while (correct == true);
// This creates a document out of the HTML on the web page
Document doc = Jsoup.connect(url).get();
// This converts the document into a string to be cleaned
String htmlToClean = doc.toString();
cleanString = Jsoup.clean(htmlToClean, Whitelist.none());
}}
免責聲明:我對編程相當陌生,如果答案很明顯,或者發佈的代碼太多,我很抱歉。如有必要,我可以編輯和刪除代碼。
你在這裏發表* *很多的代碼。請將其降低到[只能*顯示問題的[mcve])。 (做好避免問這裏的其他例外 - 每個職位一個問題:) –
感謝您的反饋意見。我編輯了代碼以最小化它。我的問題只是解析方法,所以我意識到我現在可以刪除其餘的。 – ary
好吧,這看起來很多*更*最小 - 但我認爲它可以進一步減少。如果你把所有東西都放到你的'main'方法中,那麼完全拋棄Jsoup,只有一個類,使用局部變量而不是靜態變量,你是否仍然看到問題?你能描述一下你看到的和你期望看到的內容嗎? (這種診斷研究是成爲軟件工程師的重要組成部分 - 我希望整個過程能夠幫助您學習。) –