2016-09-28 76 views
0

我想從有姓名和電話號碼也可以在它的其他的文本文件的文本文件閱讀(包括它的自我)跳出循環遞歸函數,但讓循環繼續

myBook .TXT:

7 
name1 123-456-7890 
name2 098-765-4321 
name3 135-792-4680 
name4 246-801-3579 
PHONEBOOK-FILE myBook2.txt 
name5 147-025-8369 
name6 150-263-7495 

myBook2.txt:

1 
Name7 000-222-3332 

的第一行是在該文件中的項目數,則它具有電話簿-FILE以表示另一個文件。

我不能使用數組,我不能改變myBook.txt,我不能使用try/catch和我都用遞歸

這是我的代碼有:

import java.util.*; 
import java.io.*; 

public class Phonebook 
{ 
private boolean DEBUG = true; 
private Scanner scan; 
private Scanner input; 
private File file; 
private File holder; 
private String query; 
private boolean bottomOut; 
private int nameCount; 
private String fileNameHold; 

// entry point for class 
public void run()throws IOException 
{ 
    input = new Scanner(System.in); 

    //Gets file name and checks if it exists valid file 
    while(true) 
    { 
     System.out.print("Name of phone book to read in: "); 
     fileNameHold = input.next(); 
     file = new File(fileNameHold); 
     if(file.exists()) 
      break; 
     else 
      System.out.println("That file does not exist!"); 
    } 
    System.out.println("Phonebook successfully read in!"); 

    //Main control loop 
    while(true) 
    { 
     bottomOut = false; 
     System.out.print("Please enter person to search for: "); 
     query = input.next(); 
     if(query.equals(".")) 
      break; 
     file = new File(fileNameHold); 
     System.out.println(doWork(query, file, 0)); 
    } 

    System.out.print("Thank you for using this program!"); 
    } 

    //Does the searching and recursive stuff 
    private String doWork(String query, File fileName, int level)throws IOException 
    { 
    scan = new Scanner(fileName); 

    //Grabs item count fom begining of file 
    //if(!bottomOut) 
    nameCount = Integer.parseInt(scan.nextLine()); 
    String line = ""; 

    //Runs through entries 
    for(int i=0; i<nameCount; i++) 
    { 
     line = scan.nextLine(); 
     debug("file: " +file); 
     debug("line: " + line); 
     debug("nameCount: " + nameCount); 

     if(line.toLowerCase().contains(query.toLowerCase())) 
     { 

      return line; 
     } 
     //Recursion is used to searth through linked files 
     else if(line.contains("PHONEBOOK-FILE")) 
     { 
      //System.out.println("Sanity Check"); 
      holder = new File(line.replace("PHONEBOOK-FILE ", "")); 
      if(level < 2 || (level > 0 && bottomOut)) 
       return doWork(query, holder, ++level); 

      else if(level >= 2 && !bottomOut) 
       bottomOut = true; 

      else 
       return "not found (REC)"; 

     } 

    } 
    return "not found"; 
    } 

    private void debug(String stuff) 
    { 
     if(DEBUG) 
      System.out.println("[[--DEBUG--]] " + stuff); 
    } 
} 

我承擔問題在於doWork,但我可能是錯的。它正在做的是通過文件遞歸,直到遇到指定的底部,如果它沒有找到它應該跳出遞歸的名字並繼續通過PHONEBOOK-FILE行。

當前如果您搜索通過該行的名稱,如果找不到返回。它似乎沒有出現遞歸。

正如你可以告訴我一個不是很好這個。 感謝您的幫助。

回答

1

對於文件中的每一行,您將計算一個值。要麼找不到,要麼是你的電話簿的一行。如果你得到一條線,你可以跳出循環。無論哪種方式,在循環之後,您將返回值:或者找到或未找到的行;

更棘手的是如何計算引用另一電話簿的行,答案是您只需使用該電話簿調用您的方法即可。這是遞歸部分。

import java.util.*; 
import java.io.*; 

public class Phonebook 
{ 
private Scanner input; 
private File file; 
private String query; 

// entry point for class 
public void run()throws IOException 
{ 
    input = new Scanner(System.in); 

    //Gets file name and checks if it exists valid file 
    while(true) 
    { 
     System.out.print("Name of phone book to read in: "); 
     fileNameHold = input.next(); 
     file = new File(fileNameHold); 
     if(file.exists()) 
      break; 
     else 
      System.out.println("That file does not exist!"); 
    } 
    System.out.println("Phonebook successfully read in!"); 

    //Main control loop 
    while(true) 
    { 
     bottomOut = false; 
     System.out.print("Please enter person to search for: "); 
     query = input.next(); 
     if(query.equals(".")) 
      break; 
     file = new File(fileNameHold); 
     System.out.println(doWork(query, file)); 
    } 

    System.out.print("Thank you for using this program!"); 
    } 

    //Does the searching and recursive stuff 
    private String doWork(String query, File fileName)throws IOException 
    { 
    Scanner scan = new Scanner(fileName); 
    int nameCount; 
    File recurFile; 

    nameCount = Integer.parseInt(scan.nextLine()); 
    String line = ""; 
    String value = "Not found"; 
    //Runs through entries 
    for(int i=0; i<nameCount; i++) 
    { 
     line = scan.nextLine(); 
     // if the line is a file, then the value of that line 
     // is the result to your function applied to that new file 
     if(line.contains("PHONEBOOK-FILE")) { 
      recurFile = new File(line.replace("PHONEBOOK-FILE ", "")); 
      line = doWork(query, holder, ++level); 
     } 
     // the file will either return Not found or 
     // a line corresponding to your query 
     if(line.toLowerCase().contains(query.toLowerCase())) 
     { 
      // Your line is correct. The function doesn't care where it comes from 
      value = line; 
      break; 
     } 

    } 
    return value; 
    } 


}