Java:文件读取器、按定界符分隔的字符串分隔符帮助

发布于 2024-10-31 18:21:16 字数 1986 浏览 1 评论 0原文

我正在尝试在 java 中创建一个预处理器,它将在源代码中读取。我尝试将代码全部读取为一个字符串。

问题:如何在 <<< 中间添加字符串>>>>>到它自己的某种数组列表。

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();
                            System.out.println("Line INside:" + Line);
                        }
                }
            }
        }
    }
    finally {
        if (scanner != null) {
            scanner.close();
        }
    }
}

文本

文件源代码在一行中包含“Mo <<< Mo Larry Curly>>Larry”。如果 <<< 中间只有一个名称,则此代码有效。 >>>>>但是当我添加更多时,我收到错误。

出现的错误消息:外部令牌:Mo

外部标记:<<< C线:钼 [Mo ]线程“main”中出现异常 java.util.InputMismatchException 在 java.util.Scanner.throwFor(来源未知) 在 java.util.Scanner.next(来源未知) 在 java.util.Scanner.next(来源未知) 在 processLines.pLine(processLines.java:26) 在 proProcess.main(proProcess.java:14)

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

沙沙粒小 2024-11-07 18:21:16

问题是,您正在寻找下一个标记作为关闭三柱门,而这可能不是下一个标记。我会分解逻辑,以便您可以从输入文件中逐行读取。如果某行具有 wicket 分隔符,请使用不同的扫描仪在 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();
    }
}

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();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文