开发者

Java: File reader, String separator by delimiters Help

开发者 https://www.devze.com 2023-02-24 10:20 出处:网络
I am trying to create a preProcessor in java where it will read in the source code. I tryed to read the code in all to one string.

I am trying to create a preProcessor in java where it will read in the source code. I tryed to read the code in all to one string.

Question: How do I add the strings in the middle of <<< >>> to its own array list of some sort.

public class processLines  {
public void pLine (String FileName)throws IOException{
    Scanner scanner = null;
    try{
        scanner = new Scanner(new BufferedReader(new FileReader(FileName)));
        while (scanner.hasNext()) {
            String Line = "";
            String LineB = "";
            String LineC = "";
            ArrayList<String> inside = new ArrayList<String>();
            Line = Line + scanner.next()+ " ";
            System.out.println("outside token: "+ Line);
            StringTokenizer token = new StringTokenizer(Line);
            while(token.hasMoreTokens()&& token.nextToken() != null){
                LineB = Line;
                if(LineB.contains("<<<")){
                    if(!LineB.contains(">>>") ){
                        LineC = LineC + scanner.next()+ " "; 
                        inside.add(LineC);
                        System.out.println("LineC: " + LineC);
                        System.out.print(inside);
                    }
                        if(scanner.next(">>>") != null){
                            Line =  scanner.next();
 开发者_如何学C                           System.out.println("Line INside:" + Line);
                        }
                }
            }
        }
    }
    finally {
        if (scanner != null) {
            scanner.close();
        }
    }
}

}

The text file sourceCode includes " Mo <<< Mo Larry Curly >>> Larry" all on one line. this code works if there is only one name in the middle of the <<< >>> but when I added more I get an error.

Error message that occurs: outside token: Mo

outside token: <<< LineC: Mo [Mo ]Exception in thread "main" java.util.InputMismatchException at java.util.Scanner.throwFor(Unknown Source) at java.util.Scanner.next(Unknown Source) at java.util.Scanner.next(Unknown Source) at processLines.pLine(processLines.java:26) at proProcess.main(proProcess.java:14)


The problem is that you're looking for the next token to be the closing wickets when that might not be the next token. I'd break the logic up so that you can read line by line from the input file. If a line has your wicket delimiters use a different scanner to split the content between the wickets. Here's the code. This works for zero to many tokens inside the wickets and lines w/o wickets.

public static void pLine (String FileName)throws IOException{
Scanner scanner = null;
try{
    scanner = new Scanner(new BufferedReader(new FileReader(FileName)));
    String line;
    ArrayList<String> inside;
    Scanner inner;
    int start;
    int end = 0;
    while (scanner.hasNextLine()) {
           line = scanner.nextLine();
           inside = new ArrayList<String>();
           start = line.indexOf("<<<", end);       
           end = line.indexOf(">>>", start+1);
           if (end > start) {
                inner = new Scanner(line.substring(start +3, end ));
                while (inner.hasNext()) {
                    inside.add(inner.next());
                }
            }
            System.out.println("inside : " + inside);
        }
    }
    catch (Throwable t) {
        t.printStackTrace();
    }
    finally {
        scanner.close();
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号