gpt4 book ai didi

java - 文件 I/O - Java

转载 作者:行者123 更新时间:2023-11-30 09:30:42 25 4
gpt4 key购买 nike

我目前正在编写一个小程序,它从一个简单的文件中获取文本,并将其写入另一个文件。输入文件有点像“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”或其他文件中,并且每个单词之间有许多空格。

非常感谢任何帮助,谢谢小伙伴们!

最佳答案

问题来自于普通词,即不是“ ”:

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 );

另一个问题来自 token [."](在 [] 内),您的代码只使用 <> 和 ."内的字符。迷路了。

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来开发。

关于java - 文件 I/O - Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13162873/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com