The following code only sorts the numbers from a file name wch has been passes as parameter in the code. When I tried to modify it for input it gives horrible errors... Please help me to modify the code or may be some other approach which you think is easier...
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class IntegersFromFile {
public static void main(String[] args) {
Scanner file = null;
//Scanner file1=SalesData.txt;
ArrayList<Integer> list = new ArrayList<Integer>();
try {
file = new Scanner(new File("numbers.txt"));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
while(file.hasNext()){
if (file.hasNextInt())
list.add(file.nextInt());
else file.next();
}
Collections.sort(list);
for (Integer i: list) System.out.print(i);
}
}

New Topic/Question
Reply



MultiQuote





|