BestJewSinceJC
Posting Maven
2,774 posts since Sep 2008
Reputation Points: 874
Solved Threads: 354
Skill Endorsements: 13
ok well i tried this but i want to modify it to sort text rather than numbers not sure how to do it I tried this
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class NameSort {
public static void main(String[] args) {
Scanner file = null;
[ArrayList<String> list = new ArrayList<String>();]
try {
file = new Scanner(new File("names.txt"));
} catch (FileNotFoundException e) {
e.printStackTrace(); }
while(file.hasNext()){
if (file.hasNextString()) list.add(file.nextString());
else file.next(); }
Collections.sort(list);
for (String s: list) System.out.println(s);
}
}
but it doesnt want to work keeps coming back with errors any help would be much appreciated
jade_91
Junior Poster in Training
50 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
The read loop starts on line 20:
while(file.hasNext()){
That loop is where you'd perform any operations you want to do with each entry.
Ezzaral
null
16,126 posts since May 2007
Reputation Points: 3,294
Solved Threads: 875
Skill Endorsements: 28