2013-11-05 1 views
0

그래서 내 프로그램을 수정했지만 문제는 모든 빈 공간을 물결표로 바꾸면 텍스트가 닫힌 파일로 출력되어야한다는 것입니다. 출력을 위해 파일을 다시 열고 어떻게 입력합니까?닫힌 스트림으로 출력 하시겠습니까?

//Name: Allen Li 
//Program file: Vowels.Java 
//Purpose: Using File IO, read a file's input and output this text to a new text file 
//When outputting, all blank spaces will be changed to tildes and there will be a count of each vowel(AEIOU) 


import java.util.Scanner; //input 
import java.io.File; //IO 
import java.io.IOException; //IO exception class 
import java.io.FileWriter; //file output 
import java.io.FileReader; //file input 
import java.io.FileNotFoundException; //if file isnt found, file not found class 

public class Vowels { //class 
public static void main(String[] args) { //main method 

    try { //try block 
    FileReader poetry = new FileReader("poetry.txt"); 
    FileWriter dentist = new FileWriter( 
    "LI_ALLEN_dentist.txt"); 

    int a; 
    while ((a = poetry.read()) != -1) { 
    dentist.write(a); 
    System.out.print((char) a); //print the file to the monitor 
    } 

    poetry.close(); 
    dentist.close(); 

    Scanner inFile = new Scanner(new File( 
    "LI_ALLEN_dentist.txt")); 

    int numOfVowelsA = 0; //count #s of A/E/I/O/U vowels 
    int numOfVowelsE = 0; 
    int numOfVowelsI = 0; 
    int numOfVowelsO = 0; 
    int numOfVowelsU = 0; 

    while (inFile.hasNext()) { 
    String sentence = inFile.next() /* ("\\S+") */; 

    for (int i = 0; i <= sentence.length() - 1; i++) { 
    if (sentence.toLowerCase().charAt(i) == 'a') { 
     numOfVowelsA++; 
    } 
    if (sentence.toLowerCase().charAt(i) == 'e') { 
     numOfVowelsE++; 
    } 
    if (sentence.toLowerCase().charAt(i) == 'i') { 
     numOfVowelsI++; 
    } 
    if (sentence.toLowerCase().charAt(i) == 'o') { 
     numOfVowelsO++; 
    } 
    if (sentence.toLowerCase().charAt(i) == 'u') { 
     numOfVowelsU++; 
    } 
    } 
    } 
    System.out.println(); 

    System.out.println("There are " + numOfVowelsA 
    + " A vowels in this file of text"); 
    System.out.println("There are " + numOfVowelsE 
    + " E vowels in this file of text."); 
    System.out.println("There are " + numOfVowelsI 
    + " I vowels in this file of text."); 
    System.out.println("There are " + numOfVowelsO 
    + " O vowels in this file of text."); 
    System.out.println("There are " + numOfVowelsU 
    + " U vowels in this file of text. "); 



    Scanner tildes = new Scanner(new File( 
    "LI_ALLEN_dentist.txt")); 
    while (tildes.hasNext()) { 
    String replace = tildes.nextLine(); 
    replace = replace.replaceAll(" ", "~"); 
    System.out.println(); 
    System.out.print(replace); 

    } 

    } catch (FileNotFoundException i) { 
    System.out.println("The file you are trying to use as input is not found. " + i); 
    } catch (IOException i) { 
    System.out.println("There is an issue with the input or output file. " + i); 
    } 
} 
} 
+0

스트림을 올바른 값으로 채우면 스트림을 닫지 않는 이유는 무엇입니까? –

+0

나는 dentist.close();를 움직여 보았습니다. 두 번째 while 루프는 catch 예외 이전에 끝납니다. 어떤 이유에서, 그 라인을 끝까지 움직이면 블록 if 블록을 막아 버리고 라인의 모음은 전혀 계산되지 않고 모든 값은 0으로 끝납니다. @LuiggiMendoza – allenlistar

답변

0

inFile을 닫지 않았습니다. 먼저 inFile을 닫은 다음 tildes에서 다시 열 수 있습니다. 줄 앞에 닫으십시오.

+0

infile을 닫으면 평가 만 가능합니다 파일의 텍스트 행을 평가하는 작업을 마칩니다. 또한 inFIle을 닫으면 문장을 평가하지 않습니다. – allenlistar

+0

나는 당신이하고 싶은 말을 이해하지 못한다 .. @allenlistar –

+0

infile을 닫으면 파일 자체에 아무 것도 쓰지 않고 모음의 수를 세는 방법이 없다. – allenlistar

관련 문제