【发布时间】:2016-09-04 08:58:49
【问题描述】:
我正在尝试从文件中读取 5 个名称的列表,确定每个名称的长度并使用数组对长度进行排序。但是,我似乎无法弄清楚如何将名称的长度传递给数组......非常感谢帮助!
import java.io.*;
import java.util.Arrays;
public class NewClass {
public static void main(String[] args) throws IOException,
FileNotFoundException {
String file = "names.txt";
processFile("names.txt");
}
public static void processFile (String file) throws IOException, FileNotFoundException{
BufferedReader inputReader =
new BufferedReader (new InputStreamReader
(new FileInputStream("names.txt")));
String line;
while (( line = inputReader.readLine()) != null){
int nameL =line.length();
}
/*public static void main (String [] args) {
*/
int [] aryNum;
aryNum= new int [5];
aryNum[0] = int nameL;
aryNum[1]=int nameL;
aryNum[2]=int nameL;
aryNum[3]=int nameL;
aryNum[4]=nameL;
Arrays.sort(aryNum);
int i;
for (i=0; i<aryNum.length; i++) {
System.out.println("" + aryNum[i]);
}
}
}
【问题讨论】:
-
您想仅对名称的长度进行排序,还是根据名称(字符串)的长度对名称(字符串)进行排序?
-
我想对名称的长度进行排序,但是学习如何根据名称的长度对名称进行排序也很棒!
标签: java arrays sorting bufferedreader string-length