简体   繁体   English

字符串标记器和Scanner Java

[英]String tokenizer and Scanner java

I have a large text file which I want to separate into different strings using the delimiter !- (Each string is multiple lines). 我有一个很大的文本文件,我想使用定界符!-分隔成不同的字符串(每个字符串都是多行)。

I then want to discard all the other strings that do not contain: 然后,我想丢弃所有其他不包含的字符串:

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

So far I've got this and its not outputting anything (it complies but no output to console).I'm new to programming and I'm not making much progress after researching this for sometime so any suggestions or pointers would be most appreciated thanks! 到目前为止,我已经做到了这一点,它没有输出任何内容(它合规但没有输出到控制台)。我是编程新手,经过一段时间的研究后我没有取得太大进展,所以任何建议或指针将不胜感激谢谢!

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

You should consider reading the java doc page to StringTokenizer : http://docs.oracle.com/javase/7/docs/api/java/util/StringTokenizer.html 您应该考虑将Java doc页面阅读到StringTokenizerhttp : //docs.oracle.com/javase/7/docs/api/java/util/StringTokenizer.html

There two different problems in your code: 您的代码中存在两个不同的问题:

  1. Because your desired strings are more than one line out of a file, you first have to add them up (into a String ) and then work with it via StringTokenizer to separate again. 由于所需的字符串超出文件的一行,因此您首先必须将它们加起来(放入String ),然后通过StringTokenizer与之一起使用以再次分离。
  2. You have to compare the StringTokenizer.nextToken() to your check String not the whole StringTokenizer . 您必须将StringTokenizer.nextToken()与您的检查String而不是整个StringTokenizer StringTokenizer.nextToken() then gives you the next String separated through !- . 然后, StringTokenizer.nextToken()为您提供下一个由!-分隔的String。

The following code should work: 下面的代码应该工作:

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

remove the if condition and add the below while condition 删除if条件并添加以下while条件

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

because stringtokenizer splits the text based on token. 因为stringtokenizer会根据标记拆分文本。 so your if never becomes true. 所以你的话永远不会成真。

try this........... 尝试这个...........

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

you comparing string with stringtokenizer object not value.... 您将字符串与stringtokenizer对象而不是值进行比较。...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM