Problem with seperating one String into two arrays (words seperated by Tab and Enter)

Viewed 40

I am trying to make a program that splits one string of text into to arrays the input in the String text looks as following:

voc1    deut1
voc2    deut2
voc3    deut3
voc4    deut4

the 2 seperate String[] arrays shoud be filled as following:

splits[0]="voc1"
splits[1]="voc2"
splits[2]="voc3"
splits[3]="voc4"

lines[0]="deut1"
lines[1]="deut2"
lines[2]="deut3"
lines[3]="deut4"

The first of both work perfectly [0], but from then on it collapses into a horrible mess like [1]:

splits[1]: deut2
voc3
lines[1]: voc2  deut2

I have tried everything my basic understanding of java allows me to do but it never has worked. maybe you can help me

If you have any idea to make it work, or simplify the code... feel free to comment!

(I am sorry for any Spelling mistakes)

public class raplacer {
    public static void main(String[] args) {
        

        String text= "voc1  deut1\nvoc2 deut2\nvoc3 deut3\nvoc4 deut4";
        
        String[] splits = null;
        String[] lines = null;
        int numbSplits =0;
        int numbLines =0;
        
        for(int i=0; i<3;i++) {
            System.out.print("nextLine:  " + text.indexOf("\n"));
            System.out.println("    nextTab:   " + text.indexOf("   "));
            
            if(text.indexOf("\n")>text.indexOf("    ")) {
                splits=text.split(" ");
                splits[numbSplits+1]="";
                text=text.replaceFirst(splits[numbSplits], "");
                System.out.println("Text:\n" + text);
                text=text.replaceFirst("    ", "");
                System.out.println("Text:\n" + text);
                numbSplits++;
                System.out.println("Splitet a Tab away");
            }
            else if(text.indexOf("\n")<text.indexOf("   ")) {
                lines=text.split("\n");
                lines[numbLines+1]="";
                text=text.replaceFirst(lines[numbLines], "");
                System.out.println("Text:\n" + text);
                text=text.replaceFirst("\n", "");
                System.out.println("Text:\n" + text);
                numbLines++;
                System.out.println("Splited a Line away");
            }
        
        }
        
        System.out.println("Text:\n" + text);
        System.out.println();
        System.out.println("splits0: " + splits[0]);
        System.out.println("splits1: " + splits[1]);
        System.out.println();
        System.out.println("lines0: " + lines[0]);
        System.out.println("lines1: " + lines[1]);
        
    }
}
0 Answers
Related