读取文件并分别拆分数字和字符串/字符

时间:2014-06-19 12:34:31

标签: java

我想读取一个文件并将数字和字符串/字符分开排列,任何人都可以帮帮忙,我怎么能在java程序中这样做。

例如;我34岁是印度人,有45岁

答案:

         numbers : 34, 45
         String  : I am an Indian with

请帮我解决这个问题

3 个答案:

答案 0 :(得分:1)

尝试以下代码..可能会对您有所帮助

          public static void main(String... args) {

    try {

        File file = new File("myText.text");

        FileWriter fw = new FileWriter(file);

        BufferedWriter bw = new BufferedWriter(fw);

        bw.write("I am 34 an Indian, with 45");

        bw.close();

        FileReader fr = new FileReader(file);
        BufferedReader br = new BufferedReader(fr);

        String s = br.readLine();

        String[] data = s.split("\\d");

        String[] numData = s.split("[^0-9]");

        StringBuffer numericData = new StringBuffer();

        StringBuffer charData = new StringBuffer();
        for (int j = 0; j < numData.length; j++) {

            numericData.append(numData[j]);
            // numericData.append(",");

        }

        for (int i = 0; i < data.length; i++) {

            charData.append(data[i]);

        }

        System.out.println("String :" + charData);
        System.out.println("Numbers :" + numericData);

    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

答案 1 :(得分:0)

您可以读取文件,然后像这样解析它。

boolean isInteger(String str) {
         try {
            Integer.parseInt(str);
         } catch (NumberFormatException e){
         return false;
        }
        return true;
      } 




    List<Integer> getNumbers(String str) {
       List<Integer> nums = new ArrayList<Integer>();
       String [] temp = str.split(" ");
       for(int i=0; i< temp.length; i++) {
         if(isInteger (temp[i]))
            nums.add(temp[i])

       }
    }

   List<String> getStrings() {
       List<String> str = new ArrayList<String>();
       String [] temp = str.split(" ");
       for(int i=0; i< temp.length; i++) {
         if(!isInteger (temp[i]))
            str.add(temp[i])

       }
       return str;
   }

答案 2 :(得分:0)

尝试使用BreakIterator,

import java.io.*;
import java.text.BreakIterator;
import java.util.ArrayList;
import java.util.List;

public class StringAndNumberSeparator {

    public static void main(String... args)
    {
        readFile();

    }

    private static void checkStringAndNumber(String valueTobeChecked) {
        List<String> words = new ArrayList<String>();
        List<Number> numbers = new ArrayList<Number>();
        BreakIterator breakIterator = null;
        int nextIndex = -1;
        int anIndex = -1;

        breakIterator = BreakIterator.getWordInstance();
        breakIterator.setText(valueTobeChecked);
        nextIndex = breakIterator.first();
        while (BreakIterator.DONE != nextIndex){
            anIndex = nextIndex;
            nextIndex = breakIterator.next();

            if ((BreakIterator.DONE != nextIndex) &&  Character.isAlphabetic(valueTobeChecked.charAt(anIndex))) {
                words.add(valueTobeChecked.substring(anIndex, nextIndex));
            }

            if ((BreakIterator.DONE != nextIndex) && Character.isDigit(valueTobeChecked.charAt(anIndex))){
                numbers.add(Integer.valueOf(valueTobeChecked.substring(anIndex, nextIndex)));
            }
        }

        for (String string : words){
            System.out.print( " "+string);
        }

        System.out.println();

        for (Number number : numbers){
            System.out.print( " "+number);
        }
    }

    private static void readFile(){
        BufferedReader br = null;
        try {
            String sCurrentLine;
                br = new BufferedReader(new FileReader("C:\\Users\\Admin\\Desktop\\banner.txt"));
            while ((sCurrentLine = br.readLine()) != null) {
                checkStringAndNumber(sCurrentLine);
            }
            } catch (FileNotFoundException e) {
                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            } catch (IOException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
    }
}

希望这有助于解决您的问题。