为什么我的代码打印为空文件?

时间:2015-03-19 02:18:54

标签: java string class object int

当我遇到一个错误时,我正在为我的AP计算机科学课程开发一个允许用户对音乐进行排序的项目。该程序以这种格式解析一行 - > year (tab) rank (tab) artist (tab) title (tab)。然后它可以按年份,等级,艺术家和/或标题对歌曲进行排序或过滤。此信息通过格式为:

的输入文件加载
2008    50  Ashley Tisdale  He Said, She Said
2008    123 Taylor Swift    Teardrops On My Guitar
2008    233 Finger Eleven   Paralyzer
2008    258 Paramore    Misery Business
...

这就是我的尝试。

import java.util.*;
import java.io.*;
import java.util.Arrays;

public class GazillionSongs {
   public static void main(String[] args) throws FileNotFoundException {
      System.out.println("Hello and welcome to my AP Computer Science Music Sorting Project!"); 
      Scanner kb = new Scanner(System.in);

      System.out.print("Enter the input file: ");
      String in = kb.nextLine();
      Scanner inFile = new Scanner(new File(in));

      System.out.print("Enter sort/filter command: ");
      String command = kb.nextLine();

      System.out.print("Enter an output file: ");
      String out = kb.nextLine();

      File f = new File(out);

      if (f.exists()) {
         System.out.println("Error: output file already exists...");
      } else {
         PrintStream outFile = new PrintStream(new File(out));

         ArrayList<String> lines = new ArrayList<String>();

         while (inFile.hasNextLine()) {
            String line = inFile.nextLine();
            lines.add(line);  
         }

         ArrayList<Song> songs = new ArrayList<Song>();

         Scanner allCommands = new Scanner(command);

         for(int i = 0; i < lines.size(); i++) {
            int year = 0;
            int rank = 0;
            String artist = "";
            String title = "";
            Song song = new Song(year, rank, artist, title);
            song.parse(lines.get(i));
            songs.add(song);
         }

         SongCollection songCollection = new SongCollection(songs);

         while (allCommands.hasNext()) {
            for(int i = 0; i < songs.size(); i++) {
               String command2 = allCommands.next();
               String[] tokens = command2.split(":");
               if (tokens[0].equals("year")) {
                  int min = 0;
                  int max = 0;
                  Range range = new Range(min, max);
                  range.parse(tokens[1]);
                  songCollection.filterYear(range);
               }
               if (tokens[0].equals("rank")) {
                  int min = 0;
                  int max = 0;
                  Range range = new Range(min, max);
                  range.parse(tokens[1]);
                  songCollection.filterRank(range);
               }
               if (tokens[0].equals("artist")) {
                  songCollection.filterArtist(tokens[1]);
               }
               if (tokens[0].equals("title")) {
                  songCollection.filterTitle(tokens[1]);
               }
            }
         }
         outFile.print(songCollection.toString());         
      }
   }                  
}

    import java.io.*;
    import java.util.*;

    public class Song {
       private static int year;
       private static int rank;
       private static String artist;
       private static String title;

       public Song(int year, int rank, String artist, String title) {
          this.year = year;
          this.rank = rank;
          this.artist = artist;
          this.title = title;
       }

       public static Song parse(String s) {
          String[] tokens = s.split("\t");
          year = Integer.parseInt(tokens[0]);
          rank = Integer.parseInt(tokens[1]);
          artist = tokens[2];
          title = tokens[3];
          Song song = new Song(year, rank, artist, title);
          return song;
       }

       public int getYear() {
          return this.year;
       }

       public int getRank() {
          return this.rank;
       }

       public String getArtist() {
          return this.artist;
       }

       public String getTitle() {
          return this.title;
       }

       public String toString() {
          String convertString = year + "/t" + rank + "/t" + artist + "/t" + title;
          return convertString;
       }     
    }

import java.util.*;

public class Range {
   private int min;
   private int max;

   public int getMin() { 
      return min; 
   }
   public int getMax() { 
      return max; 
   }

   public Range(int  min, int max) {
      this.min = min;
      this.max = max;
   }

   public static Range parse(String s) {
      String[] range = s.split("-");
      int min = Integer.parseInt(range[0]);
      int max = 1;

      try {
         max = Integer.parseInt(range[1]);
      } catch(ArrayIndexOutOfBoundsException err10) {
         max = Integer.parseInt(range[0]);
      }  

      if(min > max){
         return new Range(max,min);   
      } else {
         return new Range(min,max);
      }
   }

   public boolean contains(int n) {
      if(n <= max && n >= min) {
         return true;
      } else {
         return false;
      }
   }
}

import java.util.*;

