2017-05-11 14 views
0

텍스트 파일의 문자열을 사용하여 사용자 프로필을 만드는 프로그램을 만들고 각 프로필을 이진 트리에 삽입하려고합니다. 모든 것이 순서대로되어야하지만 파일을 읽으려고 할 때 NoSuchElementFound 예외가 발생합니다.파일에서 문자열을 읽고 이진 트리에 삽입하는 방법

public class BST { 
private static BSTNode root; 

BST(){ 

} 

public void insertProfile(Profile p){ 
    BSTNode currentNode = new BSTNode(p); 
    if(root == null){ 
     root = currentNode; 
    } 
    else{ 
     recursiveMethod(currentNode); 
    } 
} 

private static void recursiveMethod(BSTNode currentNode){ 
    Profile p1 = root.getProfile(); 
    Profile p2 = currentNode.getProfile(); 
    if(p1.getName().compareTo(p2.getName()) < 0 && currentNode.getLeft() != null){ 
     recursiveMethod(currentNode.getLeft()); 
    } 
    if(p1.getName().compareTo(p2.getName()) > 0 && currentNode.getLeft() !=null){ 
     recursiveMethod(currentNode.getRight()); 
    } 
} 

}

public class BSTNode { 
private BSTNode left; 
private BSTNode right; 
private Profile data; 

public BSTNode(Profile data){ 
    this.data = data; 
    setLeft(left); 
    setRight(right); 
} 

public Profile getProfile(){ 
    return data; 
} 
public void setLeft(BSTNode l){ 
    this.left = l; 
} 
public BSTNode getLeft(){ 
    return left; 
} 
public void setRight(BSTNode r){ 
    this.right = r; 
} 
public BSTNode getRight(){ 
    return right; 
} 

}

:

그리고 여기 내 이진 트리 클래스입니다

public class FileReader { 

public static BST readProfiles(String filename, BST tree){ 
    File inputFile = new File(filename); 
    FileReader.readFileDate(inputFile, tree); 
    return tree; 
} 

public static void readFileDate(File inputFile, BST tree){ 
    Scanner in = null; 
    try 
    { 
     in = new Scanner(inputFile); 
    } 
    catch (FileNotFoundException e) 
    { 
     System.out.println("Cannot open file"); 
     System.exit(0); 
    } 

    FileReader.readLine(in, tree); 

} 
public static void readLine(Scanner in, BST tree) 
{ 
    while (in.hasNextLine()) 
    { 
     String line = in.nextLine(); 
     if(line.isEmpty()){ 
      continue; 
     } 
    Scanner lineScanner = new Scanner(line); 
    lineScanner.useDelimiter(",|;"); 
    FileReader.createProfile(lineScanner, tree); 
    } 
} 
public static Profile createProfile(Scanner lineScanner, BST tree) 
{ 

    String name = lineScanner.nextLine(); 
    int dd = lineScanner.nextInt(); 
    int mm = lineScanner.nextInt(); 
    int yyyy = lineScanner.nextInt(); 
    String town = lineScanner.nextLine(); 
    String country = lineScanner.nextLine(); 
    String nationality = lineScanner.nextLine(); 
    String interests = lineScanner.nextLine(); 

    Profile p = new Profile(name, dd, mm, yyyy, town, country, 
      nationality, interests); 
    tree.insertProfile(p); 
    return p; 
} 

} : 다음

내 파일 리더 클래스

이 문제를 해결하는 방법에 대한 제안 사항이 있으십니까? 감사합니다.

답변

0
String splitProfile[] = lineScanner.split(","); 

String fullName = splitProfile[0]; 
+1

어디에서 프로그램에 넣을까요? –

+0

프로필 작성 방법 –