2

我目前正在编写一个小程序,它从一个简单的文件中获取文本,并将其写入另一个文件。输入文件有点像“madlibs”类型的交易,带有我需要放置的令牌,如“”和“”。

文本:

> One of the most <adjective> characters in fiction is named "Tarzan of the <plural-noun>." Tarzan was raised by a/an <noun> and lives in the <adjective> jungle in the heart of darkest <place>.

代码:

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

public class Story {

public static void main(String[] args) throws FileNotFoundException {

    Scanner sc = new Scanner(System.in);
    System.out.print("Input file name? ");
    String infileName = sc.next();
    Scanner input = new Scanner(new File(infileName));

    System.out.print("Output file name? ");
    String outfileName = sc.next();
    PrintStream out = new PrintStream(new File(outfileName));

    while (input.hasNext()){

        String current = input.next();
        System.out.println(current);
        int openIndex = current.indexOf("<");
        int closeIndex = current.indexOf(">");
        current = current.substring(openIndex + 1, closeIndex - openIndex);
        System.out.println(current);

        if (current.equals("adjective")){
            System.out.print("Please enter an adjective: ");
            current = sc.next();
            out.print(current);
            out.print(" ");
        } else if (current.equals("plural-noun")){
            System.out.print("Please enter a plural noun: ");
            current = sc.next();
            out.print(current);
            out.print(" ");
        } else if (current.equals("noun")){
            System.out.print("Please enter a noun: ");
            current = sc.next();
            out.print(current);
            out.print(" ");
        } else if (current.equals("place")){
            System.out.print("Please enter a place: ");
            current = sc.next(); 
            out.print(current);
            out.print(" ");
        } else {
            out.print(current);
            out.print(" ");
        }
    }
}
}

这是我似乎遇到的问题:只有用户输入的单词被打印到“out1.txt”或其他任何内容中,并且每个单词之间有许多空格。

任何帮助表示赞赏,感谢男孩和女孩!

4

2 回答 2

1

问题来自普通词,即不是“<variables>”:

int openIndex = current.indexOf( "<" );    // returns -1
int closeIndex = current.indexOf( ">" );   // returns -1 too
current = current.substring( openIndex + 1, closeIndex - openIndex );
// at this place for a normal word current is empty

你必须添加一些“如果”

String current = input.next();
int openIndex = current.indexOf( "<" );
if( openIndex > -1 )
{
    int closeIndex = current.indexOf( ">" );
    current = current.substring( openIndex + 1, closeIndex - openIndex );
}
System.out.println( current );

另一个问题来自令牌 [."](在 [] 内),您的代码仅采用 <> 和 . 内的字符。" 丢失了。

String current      = input.next();
String cstStrBefore = "";
String cstStrAfter  = "";
int openIndex = current.indexOf( "<" );
boolean var = ( openIndex > -1 );
if( var )
{
   int closeIndex = current.indexOf( ">" );
   cstStrBefore = current.substring( 0, openIndex );
   cstStrAfter  = current.substring( closeIndex + 1 );
   current      = current.substring( openIndex + 1, closeIndex - openIndex );
}

这是我使用的整个 Java 7 代码:

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintStream;
import java.util.Scanner;

public class Story {
   public static void main( String[] args ) throws FileNotFoundException {
      Scanner     sc    = new Scanner( System.in );
      Scanner     input = new Scanner( new File( "Story.txt" ));
      PrintStream out   = new PrintStream( "Story-out.txt" );
      while( input.hasNext()) {
         String current      = input.next();
         String cstStrBefore = "";
         String cstStrAfter  = "";
         int openIndex = current.indexOf( "<" );
         boolean var = ( openIndex > -1 );
         if( var ) {
            int closeIndex = current.indexOf( ">" );
            cstStrBefore = current.substring( 0, openIndex );
            cstStrAfter  = current.substring( closeIndex + 1 );
            current      =
               current.substring( openIndex + 1, closeIndex - openIndex );
         }
         System.out.println( current );
         switch( current ) {
         case "adjective"  : System.out.print( "Please enter an adjective: " ); break;
         case "plural-noun": System.out.print( "Please enter a plural noun: " ); break;
         case "noun"       : System.out.print( "Please enter a noun: " ); break;
         case "place"      : System.out.print( "Please enter a place: " ); break;
         }
         if( var ) {
            current = sc.next();
         }
         out.print( cstStrBefore + current + cstStrAfter + ' ' );
      }
      sc.close();
      input.close();
      out.close();
   }
}

我建议你使用 Netbeans 或 Eclipse 来开发。

于 2012-10-31T17:07:38.720 回答
1

在使用后关闭任何打开的流也是一种很好的做法。(尽管 JVM 可能会处理这个问题)

out.close(); 输入.close();sc.close();

在您的时间之外{}

于 2012-10-31T17:13:16.530 回答