2012-03-01 5 views
1

私は基本的に、XMLテキスト文書が正しくネストされているかどうかをテストするプログラムを実行する必要があります。私はこれが実際にこのセメスターの私たちのより簡単な課題の1つだと考えましたが、私が問題を抱えているのは、テキストファイルを1行ずつ読むことです。教授が私たちに提供したFileInputStreamのコードを実装しました。そこから問題が発生しています。基本的に、私のコードは、テキストファイルの最初の行を検証し、終了します。私はFileInputStreamに何か間違っている必要があります。どんな助けもありがとうございます。プログラムの助けが必要です.FileInputStreamがテキストファイルの1行だけを読み込むのに問題があります。 [HomeworkHelp]

// The XMLParser class prompts the user for a filename and reads an XML 
// document from the given text file. The program then reports that either the 
// document is valid based on XML rules or there was an error at a certain 
// line of input due to a violation of rules of XML tags. 

import java.io.*; 
import java.util.Scanner; 
import java.util.Stack; 
import java.util.StringTokenizer; 

public class XMLParser {   
public static void main(String[] args) throws IOException{ 

    //creates a scanner to read the users input of the file name 
    Scanner input = new Scanner(System.in); 

    //creates an integer that will be incremented for every line read 
    //to help with error reporting 
    int currentLine = 1; 

    //creates a stack that elements will be pushed on to 
    //and popped off of, strings to represent opening and closing 
    //tags of an xml document, and a string to represent the root 
    //tag (the first tag used) 
    Stack<String> stack = new Stack<String>(); 
    String open = "<"; 
    String close = "</"; 
    String root = ""; 


    //prompts the user to input a file name then reads from 
    // the file the user entered 
    System.out.println("Enter the name of the file to be read: "); 
    String fileName = input.next(); 
    FileInputStream fis = new FileInputStream(fileName); 
    InputStreamReader inStream = new InputStreamReader(fis); 
    BufferedReader stdin = new BufferedReader(inStream); 
    String data = stdin.readLine(); 

    String nextDataValue; 
    StringTokenizer tokenizer = new StringTokenizer(data); 

    while (tokenizer.hasMoreTokens()) { 
     nextDataValue = tokenizer.nextToken().trim(); 

     //if the xml document begins with anything other than 
     //a root tag, an error is reported and the program ends 
     if (root.equals("") && !nextDataValue.startsWith(open)) { 
      System.out.println("PARSE ERROR Line 1, your XML document does not start with an opening tag and is therefore invalid\nProgram terminating normally..."); 
      break; 
     } 

     //pushes an "opening tag" onto the stack 
     else if (nextDataValue.startsWith(open) && !nextDataValue.startsWith(close)) { 
      stack.push(nextDataValue); 

      //checks if there is already a root value, and if there 
      //is not it adds one 
      if (root.equals("")) { 
       root = root + nextDataValue; 
      } 

     } 

     //if a closing tag is found, this pops the stack and compares 
     //the closing tag with an opening tag to see if it is properly 
     //nested, if not an error statement is printed and the program 
     //ends 
     if (nextDataValue.startsWith(close)) { 
      String compareClose = nextDataValue; 
      String compareOpen = stack.pop(); 
      compareClose = compareClose.replace("/", ""); 
      if (!compareClose.equals(compareOpen)) { 
       System.out.println("PARSE ERROR Line " + currentLine + ", you have improperly nested the tag: " + compareOpen); 
       break; 
      } 
      //if the closing tag is the root tag and there is nothing 
      //after this closing tag, the document is valid. 
      if (compareOpen.equals(root) && !tokenizer.hasMoreTokens()) { 
       System.out.println("Input XML document is valid"); 
      } 
     }  


     currentLine++; 
    } 




} 
} 

tl; dr:私のFileInputStreamが1行のコードを通過してから停止するのはなぜですか?私はwhileループで何か間違ったことをしたのですか、それとも私が気付いていないものですか?

ご協力いただきありがとうございます!

+0

'Stringデータ= stdin.readLine()のように空の要素に気づく必要があり、言ったように、'これがすべきループに入る。そうでなければ、たった1行しか読み込めません。 – Ved

答えて

4

問題は次の行内にあります。String data = stdin.readLine();。あなたがそれをやっているところでは、プログラムは1行だけを読み込み、それを検証して終了するよう指示します。これは、あなたが反復ごとのテキストの新しい行をロードすることができます

String data = ""; 
while ((data = stdin.readLine()) != null) 
{ 
    //Read and validate the line you are reading 
} 

:何をする必要があるとこれに似たwhileループにあります。 EOFに遭遇すると、stdin.readLine()はnullを返し、ループを壊してプログラムの実行を停止させます。

0

行を1度だけ読み込みます。ループの前。

0

あなたはreadLineを繰り返し呼び出す必要があります。 1つのループで2つ以上の行を読み取ることができます。

1

彼らはあなただけBufferedReaderの

から1行を読んで、あなたはまた、このような

<emptyElement /> 

String open = "<"; 
String close = "</"; 
String close1 = "/>" 
関連する問題