Java 最短字符串长度显示在主方法中,但放置在另一个方法中时不显示

Java 最短字符串长度显示在主方法中,但放置在另一个方法中时不显示,java,string,arraylist,methods,Java,String,Arraylist,Methods,这是密码 Package labone; import java.util.*; public class LabOne { static Scanner reader = new Scanner(System.in); static ArrayList<String> names = new ArrayList<>(); public static void main(String[] args) { System.out.println("type\n

这是密码

Package labone;
import java.util.*;

public class LabOne {

static Scanner reader = new Scanner(System.in);
static ArrayList<String> names = new ArrayList<>();


public static void main(String[] args) {
    System.out.println("type\n 1: Exercise 1 'People's names'\n '");
    int choose = reader.nextInt();
    switch(choose) {
        case 1 :
            PeopleNames();
            break;
        default: 
            System.out.println("invaild choice");
            break;
    }         
}

  public static void PeopleNames() {                
       //////// PEOPLES NAMES ////////       
  System.out.println("Enter names of people, if done enter 'done'");
    String Name = reader.nextLine();
    while(!"done".equals(Name)){ //if user type done exit from adding names
        names.add(Name); // otherwise add names
        Name = reader.nextLine();  //reads what user entered
    }
    //User typed 'done' and it displays the names in the arraylist
    PrintArray(); // method

    //Find Longest String
  int largestString = names.get(0).length(); 
    int index = 0;
    for (int i = 0; i<names.size(); i++){
        if(names.get(i).length() > largestString) {
            largestString = names.get(i).length();
            index = i;
        }
    }
    System.out.println(names.get(index) + " is the longest name and the length is " + largestString);

    //Find Shortest String
   int shortestString = names.get(0).length();
    int index1 = 0;
     for (int i = 0; i<names.size(); i++){
        if(names.get(i).length() < shortestString) {
            shortestString = names.get(i).length();
            index1 = i;
        } 
    }
     System.out.println(names.get(index1) + " is the shortest name and the length is  " + shortestString); 

     //Find the Average of all the Strings
     double num = names.size(); //number of elements in arraylist(size())
     double length =-1; 
     for(String str : names){
         length = length + str.length(); //sum of the lengths in the arraylist 
     }   
     length = length + names.size()-1;   
      num = length/num;    //divide the sum with the size()
      System.out.println("The average length of the names in the list: "+num);
}


 //Method to view arrayList ( names )
public static void PrintArray() {
    System.out.println("---------");
    for(String Name : names){
        System.out.println(Name);
    }

  }
   }
封装labone;
导入java.util.*;
公共级拉伯恩{
静态扫描仪阅读器=新扫描仪(System.in);
静态ArrayList名称=新ArrayList();
公共静态void main(字符串[]args){
System.out.println(“键入\n 1:练习1‘人名’\n’”);
int choose=reader.nextInt();
开关(选择){
案例1:
PeopleNames();
打破
违约:
System.out.println(“invaild选项”);
打破
}         
}
公共静态void PeopleNames(){
////////人名//
System.out.println(“输入人名,如果完成,则输入‘完成’”);
字符串名称=reader.nextLine();
而(!“done”.equals(Name)){//if user type done退出添加名称
Name.add(Name);//否则添加名称
Name=reader.nextLine();//读取用户输入的内容
}
//用户键入“完成”,并在arraylist中显示名称
PrintArray();//方法
//查找最长字符串
int largestString=names.get(0.length();
int指数=0;
for(int i=0;i最大字符串){
largestString=names.get(i).length();
指数=i;
}
}
System.out.println(names.get(index)+”是最长的名称,长度为“+最大的字符串);
//查找最短字符串
int shortestString=names.get(0.length();
int index1=0;

对于(int i=0;i请将
reader.nextLine()
更改为
reader.next()
作为
nextLine()
函数将一个空字符串作为第一个成员添加到arrayList中。代码如下所示:

    String Name = reader.next();
    while(!"done".equals(Name)){ //if user type done exit from adding names
        names.add(Name); // otherwise add names
        Name = reader.next();  //reads what user entered
    }

关于。

请将
reader.nextLine()
更改为
reader.next()
作为
nextLine()
函数正在将一个空字符串作为第一个成员添加到arrayList中。代码如下所示:

    String Name = reader.next();
    while(!"done".equals(Name)){ //if user type done exit from adding names
        names.add(Name); // otherwise add names
        Name = reader.next();  //reads what user entered
    }

注意。

你能描述一下你自己调试这段代码的步骤吗?“它在main方法中工作”可能有点模糊。我已经添加了结果。当我将代码放在main方法中时,它会给出最短字符串和最长字符串的答案,但是,当代码放在PeopleNames方法中时,它只显示最长字符串,而不显示最短字符串。@ashmeenClote请确保数组没有值
“”
(空字符串)。这可能会导致查找最短名称时出现问题。它没有,但我会仔细检查。您能描述一下您自己调试此代码所采取的步骤吗?“它在主方法中工作”可能有点模糊。我已经添加了结果。当我将代码放在main方法中时,它会给出最短字符串和最长字符串的答案,但是,当代码放在PeopleNames方法中时,它只显示最长字符串,而不显示最短字符串。@ashmeenClote请确保数组没有值
“”
(空字符串)。这可能会导致查找最短名称时出现问题。它没有,但我会再次检查。