سؤال

The problem is it finds the key word "dodge" creates the file but doesn't write to it. I had this problem earlier and I flushing then closing the file fixed it, but that did not work this time.

Also WriteToFile(CurrentLine[ReturnWordsIndex("using")-1]); towards the bottom errors out and says ArrayIndexOutOfBoundsException: -2 I'm not sure why because "using" should never be found at position -1.

import java.io.*;    
import javax.swing.JOptionPane;

public class InputLog {
    private BufferedReader CombatLog;
    private PrintWriter CharacterFile;
    private String[] CurrentLine;

    InputLog(){
        try{
            CombatLog = new BufferedReader(new FileReader("127401175162_chatlog.txt"));
            do{
                try{
                    CurrentLine = CombatLog.readLine().split(" ");
                }
                catch(IOException e){
                    JOptionPane.showMessageDialog(null, "Sorry cannot open UserSettings File");
                }
                DetermineType();
                }while(CombatLog.ready());
            CharacterFile.close();
        }
        catch(IOException e){
            JOptionPane.showMessageDialog(null, "Could not open next line of combatlog " + e);
        }
    }
    public void WriteToFile(String S){
        try{
            CharacterFile = new PrintWriter(new File(CurrentLine[3]));
        }
        catch(IOException e){
        JOptionPane.showMessageDialog(null, "Sorry can't write " + CurrentLine[3] +" To file");
        }
        CharacterFile.flush();
        CharacterFile.print(S+ " ");
    }

    public void WriteToFileLn(String S){
        try{
            CharacterFile = new PrintWriter(new File(CurrentLine[3]));
        }
        catch(IOException e){
            JOptionPane.showMessageDialog(null, "Sorry can't write " + CurrentLine[3] +" To file");
        }
        CharacterFile.flush();
        CharacterFile.println(S+ " ");
    }


    public int ReturnWordsIndex(String S){
        for(int i=0;i<CurrentLine.length;i++){
            if(CurrentLine[i].equals(S))
                return i;
        }
        return -1;
    }

    private void DetermineType(){
        for(String A: CurrentLine)
            if(A.equals("attacks")){
                JOptionPane.showMessageDialog(null, "found dodge");
                WriteToFile(CurrentLine[2]);
                WriteToFile(CurrentLine[ReturnWordsIndex("using")-1]);
                WriteToFile("(dodge).");
                WriteToFileLn(CurrentLine[ReturnWordsIndex("attacks")-1]);
            }
    }


    public static void main(String args[]){
        new InputLog();
    }
}

Thanks, Macaire

Edit: I found out that i am simply writing one string, creating a new file, then writing another character. How can i not delete the file, and just rewrite to it?

هل كانت مفيدة؟

المحلول

How can i not delete the file, and just rewrite to it?

I assume that you mean append to it. (What you are currently doing is rewriting it.)

If so, just use FileWriter(file, true) to create a Writer that will start writing at the current end of the file; e.g.

CharacterFile = new PrintWriter(new FileWriter(new File(CurrentLine[3])));

Note: it is bad style for a Java variable or method name to start with an upper-case letter.

نصائح أخرى

WriteToFile(CurrentLine[ReturnWordsIndex("using")-1]); causes an ArrayIndexOutOfBoundsException: -2 because the word "using" is not found (so ReturnWordsIndex("using") returns -1).

You need an if/then statement so that you don't try to index CurrentLine if the word wasn't found.

In response to your edit, open the file outside of the WriteToFile and WriteToFileLn functions.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top