2013-10-25 53 views
0

所以解决空字符串,我在下面的格式期待我的数据:在Java

"domain::foo::127" 

因此,这里是我的代码:

String[] typeChunks = input.split("::"); 

      String type = typeChunks[0]; 
      String edge = typeChunks[1]; 

      double reputation = Double.parseDouble(typeChunks[2].trim()); 

但我得到这个eror

  java.lang.NumberFormatException: empty String 
at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1011) 
at java.lang.Double.parseDouble(Double.java:540) 
at org.attempt2.BuildGraph$ReduceClass.reduce(BuildGraph.java:94) 
at org.attempt2.BuildGraph$ReduceClass.reduce(BuildGraph.java:1) 
at org.apache.hadoop.mapreduce.Reducer.run(Reducer.java:176) 
at org.apache.hadoop.mapred.ReduceTask.runNewReducer(ReduceTask.java:650) 
at org.apache.hadoop.mapred.ReduceTask.run(ReduceTask.java:418) 
at org.apache.hadoop.mapred.Child$4.run(Child.java:255) 
at java.security.AccessController.doPrivileged(Native Method) 
at javax.security.auth.Subject.doAs(Subject.java:415) 
at org.apache.hadoop.security.UserGroupInformation.doAs(UserGroupInformation.java:1136) 
at org.apache.hadoop.mapred.Child.main(Child.java:249) 

什么是处理这个问题的好方法?

+0

用正则表达式匹配每个组可能会更好。 –

回答

1

您需要处理您有错误数据的情况。这不完全是一个详尽的验证,但它可能是一个起点:

String[] format = "domain::foo::127".split("::"); 

... 

boolean validateFormat(String[] format) { 
    // Check for anything that you don't want coming through as data 
    return format.length == 3; 
} 
0

使用String[] typeChunks = input.split("::");if (!input.equals("")){,不要忘记关闭}

0

您可以验证之前解析字符串值:

double reputation = (typeChunks[2].trim() != null && 
        !typeChunks[2].trim().isEmpty()) ? 
        Double.parseDouble(typeChunks[2].trim()) : 0; 
+1

一般来说,不是通过返回一个'0'来默默地失败,而应该考虑抛出一个'Exception'。这样你可以决定什么是最适合处理这个问题的。 (通知用户,使用默认值,日志等) – TheMorph

0

错误消息是由于处理空数据造成的。

double reputation = 0; 
final String reputStr = typeChunks[2]; 
if ((reputStr != null) && !("").equals(reputStr.trim())) 
{ 
    reputation = Double.parseDouble(typeChunks[2].trim()); 
} 
2

除了应该在使用数据之前,没有一种单一的好方法来验证数据。我建议使用Scanner作为整体解析您的数据字符串,而不是将您的数据字符串拆分,然后单独将它们转换为正确的数据类型,这样可以为您提供类型安全性。

Double reputation = null; 
String type = null, edge = null; 

String dataString = "domain::foo::127"; 
Scanner scanner = new Scanner(dataString).useDelimiter("::"); 

if (scanner.hasNext()) { 
    type = scanner.next(); 
} else 
    throw new IllegalArgumentException("Type not found!"); 
if (scanner.hasNext()) { 
    edge = scanner.next(); 
} else 
    throw new IllegalArgumentException("Edge not found!"); 
if (scanner.hasNextDouble()) { 
    reputation = scanner.nextDouble(); 
} else 
    throw new IllegalArgumentException("Reputation not found!"); 

System.out.println(type); // domain 
System.out.println(edge); // foo 
System.out.println(reputation); // 127.0 


同样好的办法是要测试的正则表达式的完整数据串(如果它不如愿长),但有可能失去对信息的成本到底哪个数据单元验证失败。

Pattern pattern = Pattern.compile("(\\w+)::(\\w+)::(\\d+)"); 
Matcher matcher = pattern.matcher(dataString); 

if (matcher.matches()) { 
    type = matcher.group(1); 
    edge = matcher.group(2); 
    reputation = Double.valueOf(matcher.group(3)); 
} else 
    throw new IllegalArgumentException("Invalid input data"); 
1

使用正则表达式,您可以验证输入字符串是否有效!

String pattern = "[a-z]+::{1}[a-z]+::{1}[0-9]+(\\.[0-9][0-9]?)?"; 

String type, edge; 
double reputation; 

if(input.matches(pattern)){ 
    String[] typeChunks = input.split("::"); 
    type = typeChunks[0]; 
    edge = typeChunks[1]; 
    reputation = Double.parseDouble(typeChunks[2].trim()); 
} 
else 
    throw new IllegalArgumentException(); 

此正则表达式将检查

  1. 字母型
  2. 字母边缘
  3. 数字声誉带或不带小数点
  4. “::” 三个
0

之间怎么样创建一个简单的助手类来检查你的字符串...像

public class StringUtil {

public static boolean isNullOrEmpty(final String string)

{
return string == null || string.isEmpty() || string.trim().isEmpty();

}

}

所以在这种方式通过Y你不需要使用trim()。因为如果你对一个空字符串使用trim(),你会得到一个异常。但是您仍然必须处理Double.parseDouble中的NumberFormatException。

所以,如果你不想添加try和catch块,每次你可以为Double.parseDouble创建一个简单的包装来捕获异常并以你的方式处理它们(比方说返回-1)。

double reputation = StringUtil.isNullOrEmpty(typeChunks[2])== true ? 0 : YourClass.methodToParseDoubleAndHandleException(typeChunks[2]);

+0

我喜欢这种方法,你总是控制你的结果 – Anton