使用BufferedReader获取Java上的数据
我是java新手,需要帮助。 我正在用Java为一个大学活动制作一个Mp3播放器。首先,我需要获得所有艺术家的信息。所以我创建了艺术家课程:
public class Artist {
int id;
String Artist_name, country;
//This is the contructor
Artist(int id, String Artist_name, String country){
this.id = id;
this.Artist_name = Artist_name;
this.country = country;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getArtist_name() {
return Artist_name;
}
public void setArtist_name(String Artist_name) {
Artist_name = Artist_name;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
//This is the toString method. I must use it for this activity. It just links everything.
public String toString(){
return "ID: " + id + "\n" + "Name: " + artist_name + "\n" + "Country: " + country;
}
好的,现在我只需要一个ArtistTest类来测试一切。前两个实例必须是硬代码插入,然后第三个实例是使用BufferedReader的常规用户插入。这就是我的问题,我该怎么做?然后,我如何使用toString方法来打印这三位艺术家? 这是我到目前为止编写的代码(不完整,可能是错误的)
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class ArtistTest {
public static void main(String[] args) throws IOException{
Artist band1, band2;
//Just hardcode.
try{
band1 = new Artist(1,"The Beatles","England");
band2 = new Artist(2,"The Rolling Stones","England");
}
catch(Exception e){
System.out.println("Error");
}
//The problem...
BufferedReader band3= new BufferedReader(new InputStreamReader(System.in));
try{
System.out.print("ID:");
...
System.out.print("Artist Name:");
...
System.out.print("Country:");
...
}
catch(IOException e){
System.out.println("Erro");
}
toString(...)
}
}
谢谢你的建议
共 (0) 个答案