gpt4 book ai didi

java - 为什么我的输出文件是空的?

转载 作者:行者123 更新时间:2023-11-30 08:55:16 24 4
gpt4 key购买 nike

当我遇到一个错误时,我正在为我的 AP 计算机科学类(class)开发一个允许用户对音乐进行排序的项目。程序解析这种格式的一行 -> 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;
}
}

为什么我输出信息时文件是空白的?

最佳答案

您永远不会关闭您的outFile。添加 outfile.close() 添加程序的结尾。

关于java - 为什么我的输出文件是空的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29115896/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com