0

The title might be a little misleading but I am writing a piece of code that has this as the contents of the text file:

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

Essentially I want them to be in alphabetical order and it should make a brand new file that looks like this:

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

What I have tried is to use the indexOf() method to extract only the names of the list of games from my existing text file. I have also tried to store them in a new array to avoid confusion for the computer. The problem is that when I try to store the indexOf of the info array into a new array, the line gives an error of "cannot convert from int to string" and I am not sure on how to fix the error.

This is my code below:

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);

    output(file,arr);

    outputSort1(file, 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 output(File file, String[] info) throws IOException{

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

  public static void sortByMonth(String[]info){

    String temp;

    for (int j = 0; j < info.length; j++) {
      for (int i = j + 1; i < info.length; i++) {
 
  if (info[i].compareTo(info[j]) < 0) {
    temp = info[j];
    info[j] = info[i];
    info[i] = temp;
        }
     }
    }
  }

  public static void outputSort1(File file,String[] info) throws IOException{
    sortByMonth(info);
    FileWriter writer = new FileWriter("fileNameSorted1.txt");
    for(String aString:info){
        writer.write(aString);
    }
    writer.close();
}

public static void sortByName(String[]info){

    String[] names = new String[3];

      for(int i = 0; i < info.length; i ++){

          names[i] = info[i].indexOf(" " ,info.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;
          }
       }
      }
    }

}

2 Answers2

1

You've declared names array as String[] so you can't assign integer to it. indexOf method returns integer.

public static void sortByName(String[]info) {
    String[] names = new String[3];   //<-- declaration suggests store string
    for (int i = 0; i < info.length; i++) {
        names[i] = info[i].indexOf(" ", info.length);//<-you are assigning integer
    }

I think what you are trying to do is like this:

names[i] = info[i].substring(info[i].indexOf(" "), info[i].length());

Use java.nio APIs for file implementations as java.io apis are outdated. Also, if you use Stream operations then the implementation becomes much easier:

public class Test {
    public static void main(String[] args) throws Exception {
        Path file = Path.of("e:\\releasedates.txt");
        List<String> records = Files.readAllLines(file);

        List<String> sortedByName = records.stream()
                .map(s -> s.substring(s.indexOf(" "), s.length()))
                .sorted(String::compareTo)
                .collect(Collectors.toList());
        System.out.println(sortedByName);
        Files.write(Path.of("e:\\fileNameSorted.txt"), sortedByName);

        List<String> sortedByDate = records.stream().sorted(Test::compareDates)
                .map(s -> s.substring(s.indexOf(" "), s.length()))
                .collect(Collectors.toList());
        System.out.println(sortedByDate);
        Files.write(Path.of("e:\\fileDateSorted.txt"), sortedByDate);
    }

    public static int compareDates(String d1, String d2) {
        d1 = d1.substring(0, d1.indexOf(" "));
        d2 = d2.substring(0, d2.indexOf(" "));
        LocalDate ld1 = LocalDate.parse(d1,
                DateTimeFormatter.ofPattern("MM/dd/yy"));
        LocalDate ld2 = LocalDate.parse(d2,
                DateTimeFormatter.ofPattern("MM/dd/yy"));

        return ld1.compareTo(ld2);
    }
}
Community
  • 1
  • 1
the Hutt
  • 16,980
  • 2
  • 14
  • 44
0

Answer by @onkar ruikar is correct. indexOf returns int and you are trying to store it in String. I would like to extend his answer, where you can store the game/movie names in TreeSet instead of Array, so that by default it will be sorted in alphabetical order.

If you want to allow duplicate game/movie names, then you can use ArrayList and call Collections.sort(<array list>) method, which will sort the ArrayList in alphabetical order.

Here is the detailed answer of how can we sort Collections in Java: https://stackoverflow.com/a/8725470/3709922

Jignesh M. Khatri
  • 1,407
  • 1
  • 14
  • 22