簡體   English   中英

找不到字符串時無法顯示消息 -Java

[英]Cannot display a message when string is not found -Java

我正在處理一個用戶可以通過名字或姓氏查找美國總統的任務。 該部分有效,但是當我嘗試讓它顯示一條消息時,如果找不到名稱,它要么不顯示,要么在找到名稱時也顯示。 我對java很陌生,任何幫助將不勝感激。

public static void main(String[] args) throws IOException, FileNotFoundException {
    // TODO Auto-generated method stub

    //calls method which checks that the file exists
    checkForDocument();

    // declares String name
    String name = null;

    //Sets button text to "Search by first name" for the first button and "Search by last name" for the second button
    Object[] options = {"Search by first name", "Search by last name"};

    //creates frame
    Component frame = null;

    // displays the following message above the options within the dialog box
    int n = JOptionPane.showOptionDialog(frame,
            "Presidents of The United States ",

            //
            null, JOptionPane.YES_NO_CANCEL_OPTION,

            //
            JOptionPane.QUESTION_MESSAGE, null, options, options[1]);

    // if the user clicks search by first name
    if (n == JOptionPane.YES_OPTION) {

        //firstNameSearch method is called
        firstNameSearch();

        //if user clicks search by last name
    } else if (n == JOptionPane.NO_OPTION) {

        //prompts the user to enter a name and assigns it to lastName
        String   lastName = JOptionPane.showInputDialog("Enter a last name");

        //calls the LastNameSearch method
        lastNameSearch(lastName);

    } 


    // trailing else
    else 
    {
        //
        JOptionPane.showMessageDialog (null, "You must choose an option");

    }

}


//method for searching by first name
public static void firstNameSearch () throws IOException {

    File file = new File ("USPres.txt");

    Scanner inputFile = new Scanner(file);

    //declares count
    int count;

    //user is prompted to enter a first name
    String firstName = JOptionPane.showInputDialog("Enter a first name");

    for  ( count = 1; inputFile.hasNext(); ++count)
    {
        // assigns string line to inputFile's Next line
        String line = inputFile.nextLine();

        //assigns string firstWord to first word in line
        String firstWord = line.split(" ")[0];

        //if firstWord equals firstNem
        if (firstWord.compareToIgnoreCase(firstName)==0)
        { 
            //prints the line
            JOptionPane.showMessageDialog(null,line);
        } 

        if ( count > 44)

            if (firstWord.compareToIgnoreCase(firstName)!=0)

                JOptionPane.showInternalMessageDialog(null, "Name not found");
    }

}

//method for searching by last name
public static void lastNameSearch(String lastName) throws IOException

{   
    //checks that document exists
    checkForDocument();

    // declares String lastWord
    String lastWord = null;

    File   file = new File ("USPres.txt");

    Scanner inputFile = new Scanner(file);

    // declares the string line
    String line = null;

    //while file has another line
    while(inputFile.hasNext()) {

        //assigns variable line to next line of input file
        line = inputFile.nextLine();   

        //assigns last word of a line to lastWord
        lastWord = line.substring(line.lastIndexOf(" ")+1);


        //if lastWord is equal to  the last name input by the user
        if(lastWord.compareToIgnoreCase(lastName)==0) { 

            //prints the line
            JOptionPane.showInternalMessageDialog(null,line);

        }
    }
}

// Checks that file exists
public static void checkForDocument() throws IOException
{
    // checks that USPres.txt exists
    try
    {
        //assigns file to USPres.txt
        File   file = new File ("USPres.txt");

        Scanner inputFile = new Scanner(file);

    }

    // deals with FileNotFoundException
    catch (FileNotFoundException e)
    {
        //message which displays if file does not exist
        JOptionPane.showMessageDialog(null, "Error. File not found");

        //terminates program
        System.exit(0);
    }

}

//displayMessage method header
public static void displayMessage() throws IOException{

    //displays message
    JOptionPane.showMessageDialog(null, "Name not found");

    //terminates program
    System.exit(0);

}

如果您使用 for/while 循環遍歷所有名稱,則只能說找不到名稱。 如果找到了名稱,您可以獲取結果並通過返回來中斷循環。 如果循環的每次迭代都沒有成功,您可以確定找不到名稱。 您還可以在循環后添加條件。 例如對於不同的對話消息。

for (int i=0; i<maxCount && inputFile.hasNext(); i++) {
    ...
        
    if (firstWord.compareToIgnoreCase(firstName) == 0) {
        JOptionPane.showMessageDialog(null, line);
        return;
    }
}

displayMessage();

下一步提示:嘗試提取重復代碼並使用帶參數的方法。 例如文件路徑。 ;)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM