根据它们之间的空间区分字符串

时间:2015-04-26 09:10:30

标签: java string

我有一个由应用程序生成的文本文件,其中包含如下字符串:

26    
50 597 424 561
499 849
283 76
115 81
4
26

public void listingCoord() throws IOException{

    File f = new File("coord.txt");
    FileWriter writer = new FileWriter(f);

    for(String s: interationList){

    writer.write(s+"\n");
    }
    writer.close();

    }

public void replay(){

    BufferedReader br = null;
    try{
        String line;
        br = new BufferedReader(new FileReader("coord.txt"));
        while((line = br.readLine())!=null){
            System.out.println(line);
        }
    }
        catch (IOException io){ io.printStackTrace();}
    try {
        br.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

列出字符串的代码在上面。 只是有一个列表,其中包含显示的字符串,列表和重放方法写入和读取行。

我想要做的是读取这个整数,但是以不同的方式。 我知道如何阅读文本文件并显示它,但我想要的是对数字做出决定:

  • 如果它处于一个没有像26这样的空格的位置,就把它作为参数发送给正确的方法 - 因为我已经准备好了方法 - 。

  • 如果它位于两个位置,一个空格如499 849,则发送正确的方法。

等等,

有办法做到这一点吗?我的意思是根据它们之间的空间区分数字分区。

2 个答案:

答案 0 :(得分:1)

如果我理解得很清楚,你必须知道有多少数字排成一行 所以你应该逐行阅读文本文件,然后解析整数:

import java.util.Scanner;
import java.io.*;
public class Parser{
    public static void main(String[] args){ //excepiton catching needed
        Scanner sc = new Scanner(new File(args[0])); //for example
        while(sc.hasNextLine()){
            String line = sc.nextLine();
            String[] array = line.split(" ");
            switch(array.length){
                case 1: oneParamMethod(array[0]); break;
                case 2: twoParamMethod(array[0],array[1]); break;
                default: System.out.println("Strange line");
            }
        }
        sc.close();
    }
    public static void oneParamMethod(String param){
        int value = Integer.parseInt(param);
        // there will be your code
    }
    public static void twoParamMethod(String param1,String param2){
        int value1 = Integer.parseInt(param1);
        int value2 = Integer.parseInt(param2);
        // there will be your code
    }
}

未经测试
如果可能的话应该使用数组而不是开关,因为@Pshemo在他的评论中注意到了

答案 1 :(得分:1)

基于this回答,我更喜欢这个:

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class Parser {
    public static void main(String[] args) {
        Scanner sc;
        try {
            sc = new Scanner(new File(args[0]));
            parseLines(sc);
            sc.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    private static void parseLines(Scanner sc) {
        while (sc.hasNextLine()) {
            String line = sc.nextLine();
            String[] array = line.split(" ");
            try {
                parseLineArray(array);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    private static void parseLineArray(String[] array) throws Exception {
        switch (array.length) {
        case 0:
            throw new Exception("Empty line found");
        case 1:
        case 2:
            processEntry(array);
            break;
        default:
            throw new Exception("Too many entries: [" + array.toString() + "]");
        }
    }

    private static void processEntry(String[] array) {
        if (array.length == 1) {
            oneParamMethod(Integer.parseInt(array[0]));
        } else {
            twoParamMethod(Integer.parseInt(array[0]), Integer.parseInt(array[1]));
        }
        // your code
    }

    private static void oneParamMethod(Integer value1) {

        // your code
    }

    private static void twoParamMethod(Integer value1, Integer value2) {

        // your code
    }
}
  • 异常处理
  • 方法更多,但方法中的代码更少,因此代码更易读
  • 不导入任何您不需要的东西(例如:import java.io.*;)