您的程序应该读取一个输入文件(程序的第一个参数)。第一行包含数字“N”的值,后跟多行。您可以假设输入文件的格式正确,并且第一行的数字(即“N”)是有效的正整数。例如
这是我的输入:
2
你好世界CodeEval
Quick Fox
A
旧金山
期望的输出应该是:
旧金山
你好世界
这是我的代码:
class Longest
{
public static void main(String args[]) throws FileNotFoundException
{
BufferedReader in = null;
List<String> myList = new ArrayList<String>();
try
{
in = new BufferedReader(new FileReader("C:\\filename.txt"));
String str;
while ((str = in.readLine()) != null)
{
if (str.length() == 0) continue;
myList.add(str);
}
}
catch (IOException e)
{
e.printStackTrace();
}
System.out.println("Contents of the ArrayList : "+myList);
System.out.println("Size of the ArrayList : "+myList.size());
String s = myList.remove(0);
System.out.println(System.getProperty("line.separator"));
System.out.println("Number of lines to be printed : "+s);
System.out.println("After removing first element of ArrayList : "+myList);
System.out.println("Size of the ArrayList : "+myList.size());
Comparator comparator=Collections.reverseOrder();
Collections.sort(myList,comparator);
System.out.println("After sorting ArrayList in Descending Order :"+myList);
int x = Integer.parseInt(s);
System.out.println(System.getProperty("line.separator"));
for (String s1 : myList) {
System.out.println(s1);
}
System.out.println(System.getProperty("line.separator"));
for(int i=0; i<x; i++){
System.out.println(myList.get(i));
}
}
}
但我得到这个输出:
旧金山
快狐
我哪里错了?