0

我正在学习java,我有一个关于从文件中读取的问题,我也想从包含字符串的文件中读取数字。这是我的文件的一个示例:

66.56
"3
JAVA
3-43
5-42
2.1
1

这是我的编码: public class test {

public static void main (String [] args){
      if (0 < args.length) {
     File x = new File(args[0]);
    try{     
Scanner in = new Scanner( new FileInputStream(x));
ArrayList<Double> test = new ArrayList<>();
while(in.hasNext()){ 
    if(in.hasNextDouble()){
      Double f=in.nextDouble(); 
      test.add(f);}
    else 
            {in.next();}
}
 catch(IOException e) { System.err.println("Exception during reading: " + e); }
}

我的问题是它只添加 66.56,2.1 和 1 它没有在 "3 之后添加 3 或者它忽略 3-43 和 5-42 你能告诉我如何跳过字符串并且只在此处添加双打吗?谢谢

4

2 回答 2

0

All the said three ie; "3, 3-43 and 4-42 are strings

Either u read a string and split it and check for number at " and - or you put in a space between characters and integers. The JVM after compilation would treat it all as string if it cannot be converted to a double. And the File reader wont stop reading till at least a space or a newline. Hence your code would never work the way you intend it to unless you do as I said above.

Solution 1:
Change your input file to something like this:

66.56
" 3
JAVA
3 - 43
5 - 42
2.1
1

Solution 2:
Considering the highly variable nature of your input file I am posting a solution only made for your current input. If the input changes a more versatile algorithm would need to be implemented.

public static void main(String[] args) {
        File x = new File(args[0]);
        try {
            Scanner in = new Scanner(new FileInputStream(x));
            ArrayList<Double> test = new ArrayList<>();
            while (in.hasNext()) {
                if (in.hasNextDouble()) {
                    Double f = in.nextDouble();
                    test.add(f);
                } else {
                    String s=in.next();
                    if(s.contains("\"")){
                        String splits[]=s.split("\"");
                        test.add(Double.parseDouble(splits[1]));
                    }
                    else if (s.contains("-")){
                        String splits[]=s.split("-");
                        test.add(Double.parseDouble(splits[0]));
                        test.add(Double.parseDouble(splits[1]));
                    }
                }
            }
            System.out.println(test);
        } catch (IOException e) {
            System.err.println("Exception during reading: " + e);
        }
}
于 2013-10-06T13:36:28.457 回答
0

您可以编写自定义类型传感器实用程序类来检查对象是否可以转换为整数。我会这样处理这个问题。

此外,我可以看到你有像2.1" 3这样的值来处理这些场景,编写额外的方法,比如isDoubleType()isLongType()等。

您还需要编写一些自定义逻辑来解决此问题。

public class TypeSensor {
public String inferType(String value) throws NullValueException {
        int formatIndex = -1;

        if (null == value) {
            throw new NullValueException("Value provided for type inference was null");
        }else if (this.isIntegerType(value)) {
            return "Integer";
        }else{
            LOGGER.info("Value " + value + " doesnt fit to any predefined types. Defaulting to String.");
            return "String";
        }
    }
}

private boolean isIntegerType(String value) {
        boolean isParseable = false;
        try {
            Integer.parseInt(value);
            LOGGER.info("Parsing successful for " + value + " to Integer.");
            isParseable = true;
        } catch (NumberFormatException e) {
            LOGGER.error("Value " + value + " doesn't seem to be of type Integer. This is not fatal. Exception message is->" 
                                                + e.getMessage());
        }
        return isParseable;
    }
}
于 2013-10-06T14:10:17.340 回答