简体   繁体   English

如何在Java中读取以空格分隔的字符串和数字?

[英]How to read strings and numbers separated by space in Java?

I've been working on a project in Java and I need to read a text file that looks like this 我一直在用Java开发一个项目,我需要阅读一个看起来像这样的文本文件

1 1 'Who is Albert Einstein?' 1 1“谁是爱因斯坦?” 'commander' 'physicist' 'doctor' 1 I need to take the values separately , for example id=1,type=1,question=Who is Albert Einstein,answer1=commander etc. '指挥官''物理学家''医生'1我需要分别取值,例如id = 1,type = 1,question =谁是Albert Einstein,answer1 =指挥官等。

Is there some way I can separate them by space and keep the strings between apostrophes as a whole? 我是否可以通过某种方式将它们按空格分开并将字符串整体放在撇号之间?

This is hard to do as a standard string split will not understand not to split anything inside the quotes. 这很难做到,因为标准的字符串拆分将不理解不对引号内的任何内容进行拆分。

You could write your own manual split easily enough, loop through, flip an "inQuote" flag every time you find a quote. 您可以轻松地编写自己的手动拆分,遍历,每次找到报价时翻转“ inQuote”标志。 Split on spaces whenever you find a space and the flag is not set. 只要找到空格并且未设置标志,就在空格上分割。

This should work: 这应该工作:

try {
    BufferedReader reader = new BufferedReader(new FileReader("the-file-name.txt"));
    ArrayList<ArrayList<String>> lines = new ArrayList<ArrayList<String>>();
    String line = reader.readLine();
    while(line != null) {
        ArrayList<String> values = new ArrayList<String>();
        String curr = "";
        boolean quote = false;
        for(int pos = 0; pos < line.length(); pos++) {
            char c = line.charAt(pos);
            if(c == '\'') {
                quote = !quote;
            }
            else if(c == ' ' && !quote) {
                values.add(curr);
                curr = "";
            }
            else {
                curr += c;
            }
        }
        lines.add(values);
        line = reader.readLine();
    }
    reader.close();
    // Access the first value of the first line as an int
    System.out.println("The first value * 2 is " + (Integer.parseInt(lines.get(0).get(0)) * 2));

    // Access the third value of the first line as a string
    System.out.println("The third value is " + lines.get(0).get(2));
} catch (IOException e) {
    System.out.println("Error reading file: ");
    e.printStackTrace();
} catch (Exception e) {
    System.out.println("Error parsing file: ");
    e.printStackTrace();
}

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

相关问题 Java - 如何将由空格分隔的整数读入数组 - Java - How to read integers separated by a space into an array 如何从java中的文件中读取字符串和数字? - How to read strings and numbers from file in java? 如何从Java中的文件导入多个矩阵,其中数字之间用空格分隔,而矩阵之间用“ @”分隔? - How do I import multiple matrices from a file in Java, where the numbers are separated by a space and the matrices are separated by “@”? 从文件中读取以“:”分隔的数字-Java - Read numbers separated by “:” from file - Java 如何不在以空格分隔的字符串的末尾插入多余的空格 - how to not insert extra space at the end or beginning of space separated strings 如何从文件中读取由返回分隔的字符串 - How to read strings separated by return from a file 从Java文件中获取以空格分隔的数字 - Getting space-separated numbers from a file in Java 如何读取同一行中的两个数字输入,用Java中的空格分隔? - How do I read two numeric inputs in the same line, separated by a space in Java? 如何在Java中的字符串中找到以空格分隔的字符? - How to find space separated chars in a string in java? 我如何从java中用逗号分隔的.txt文件中读取数字对(x,y)? - How can i read pairs (x,y) of numbers from a .txt file separated by commas in java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM