日期:2014-05-20  浏览次数:20753 次

***公司一道机试题,为什么总是做不对?!求指导
各位,今天做**公司一道机试题,题目很简单,就是校验电子邮件地址,我在myeclipse中运行也正常,但是代码粘贴到在线机试平台后,提交后就提示4个测试用例全部失败。

机试答题前的说明:

题目:

我的代码
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {

public static void main(String[] args)throws IOException {
 BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  
   System.out.println("请输入要验证的邮箱地址:"+"\n"+"-->");
   String inputStr = reader.readLine();
   Main ma=new Main();
   boolean x=ma.validateEmail(inputStr);
   
if(x==true){
System.out.println("1");
}else{
System.out.println("0");
}

  

}

public  boolean validateEmail(String email) {
int pos = email.indexOf("@");
if (pos == -1 || pos == 0 || pos == email.length() - 1) {
return false;
}
String[] strings = email.split("@");
if (strings.length != 2) {
return false;
}
CharSequence cs = strings[0];
for (int i = 0; i < cs.length(); i++) {
char c = cs.charAt(i);
if(c==48){
return false;
}
if (!Character.isLetter(c) && !Character.isDigit(c)&& c!=95&& c!=46) {

return false;
}
}
pos = strings[1].indexOf(".");
if (pos == -1 || pos == 0 || pos == email.length() - 1) {
return false;
}
strings = strings[1].split("\\.");
if(strings.length<2){
return false;
}

for (int j = 0; j < strings.length; j++) {
cs = strings[j];
if(j==strings.length-1){
if(!cs.equals("com")){
return false;
};
}
if (cs.length() == 0) {
return false;
}
for (int i = 0; i < cs.length(); i++) {
char c = cs.charAt(i);
if(c==48){
return false;
}
if (!Character.isLetter(c) && !Character.isDigit(c) && c!=95) {

return false;
}
}

}
return true;
}

}


提交后,说我的代码测试用例都不通过,截图如下:


我个人的想法:
我实在找不到错误的原因在哪,不知道是不是没满足内存不大于128M的要求而引起的,请大家帮忙看看!
------解决方案--------------------
想的太多了吧,尽量用简单的方法吧,1用split("@")=2就行了,2 用endwith就行吧,3 ,4 感觉可以用正则表达式,这个我也不太会。
------解决方案--------------------
引用:
想的太多了吧,尽量用简单的方法吧,1用split("@")=2就行了,2 用endwith就行吧,3 ,4 感觉可以用正则表达式,这个我也不太会。
3也可以用split(" ")=0

4肯定用正则表达式
public boolean isEmail(String str){   

  Pattern pattern = Pattern.compile("^w+[-+.]w+)*@w+([-.]w+)*.w+([-.]w+)*$”");   

  Matcher isNum = pattern.matcher(str);  

  if( !isNum.matches() ){  

      return false;   

  }   

  return true;   

}  

------解决方案--------------------
试了一下 这个应该能满足那个公司的验证规则
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.regex.Pattern;

public class Main {

public static void main(String[] args)throws IOException {
 BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  
   System.out.println("请输入要验证的邮箱地址:"+"\n"+"-->");
   String inputStr = reader.readLine();
   Main ma=new Main();
   boolean x=ma.validateEmail(inputStr);
   
if(x==true){
System.out.println("1");
}else{
System.out.println("0");
}

  

}

public  boolean validateEmail(String email) {
return Pattern.matches("^([A-Za-z1-9
------解决方案--------------------
.
------解决方案--------------------
_])+\\@([A-Za-z1-9
------解决方案--------------------
.
------解决方案--------------------
_])+[.com]+$", email);
}

}

------解决方案--------------------
你这图片上不是已经有了运行内存:
13452.0KB 了吗?测试个邮箱格式明显不会用到128M内存的