[英]cat a textfile into standard input resulting null in array?
I'm cating a file using the cat text-file | 我正在使用cat文本文件对文件进行分类。 Java my-program on my terminal 我的终端上的Java my-program
the result when I print the lines after i store it into a array results in null can someone explain why? 将行存储到数组中后打印行的结果为null,有人可以解释为什么吗?
Scanner scan2 = new Scanner(System.in);
Scanner scan = new Scanner(System.in);//create new scanner object
int index = 0;//create index to increment through array
while(scan.hasNextLine()){//while looop to execute if file has length
String line = scan.nextLine();//store line into string input
count++;
}
if(count < BUFSIZE){
stringArray = new String[count];
}
else{
stringArray = new String[BUFSIZE];
}
while(scan2.hasNextLine()){
String line2 = scan2.nextLine();
if(index > stringArray.length-1)
{
stringArray = expandArray(stringArray,BUFSIZE);//call method to increase array length
}
stringArray[index] = line2;//store line into array at given index
index++;//increment index
}
/*while(sorted){
sorted = false;
for(int i = 0; i < stringArray.length-1; i++){
if(stringArray[i].compareTo(stringArray[i+1]) > 0){
temp = stringArray[i];
stringArray[i] = stringArray[i+1];
stringArray[i+1] = temp;
sorted = true;
}
}
}*/
for(int i = 0; i < stringArray.length; i++){
System.out.println(stringArray[i]);
}
}
}
private static String [] expandArray(String [] array, int extend){
String newArray [] = new String[array.length+extend];//create new array with given array and int as length to extend
//for loop to copy data from old array into new created array
for( int i = 0; i < array.length; i++){
newArray[i] = array[i];
}
return newArray;//return newly created array
}
}
the program bubble sorts the array of strings. 程序气泡对字符串数组进行排序。 If I read in the file through Scanner file its fine, but why I cat it doesn't. 如果我通过Scanner文件读取了文件,则很好,但是为什么我不满意。 The expand array method is to dynamically expand the array every-time it reaches max capacity. expand array方法是在每次达到最大容量时动态扩展阵列。 Thank you 谢谢
The following is wrong: 以下是错误的:
Scanner scan2 = new Scanner(System.in);
Scanner scan = new Scanner(System.in);//create new scanner object
Basically, each scanner grabs the System.in
stream, but streams can only be read once . 基本上,每个扫描器都获取System.in
流,但是流只能读取一次 。 You should change your code to use only one scanner, and then only use it once. 您应该将代码更改为仅使用一台扫描仪,然后仅使用一次。
When you wrote the program to use a file, the Scanner would actually open two streams to the file so that it can be read twice, but this won't work when all you have is one stream. 当您编写程序以使用文件时,扫描程序实际上会打开该文件的两个流,以便可以读取两次,但是当您只有一个流时,此操作将无效。
EDIT: 编辑:
Here is a version where you use only one Scanner (and thus one stream) : 这是一个仅使用一个Scanner(因此使用一个流)的版本:
Scanner scan = new Scanner(System.in);//create new scanner object
int count = 0;
stringArray = new String[BUFSIZE];
while (scan.hasNextLine()){//while looop to execute if file has length
String line = scan.nextLine();//store line into string input
if (count >= stringArray.length) {
//call method to double array length
stringArray = expandArray(stringArray, stringArray.length);
}
stringArray[count] = line;
count++;
}
// Shrink array to required size
String[] temp = stringArray;
stringArray = new String[count];
System.arraycopy(temp, 0, stringArray, 0, count);
Please note I didn't test it, but this is conceptually how you could do it. 请注意,我没有测试它,但是从概念上讲,这是您可以执行的操作。
The other alternative is to use an ArrayList<String>
which will automatically expand and shrink. 另一种选择是使用ArrayList<String>
,它将自动扩展和收缩。
Scanner scan = new Scanner(System.in);
List<String> list = new LinkedList<>();
while(scan.hasNext()) list.add(scan.next());
scan.close();
Collections.sort(list);
for(String line : list) System.out.println(line);
lol 大声笑
UPDATE @JBert: 更新 @JBert:
System.out.println(StringUtils.join(Ordering.<String>natural().sortedCopy(IOUtils.readLines(System.in)), "\r\n"));
looool looool
In this code 在这段代码中
while(scan2.hasNextLine()){
String line2 = scan2.nextLine();
if(index > stringArray.length-1)
you are doing something if index is greater than the length of the array, but no doing anything otherwise. 如果index大于数组的长度,则您正在执行某项操作,但没有其他操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.