Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //This is a Java program.
- //It sorts the list of your things specified by name.
- //This code was written in the JGrasp compiler.
- //Have the text file in the same directory as this file.
- //The file that you input must exist and its name match
- //it with the format extension.
- //For example, if you have a text file
- //called "BookList," you must input it as BookList.txt.
- //In the file, each item must be written on each line like this:
- //The Great Gatsby
- //A Streetcar Named Desire (without the double slashes [//])
- import java.util.*;
- import java.io.*;
- public class SortAListFromFile{
- public static void main(String [] args){
- List<String> media = new ArrayList<String>();
- try{
- Scanner console = new Scanner(System.in);
- System.out.println("What is your text file to sort called?");
- String fileName = console.nextLine();
- Scanner textConsole = new Scanner(new File(fileName));
- while(textConsole.hasNextLine()){media.add(textConsole.nextLine());};
- console.close();
- textConsole.close();
- }
- catch (FileNotFoundException e) {
- System.out.println("An error occurred.");
- e.printStackTrace();
- }
- Collections.sort(media);
- System.out.println("The sorted order is: ");
- for(var iterator : media) System.out.println(iterator);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement