Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 读取文件并分别拆分数字和字符串/字符_Java - Fatal编程技术网

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

Java 读取文件并分别拆分数字和字符串/字符,java,Java,我想读取一个文件,并将数字和字符串/字符分别拆分成一行,请任何人帮忙,我如何在java程序中实现这一点 Eg;我34岁,印度人,45岁 答复: numbers : 34, 45 String : I am an Indian with 请帮我解决这个问题。您可以读取文件,然后像这样解析它 boolean isInteger(String str) { try { Integer.parseInt(str);

我想读取一个文件,并将数字和字符串/字符分别拆分成一行,请任何人帮忙,我如何在java程序中实现这一点

Eg;我34岁,印度人,45岁

答复:

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

请帮我解决这个问题。

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

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;
   }
布尔isInteger(字符串str){
试一试{
Integer.parseInt(str);
}捕获(数字格式){
返回false;
}
返回true;
} 
列表编号(字符串str){
List nums=new ArrayList();
字符串[]temp=str.split(“”);
对于(int i=0;i
试试下面的代码..这会对你有帮助吗

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

}
publicstaticvoidmain(字符串…参数){
试一试{
File File=新文件(“myText.text”);
FileWriter fw=新的FileWriter(文件);
BufferedWriter bw=新的BufferedWriter(fw);
写下(“我是34岁的印度人,45岁”);
bw.close();
FileReader fr=新的FileReader(文件);
BufferedReader br=新的BufferedReader(fr);
字符串s=br.readLine();
字符串[]数据=s.split(\\d”);
字符串[]numData=s.split([^0-9]”);
StringBuffer numericData=新的StringBuffer();
StringBuffer charData=新的StringBuffer();
对于(int j=0;j
尝试使用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.
        }
    }
}
import java.io.*;
导入java.text.BreakIterator;
导入java.util.ArrayList;
导入java.util.List;
公共类字符串和编号分隔符{
公共静态void main(字符串…参数)
{
readFile();
}
私有静态无效检查StringAndNumber(要检查的字符串值){
List words=new ArrayList();
列表编号=新的ArrayList();
BreakIterator BreakIterator=null;
int-nextIndex=-1;
int-anIndex=-1;
breakIterator=breakIterator.getWordInstance();
breakIterator.setText(valueTobeChecked);
nextIndex=breakIterator.first();
while(BreakIterator.DONE!=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))){
add(Integer.valueOf(valueTobeChecked.substring(anIndex,nextIndex));
}
}
for(字符串:单词){
系统输出打印(“+字符串);
}
System.out.println();
用于(编号:编号){
系统输出打印(“+编号);
}
}
私有静态void readFile(){
BufferedReader br=null;
试一试{
弦电流线;
br=新的BufferedReader(新的文件阅读器(“C:\\Users\\Admin\\Desktop\\banner.txt”);
而((sCurrentLine=br.readLine())!=null){
检查字符串和编号(当前行);
}
}catch(filenotfounde异常){
e、 printStackTrace();//要更改catch语句的主体,请使用文件|设置|文件模板。
}捕获(IOE异常){
e、 printStackTrace();//要更改catch语句的主体,请使用文件|设置|文件模板。
}
}
}

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

对不起,StackOverflow不是免费的代码编写服务。您需要向我们表明您已尝试为自己编写此程序。请提供您的实现,并让我们知道您遇到的问题,然后我们可以更好地帮助您。将行读入字符串,按空格分隔,迭代结果,检查它是否是一个数字,并根据需要添加到两个列表中的一个。如果您还没有开始在google中编写代码搜索。