ppmk22
Goto Top

Textdatei parsen

Hallo Forum

Ich versuche mich gerade an folgendem Problem.

Ich lese eine txt ein.

Inhalt sieht in etwas so aus

text text text text
text text
15448 text text


X1|---1---3---2---1|---1---3---2---1|---1---3---2---1|---1---3---2---1|
X2|2--2---3---2-3-1|---1-1-3---2---1|-2-1---3-2-2---1|---1---3---2---1|
X3|-1-1---3---2---1|---1---3---2---1|---1---3---2---1|---1-2-1---2---1|

text text

X1|---1---3---2---1|---1---3---2---1|---1---3---2---1|---1---3---2---1|
X3|---1---3---2---1|---1---3---2---1|---1---3---2---1|---1---3---2---1|

text text

X2|2--2---3---2-3-1|---1-1-3---2---1|
X3|-1-1---3---2---1|---1---3---2---1|


usw.

Daraus soll eine ArrayList werden, das sich so zusammen setzt.
aus - soll 0 werden
aus 1 soll a werden
aus 2 soll b werden
aus 3 soll c werden

Es soll jeweils das fortlaufende Zeichen, ab dem ersten | , aus den reihen in die Liste gepackt werden.
Beispiel:
Reihe X1 erste Zeichen nach dem | ist "-"
Reihe X2 erste Zeichen nach dem | ist "2"
Reihe X3 erste Zeichen nach dem | ist "-"
Reihe X1 zweite Zeichen nach dem | ist "-"
Reihe X2 zweite Zeichen nach dem | ist "-"
Reihe X3 zweite Zeichen nach dem | ist "1"
usw.

0,b,0
[1] 0,0,a
[2] 0,0,0

nun ist im zweiten Block kein X2, dies soll aber trotzdem mit "0" gefüllt werden

Ich weiss nicht mehr weiter und auch schon nicht was ich alles probiert habe.

Hier ist mein letzter Stand der Dinge.

package filereader;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.StringTokenizer;

public class Main {

    public static void main(String args) {


        String file = "H:\\test.txt";  

        String zeile = null;
        try {
            BufferedReader in = new BufferedReader(
                    new FileReader(file));
            try {
                while ((zeile = in.readLine()) != null) {

                    StringTokenizer stringT = new StringTokenizer(zeile, "");  

                    while (stringT.hasMoreTokens()) {
                        String token = stringT.nextToken().trim();

                            if (token.startsWith("X1|"))  
                            {
                               test("X1", token.substring(3));  
                            }
                            if (token.startsWith("X2|"))  
                            {
                                test("X2",token.substring(3));  
                            }
                            if (token.startsWith("X3|"))  
                            {
                                test("X3",token.substring(3));  
                            }
                         }
                    }
                
                in.close();
            } catch (IOException e) {
                System.out.println("Read error " + e);  
            }
        } catch (IOException e) {
            System.out.println("Open error " + e);  
        }
    }



    private static void test(String Sound,String token)
    {
        int i = 0;
        System.out.println(Sound + " ");  
        StringTokenizer sT = new StringTokenizer(token, "|");  
        // System.out.println(sT);
        while (sT.hasMoreTokens()) {
            i++;
            token = sT.nextToken().trim();
//            System.out.println(sT.countTokens() + " " + token); 
            System.out.println(i + " " + token);  


            
            if(sT.hasMoreElements() == false){

                
                
            }
         }
        
    }


}

Bin für alle Vorschläge dankbar

LG
PP

Content-Key: 136461

Url: https://administrator.de/contentid/136461

Printed on: April 18, 2024 at 09:04 o'clock

Member: dog
dog Feb 21, 2010 at 02:19:44 (UTC)
Goto Top
Mit Java kenne ich mich nicht aus, aber in PHP in etwa so:
<?php

	function convstr($str)
	{
		$conv = array();
		for ($i=0; $i < 16; $i++) { 
			switch (@$str[$i]) {
				case '1':  
					$conv = 'a';  
					break;
				case '2':  
					$conv = 'b';  
					break;
				case '3':  
					$conv = 'c';  
					break;
				default:
					$conv = 0;
					break;
			}
		}
		return $conv;
	}
	
	function fill(&$array,$n,$lowerKey,$upperKey)
	{
		for ($i=$lowerKey; $i <= $upperKey; $i++) {
			$nullArr = convstr('----------------');  
			$array[$n][$i] = array_merge($nullArr,$nullArr,$nullArr,$nullArr);
		}
	}

	$in = <<<STR
text text text text
text text
15448 text text


X1|---1---3---2---1|---1---3---2---1|---1---3---2---1|---1---3---2---1|
X2|2--2---3---2-3-1|---1-1-3---2---1|-2-1---3-2-2---1|---1---3---2---1|
X3|-1-1---3---2---1|---1---3---2---1|---1---3---2---1|---1-2-1---2---1|

text text

X1|---1---3---2---1|---1---3---2---1|---1---3---2---1|---1---3---2---1|
X3|---1---3---2---1|---1---3---2---1|---1---3---2---1|---1---3---2---1|

text text

X2|2--2---3---2-3-1|---1-1-3---2---1|
X3|-1-1---3---2---1|---1---3---2---1|


usw.
STR;

	preg_match_all('/X([1-3])\|(?:([\-123]{16})(?:\|([\-123]{16})(?:\|([\-123]{16}))?(?:\|([\-123]{16}))?)?)\|/', $in, $match);  
		
	$buildRes = array();
	
	$lastX = 0;
	$n = 0;
	for ($i=0; $i < count($match); $i++) { 
		$x = $match[1][$i];
		if($x < $lastX)
			$n++;
		if($lastX > 0 && ($lastX+1) != $x)
			fill($buildRes,$n,$lastX,$x);
		$buildRes[$n][$x] = array_merge(convstr($match[2][$i]),convstr($match[3][$i]),convstr($match[4][$i]),convstr($match[5][$i]));
		$lastX = $x;
	}
?>

Array
(
     => Array
        (
            [1] => Array
                (
                     => 0
                    [1] => 0
                    [2] => 0
                    [3] => a
                    [4] => 0
                    [5] => 0
  <--- snip --->
                    [63] => 0
                )

        )

)

Das ganze wieder rückwärts über:
	function restr($key)
	{
		if($key === "a")  
			return '1';  
		if($key === "b")  
			return '2';  
		if($key === "c")  
			return '3';  
		return '-';  
	}
	
	
	
	function rebuild(&$array)
	{
		foreach ($array as $block => $xs) {
			foreach ($xs as $x => $letters) {
				echo 'X'.$x;  
				for ($i=0; $i < count($letters); $i++) { 
					if($i % 16 == 0)
						echo '|';  
					echo restr($letters[$i]);
				}
				echo "|\n";  
			}
			echo "\n\n";  
		}
	}
	
	rebuild($buildRes);

ergibt:

X1|---1---3---2---1|---1---3---2---1|---1---3---2---1|---1---3---2---1|
X2|2--2---3---2-3-1|---1-1-3---2---1|-2-1---3-2-2---1|---1---3---2---1|
X3|-1-1---3---2---1|---1---3---2---1|---1---3---2---1|---1-2-1---2---1|


X1|----------------|----------------|----------------|----------------|
X2|----------------|----------------|----------------|----------------|
X3|---1---3---2---1|---1---3---2---1|---1---3---2---1|---1---3---2---1|


X2|2--2---3---2-3-1|---1-1-3---2---1|----------------|----------------|
X3|-1-1---3---2---1|---1---3---2---1|----------------|----------------|

Du siehst, ein paar Dinge musst du noch anpassen face-wink