Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.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_Arrays_Sorting_Substring_Names - Fatal编程技术网

Java 在';之前计数字符';在数组的每个索引中,并返回最高

Java 在';之前计数字符';在数组的每个索引中,并返回最高,java,arrays,sorting,substring,names,Java,Arrays,Sorting,Substring,Names,我需要从这个数组中获取最长的姓氏并返回它 String[] names = {"Turner, Brendan", "Savage, Fred", "Zidy, Boop", "Zobie, Brendan", "Flurb, Fred", "Mopeeeeeyyy, Boopertinson"}; 到目前为止,我有这个 public sta

我需要从这个数组中获取最长的姓氏并返回它

String[] names = {"Turner, Brendan", "Savage, Fred", "Zidy, Boop",
                 "Zobie, Brendan", "Flurb, Fred", "Mopeeeeeyyy, Boopertinson"};
到目前为止,我有这个

public static void getLongestSurname(String[] name){
         int i = 0;
         int x = 0;
         int currentLength = 0; 
         int lastLength = 0; 
         String longestName = null;
    for(int j = 0; j < (name.length); j++){
      
          while (name[j].charAt(i) != ',') {
          i++;
          currentLength++;
          }
      System.out.println(i);
      System.out.println("current is"+currentLength);
      i = 0;
      currentLength = 0;
        if ( currentLength > lastLength ){
           longestName = name[i]; 
          }
    }
    
     System.out.println("longest surname should be; "+ longestName);
  }
public静态void getLongestName(字符串[]名称){
int i=0;
int x=0;
int currentLength=0;
int lastLength=0;
字符串longestName=null;
对于(int j=0;j<(name.length);j++){
while(name[j].charAt(i)!=','){
i++;
currentLength++;
}
系统输出打印LN(i);
System.out.println(“电流为”+电流长度);
i=0;
currentLength=0;
如果(currentLength>lastLength){
longestName=名称[i];
}
}
System.out.println(“最长的姓氏应该是;”+longestName);
}
但它给出的结果是“最长的姓氏应该是;特纳,布伦丹” 这不是名单上最长的名字

我在这里做错了什么,但此时我的大脑感觉像是炒蛋。有人能帮忙吗


谢谢。

您需要跟踪
longestName
在迭代所有名称时,将当前名称的长度与当前
longestring
的长度进行比较,而不是与lastLength进行比较

如果currentName的长度大于longestNumber的长度,则将更新longestName

解决方案

public static void getLongestSurname(String[] name){
        
        int currentLength = 0; 
        int lastLength = 0; 
        String longestName = null;
        for(int j = 0; j < (name.length); j++){

           // split string by "," to get surname
            String[] strAr = name[j].split(",");
            // length of current surname 
            currentLength = strAr[0].length();
           // compare length of current surname with longest surName                
            if ( currentLength > lastLength ){
                longestName = name[j]; 
                lastLength = strAr[0].length();
            }
        }

        System.out.println("longest surname should be; "+ longestName);
    }

public静态void getLongestName(字符串[]名称){
int currentLength=0;
int lastLength=0;
字符串longestName=null;
对于(int j=0;j<(name.length);j++){
//用“,”分隔字符串以获得姓氏
字符串[]strAr=name[j]。拆分(“,”;
//当前姓氏长度
currentLength=strAr[0]。长度();
//比较当前姓氏和最长姓氏的长度
如果(currentLength>lastLength){
longestName=名称[j];
lastLength=strAr[0]。长度();
}
}
System.out.println(“最长的姓氏应该是;”+longestName);
}

对于数组中的每个名称,解析姓氏。将其与当前最长的姓氏进行比较。如果电流为null或更短,则更换它。继续这样做,直到列表用尽。返回结果

public String findLongestSurname(String[] names) {
  String longestSurname = null;
  for (String name : names) {
    String[] tokens = name.split(",");
    String surname = tokens[0];
    if (longestSurname == null || surname.length > longestSurname.length) {
      longestSurname = surname;
    }
  }
  return longestSurname;
}

我不确定我在跟踪你。但当我在这里使用它时,longestname将成为名字[I]中任何东西的过滤掉的姓氏。在if语句中,它是null。当然,如果我的数组只包含姓氏,我可以使用.length操作符,因为索引数组中的字符串和姓氏是相同的数字,但是对于这些字符串,在比较中,我需要忽略末尾的“.firstname”。希望这至少能让你了解我困惑的本质。谢谢你的建议,对不起,我没听进去。