public class SongCollection {

   private ArrayList<Song> songs = new ArrayList<Song>();

   public SongCollection(ArrayList<Song> songs) {
      this.songs = songs;
   }

   public ArrayList<Song> getList() {
      return songs;
   }

   public void filterYear(Range r) {
      for(int i = 0; i < songs.size(); i++) {
         if(!r.contains(songs.get(i).getYear())) {
            songs.remove(i);
            i--;
         }
      }
   }

   public void filterRank(Range r) {
      for(int i = 0; i < songs.size(); i++) {
         if(!r.contains(songs.get(i).getRank())) {
            songs.remove(i);
            i--;
         }
      }
   }

   public void filterArtist(String s) {
      for(int i = 0; i < songs.size(); i++) {
         if(!songs.get(i).getArtist().toLowerCase().contains(s.toLowerCase())) {
            songs.remove(i);
            i--;
         }
      }
   }

   public void filterTitle(String s) {
      for(int i = 0; i < songs.size(); i++) {
         if(!songs.get(i).getTitle().toLowerCase().contains(s.toLowerCase())) {
            songs.remove(i);
            i--;
         }
      }
   }

   public void sortYear() {
      for(int i = 1; i <= songs.size(); i++) {
         Song temp = songs.get(i);
         int j;
         for (j = i - 1; j >= 0 && temp.getYear() < songs.get(j).getYear(); j--) {
            songs.set((j + 1), songs.get(j));
            songs.set((j), temp);
         }
      }
   }

   public void sortRank() {
      for(int i = 1; i <= songs.size(); i++) {
         Song temp = songs.get(i);
         int j;
         for (j = i - 1; j >= 0 && temp.getRank() < songs.get(j).getRank(); j--) {
            songs.set((j + 1), songs.get(j));
            songs.set((j), temp);
         }
      }
   }

   public void sortTitle() {
      for(int i = 1; i <= songs.size(); i++) {
         Song temp = songs.get(i);
         int j;
         for (j = i - 1; j >= 0 && temp.getTitle().toLowerCase().compareTo(songs.get(j).getTitle().toLowerCase()) < 0; j--) {
            songs.set((j + 1), songs.get(j));
            songs.set((j), temp);
         }
      }
   }

   public void sortArtist() {
      for(int i = 1; i <= songs.size(); i++) {
         Song temp = songs.get(i);
         int j;
         for (j = i - 1; j >= 0 && temp.getArtist().toLowerCase().compareTo(songs.get(j).getArtist().toLowerCase()) < 0; j--) {
            songs.set((j + 1), songs.get(j));
            songs.set((j), temp);
         }
      }
   }

   public String toString() {
      String stringOfSong = "";
      for(int i = 0; i < songs.size(); i++) {
         stringOfSong += songs.get(i).toString() + "\n";
      }
      return stringOfSong;
   }
}

输出信息时,为什么文件空白?我真的很感激帮助,因为我一直试图解决这个问题几天了?

1 个答案:

答案 0 :(得分:0)

这里有一些重大错误,我个人不希望在APCS类的代码中找到这些错误。

  • Song 类中,您有一个重要的禁忌事件:所有应该是实例字段的字段实际上都是静态的。其次,你的 Song.parse(String s)方法正在返回(虽然搞砸了)单例。最好删除所有静态修饰符,并简单地让parse方法返回自己( this )。
  • Song没有默认构造函数,你在 GazillionSongs .38开始的for循环中有点挣扎。
  • SongCollection类中的所有过滤方法都被破坏,即它们不起作用,因为您要从正在遍历的列表中删除项目。这很重要!因为你没有从中获得任何错误(因为你没有这么说),所以可以看出它们永远不会被成功调用。这当然进一步表明您没有以单元测试格式测试代码,甚至只是简单地通过并运行它。在这一点上,我已经停止尝试评估您的代码,并且只能为您提供以下建议:在寻求帮助之前先检查您先写的所有内容。即便如此,请问你的老师,因为他们可以亲自向你解释,并且很可能会从中受到较少的麻烦。

如果你真的想要我,那么我只想说你得到一份空白文件的原因,除了在很多这些事情中完全缺乏正确性(不是那么糟糕;你是一名学生之后所有,并且这些编码方式可以修复),就是在行 GazillionSongs .50,你开始一个while循环,出错了;在那里,表示while循环永远不会运行,并且会打印一个空的ArrayList 歌曲

我希望这有所帮助,并为你揭开一点光明。祝你好运,我敢说,你可能需要它。我所有的后悔道歉。

// Edit: the comments are absolutely correct. Closing the file tends to help, but I felt it was 
// self-explanatory amongst the many other errors.