我该如何开始。我知道如何制作这种方法,但我不知道究竟要放入什么内容。我正在制作一个歌曲课程,其中包含歌曲,艺术家和标题的年份,等级。我从另一个txt文件中获取此信息。 Song应该有一个静态解析方法,它采用带制表符分隔的部分的字符串并返回一个Song对象。 这是我开始使用的代码,但我不知道这是否非常合法。我也不知道它是否真的有效。
public class Billboard {
private int year;
private int rank;
private String artist;
private String title;
public void setYear(int Y){
this.year = Y;
}
public void setRank(int R){
this.rank = R;
}
public void setArtist(String A){
this.artist = A;
}
public void setTitle(String T){
this.title = T;
}
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 void Song (String Line) {
String[] token = line.split("\t");
title = token[0];
artist = token[1];
year = Integer.parseInt(token[2]);
}
}
答案 0 :(得分:1)
您不能将此方法设为静态,因为它访问对象变量:title, artist, year
因此,您需要使用构造函数,或将对象传递给方法:
public Billboard (String Line) {
String[] token = line.split("\t");
this.title = token[0];
this.artist = token[1];
this.year = Integer.parseInt(token[2]);
}
或者
public static Billboard parseString(String Line) {
return parseString(line, new Billboard());
}
public static Billboard parseString(String Line, Billboard billboard) {
String[] token = line.split("\t");
billboard.setTitle(token[0]);
billboard.setArtist(token[1]);
billboard.setYear(Integer.parseInt(token[2]));
return billboard;
}
答案 1 :(得分:1)
宋班:
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import utils.Utils;
public class Song {
private String title;
private String artist;
private int rank;
private int year;
public Song(String title, String artist, int rank, int year){
this.title = title;
this.artist = artist;
this.year = year;
this.rank = rank;
}
public String getTitle() {
return title;
}
public String getArtist() {
return artist;
}
public int getRank() {
return rank;
}
public int getYear() {
return year;
}
public static Song parseLine(String line, String delimiter) {
String[] parts = Utils.splitLine(line, delimiter);
return new Song(parts[0], parts[1], Integer.parseInt(parts[2]),Integer.parseInt(parts[3]));
}
public static List<Song> readFile(String dataFile) {
List<Song> lines = new ArrayList<Song>();
String line = null;
try{
FileReader fr = new FileReader(new File(dataFile));
BufferedReader br = new BufferedReader(fr);
while((line = br.readLine()) != null){
lines.add(parseLine(line, "\t"));
}
}catch(IOException e){
e.printStackTrace();
}
return lines;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((artist == null) ? 0 : artist.hashCode());
result = prime * result + rank;
result = prime * result + ((title == null) ? 0 : title.hashCode());
result = prime * result + year;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Song other = (Song) obj;
if (artist == null) {
if (other.artist != null)
return false;
} else if (!artist.equals(other.artist))
return false;
if (rank != other.rank)
return false;
if (title == null) {
if (other.title != null)
return false;
} else if (!title.equals(other.title))
return false;
if (year != other.year)
return false;
return true;
}
}
Utils.splitLine():
public class Utils {
public static String[] splitLine(String line, String delimiter) {
if(delimiter.equals("|")){
delimiter = "\\|";
}
if(delimiter.equals(".")){
delimiter = "\\.";
}
String[] rtn = line.split(delimiter);
for(int i=0;i<rtn.length;i++){
rtn[i] = rtn[i].trim();
}
return rtn;
}
}
试验:
import static org.junit.Assert.*;
import java.util.ArrayList;
import java.util.List;
import org.junit.Test;
public class HelpTest {
@Test
public void test_parseLine() {
// tabs betwen the title artist year and rank
String line = "Run Runaway Slade 20 1984";
Song expected = new Song("Run Runaway","Slade", 20, 1984);
Song actual = Song.parseLine(line, "\t");
assertEquals(expected, actual);
}
@Test
public void test_readFile() {
String path = "src/testSongData";
Song song = new Song("Run Runaway","Slade", 20, 1984);
List<Song> expected = new ArrayList<Song>();
expected.add(song);
List<Song> actual = Song.readFile(path);
assertEquals(expected, actual);
}
}
用于src/testSongData
的文字文件readFile()
:
Run Runaway Slade 20 1984
答案 2 :(得分:0)
public void Song (String Line)
将其更改为
public static Song createSong (String aLine_in)
请注意更改:
static
已添加关键字。
方法名称以小写字母开头。 (找到更合适的名字)
局部变量/方法参数以小写字母开头。因为它是一个参数_in
是前缀。
您可以按如下方式调用静态方法:
Song aSong = Song.createSong("...\t...\t...");