1

So I am writing some code that takes an unsorted file and writes out only the names of the games in a new file in alphabetical order.

This is what the original file looks like:

04/26/16    Sega 3D Classics Collection
07/14/16    Batman: Arkham Underworld
06/24/16    Tokyo Mirage Sessions #FE

This is the desired contents of the new file

Batman: Arkham Underworld
Sega 3D Classics Collection 
Tokyo Mirage Sessions #FE

The problem with my code that it for some reason takes out the first word and sorts it. (for example instead of Batman: Arkham Underworld it would be Arkahm Underworld ).What I have tried is to subtract 1 from the indexOf(" ") and the info.length of my method but that did not seem to work. I know for sure that the method and the writing of the files work but the first word is missing ad will sort with the string without the first word. I'm not quite sure where the problem is with this code

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
    
public class Main {
    public static void main(String[] args) throws IOException {
        File file = new File("releasedates.txt");

        String[] arr = input(file);

        sortByName(arr);
    }

  public static String[]input (File file) throws FileNotFoundException{
    String[]arr = new String[3];
    Scanner sc = new Scanner(file);

    for(int i = 0; i < arr.length; i++){
      arr[i] = sc.nextLine();

    }
    return arr;
  }


    public static void sortByName(String[] info) throws IOException {
        String[] names = new String[3];

        for (int i = 0; i < info.length; i++) {
            names[i] = info[i].substring(info[i].indexOf(" "), info[i].length());
        }

        String temp;

        for (int j = 0; j < names.length; j++) {
            for (int i = j + 1; i < names.length; i++) {

                if (names[i].compareTo(names[j]) < 0) {
                    temp = names[j];
                    names[j] = names[i];
                    names[i] = temp;
                }
            }
        }

        FileWriter writer = new FileWriter("SortedByName.txt");
        for (String aString : names) {
            writer.write(aString);
        }
        writer.close();
    }

}
3
  • The "space" between date and the title... is it multiple spaces or a tab 0x09 character? That would completely explain the behavior your are seeing. Commented Apr 5, 2021 at 4:34
  • @JimGarrison the spacing between the date and the name is a tab spacing Commented Apr 5, 2021 at 4:35
  • Well then, you're looking for a space character and skip right over the tab to the first "real" space (0x20) character. If you can guarantee it's always going to be a tab, change the indexOf parameter. If not, use split(regex,limit) (with limit 2) instead. Commented Apr 5, 2021 at 4:38

1 Answer 1

1

The data has a tab between the date and title, and your indexOf() is looking for a space, so you skip right over the tab and first word of the title to the first 'real' space.

Instead, use

names[i] = (info[i].split("\\s+",2))[1];

This splits the string at the first string of one or more whitespace characters (which includes tab), limiting the output to 2 results, and the [1] selects the second element of the returned array, which will contain the title.

This works for both spaces and tab(s).

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the answer! However I have a question, once this is written inside the new file, how do I make it display each name is each line? Because right now it is displayed as one line. I want to break the line between each name
Output a newline ("\n") at the end of each line.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.