我目前正在编写一个小程序,它从一个简单的文件中获取文本,并将其写入另一个文件。输入文件有点像“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”或其他内容中,并且每个单词之间都有多个空格。
感谢任何帮助,谢谢你们!
答案 0 :(得分:1)
问题来自普通词,即不是“&lt; variables&gt;”:
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
你必须添加一些“if”
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 );
另一个问题是令牌[。“](在[]内),你的代码只包含&lt;&gt;和。”中的字符。“迷路了。
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进行开发。
答案 1 :(得分:1)
在使用后关闭任何开放流也是一种很好的做法。 (虽然JVM可能会解决这个问题)
out.close(); input.close(); sc.close();
在你身边{}