【问题标题】:String tokenizer and Scanner java字符串标记器和扫描器 java
【发布时间】:2014-04-23 09:36:05
【问题描述】:

我有一个大文本文件,我想使用分隔符将它分成不同的字符串!-(每个字符串都是多行)。

然后我想丢弃所有其他不包含的字符串:

===========  ALL OBJECTS IN CLASS: FENESTRATIONSURFACE:DETAILED ===========

到目前为止,我已经得到了这个并且它没有输出任何东西(它符合但没有输出到控制台)。我是编程新手,在研究了一段时间后我没有取得太大进展,所以任何建议或指针都会非常感谢谢谢!

public class Main {
    public static void main(String[] args) throws FileNotFoundException {
        File file = new File("C:/Users/Anton/Pictures/1 x geotransform0.5m shading.txt");
        Scanner scan = new Scanner(file);
        while(scan.hasNextLine()){
            StringTokenizer st = new StringTokenizer(scan.nextLine(),"!-");
            if(st.equals("   ===========  ALL OBJECTS IN CLASS: FENESTRATIONSURFACE:DETAILED ===========")) {
                System.out.print(st);
            }
        }
        scan.close();
    }
}

【问题讨论】:

    标签: java java.util.scanner stringtokenizer


    【解决方案1】:

    你应该考虑阅读java doc页面到StringTokenizer:http://docs.oracle.com/javase/7/docs/api/java/util/StringTokenizer.html

    您的代码中有两个不同的问题:

    1. 由于您想要的字符串不止一行,因此您首先必须将它们相加(到String),然后通过StringTokenizer 使用它以再次分离。
    2. 您必须将StringTokenizer.nextToken() 与您的支票String 进行比较,而不是整个StringTokenizerStringTokenizer.nextToken() 然后为您提供通过 !- 分隔的下一个字符串。

    以下代码应该可以工作:

    public class Main {
    public static void main(String[] args) throws FileNotFoundException {
        File file = new File("C:/Users/Anton/Pictures/1 x geotransform0.5m shading.txt");
        Scanner scan = new Scanner(file);
        //Scanning
        //first scan the whole file into a string (because a sting can have more than one line)
        String temp = "";
        while(scan.hasNextLine()){
            temp = scan.nextLine();
        }
        //now add the string to tokeinzer
        StringTokenizer st = new StringTokenizer(temp,"!-");
        //now give output
        while(st.hasMoreTokens()){
        String temp2 = st.nextToken();
            if(temp2.equals("   ===========  ALL OBJECTS IN CLASS: FENESTRATIONSURFACE:DETAILED ===========")) {
                System.out.print(temp2);
            }
        }
        scan.close();
    }
    }
    }
    

    【讨论】:

    • Robert 首先感谢您的回答,这部分有效,主要是您忘记在第一个 while 循环中输入 temp = temp + scan.nextLine() 来更新 temp 的值。一旦解决了这个问题,它只会输出“ =========== ALL OBJECTS IN CLASS: FENESTRATIONSURFACE:DETAILED ===========" 我想要做的是输出令牌包含这个。有什么建议吗?
    【解决方案2】:

    删除 if 条件并添加以下 while 条件

     while (st.hasMoreElements()) {
                            System.out.println(st.nextElement());
                        }
    

    因为 stringtokenizer 根据 token 分割文本。所以你的 if 永远不会成为真的。

    【讨论】:

      【解决方案3】:

      试试这个............

      while(scan.hasNextLine()){
                      StringTokenizer st = new StringTokenizer(scan.nextLine(),"!-");
                      while(st.hasMoreTokens()){
                          String stt = st.nextElement().toString();
                          if(stt.equals("===========  ALL OBJECTS IN CLASS: FENESTRATIONSURFACE:DETAILED ===========")) {
                              System.out.print(stt);
                          }
                      }
                  }
      

      您将字符串与 stringtokenizer 对象而不是值进行比较......

      【讨论】:

        猜你喜欢
        • 2016-08-03
        • 2014-10-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-11-27
        相关资源
        最近更新 更多