2014-01-20 7 views
0

이 코드를 작성하여 파일을 읽은 다음 파일의 각 이름에 대해 표시를 요청합니다. 그리고 마크가 40 이상이면 pass와 below가 실패하고 각각의 이름을 해당 파일에 씁니다. 여기 while(namesFile.hasNext() 내 코드는 다음과 같습니다 :텍스트 파일 읽기 및 쓰기

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

public class TestResults { 
    public static void main(String[] args) { 
     String errs = ""; 
     Scanner k = new Scanner(System.in); 

     try { 
      try (
        Scanner namesFile = new Scanner(new File("Names.txt")); 

        PrintWriter passFile = new PrintWriter("Pass.txt"); 
        PrintWriter failFile = new PrintWriter("Fail.txt");) { 

       while (namesFile.hasNext()) { 
        try { 
         String tempLine = namesFile.nextLine(); 
         System.out.println("Please Enter Mark For " + tempLine + " : "); 
         int mark = k.nextInt(); 
         if (mark >= 40) { 
          passFile.println(tempLine + " " + mark + "%"); 
         } else { 
          failFile.println(tempLine + " " + mark); 
         } 
        } catch (InputMismatchException ime) { 
         String valueStr = namesFile.next(); 
         errs += "\n\t" + valueStr; 
        } finally { 
         namesFile.close(); 
         passFile.close(); 
         failFile.close(); 
        } 
       } 
      } 
     } // Checks to see if file is there. 
     catch (IOException ioe) { 
      System.out.println("ERROR: " + ioe.getMessage()); 
     } 
    } 
} 
+3

오류 메시지는 무엇을 말하는가하지만 라인 27에서 오류가? – Fildor

+2

왜 시도하고 있습니까? { – Rahul

+0

코드를 올바르게 작성하십시오. – Rahul

답변

1
public class TestResults { 

    public static void main(String[] args) { 
    Scanner namesFile; 
    PrintWriter passFile; 
    PrintWriter failFile; 


    String errs = ""; 
    Scanner k = new Scanner(System.in); 

    try { 
     try { 
       namesFile = new Scanner(new File("D:/Names.txt")); 

       passFile = new PrintWriter("D:/Pass.txt"); 
       failFile = new PrintWriter("D:/Fail.txt"); 



       try { 
        while (namesFile.hasNext()) { 
        String tempLine = namesFile.nextLine(); 
        System.out.println("Please Enter Mark For " + tempLine + " : "); 
        int mark = k.nextInt(); 
        if (mark >= 40) { 
         passFile.println(tempLine + " " + mark + "%"); 
        } else { 
         failFile.println(tempLine + " " + mark); 
        } 
        } 
       } catch (InputMismatchException ime) { 
        String valueStr = namesFile.next(); 
        errs += "\n\t" + valueStr; 
       } finally { 
        namesFile.close(); 
        passFile.close(); 
        failFile.close(); 
       } 



     } 
     catch(IOException ioe){ 
      System.out.println("ERROR: " + ioe.getMessage()); 
     } 



    } // Checks to see if file is there. 
    catch (Exception e) { 

    } 

    } 
} 
+0

개선 된 점은 무엇입니까? – Fildor

+0

SomeSickJoke의 코드에서 namesFile은 while()의 범위에 없습니다. 그것은 오류입니다. 그리고 그는 while 루프 안에 마지막 블록을 썼습니다. 파일을 단일 행으로 읽고 스트림을 닫습니다. 그래서 두 번째 줄을 읽을 수 없습니다. while 루프를 try 블록 안에 넣습니다. 그리고 제대로 작동합니다. –

+0

예. 그것은 "자원으로 시도"입니다. [여기] (http://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html)를 참조하십시오. – Fildor