これがすべて理解するのが難しいと思われる場合は申し訳ありませんが、私はプログラミングが初めてで、いくつかの本やWebサイトを調べました.私の理解から、私がやろうとしていることはうまくいくはずです. 私が取り組んでいる課題は、クラスを呼び出すクラスです。すべてのコードをここに入れることなく、不明確な領域でできるだけ具体的にしようとします。null ポインターの例外は、次の特定のコード行で発生します。
if(CDList[i].getArtist().equals(artist) == true)
//CDList は CD オブジェクトの配列です (別のクラスで作成されます)
//getArtist() は、文字列を返す CD クラスのメソッドです。
// equals() のアーティストは、ユーザーが入力した Scanner オブジェクトであり、文字列でもあります
この特定のメソッドのポイントは、配列 CDList を検索し、保存されているアーティスト文字列をスキャンされたアーティスト文字列と比較し、タイトルについても同じであることです。見つかった場合、配列のその部分の内容が削除されます。それが役立つ場合は、メソッドの残りの部分を次に示します。
void delete()
{
Scanner input = new Scanner(System.in);
System.out.println("Enter artist and title to be deleted: ");
String artist = input.nextLine();
String title = input.nextLine();
for(int i = 0; i <= numOfCDs; i++)
{
if(CDList[i].getArtist().equals(artist) == true)
{
for(int j = 0; j <= numOfCDs; j++)
{
if(CDList[j].getTitle().equals(title) == true)
{
System.out.println("Found CD: " + CDList[j].getArtist() + " " +
CDList[j].getTitle());
System.out.println("Would you like to delete it? Y/1 N/0 ");
if(input.nextInt() == 1)
{
CDList[j] = null;
numOfCDs--;
}
}
else
System.out.println("CD not found.");
}
}
else
System.out.println("CD not found.");
}
}
申し訳ありませんが、これが残りのコードです。あまりにも多いと思ったので、省略します。
CD クラス:
package assignment3;
public class CD
{
public String artist;
public String title;
private tracklist listOfTracks = new tracklist();
CD(String artistName, String titleName)
{
artist = artistName;
title = titleName;
}
public String getArtist()
{
return artist;
}
public String getTitle()
{
return title;
}
public boolean addTrack(String trackInfo)
{
boolean result = false;
if(listOfTracks.add(trackInfo) == true)
result = true;
return result;
}
public int numTracks()
{
int count = listOfTracks.count();
return count;
}
public void display()
{
System.out.println(" ");
System.out.println(getArtist() + " : " + getTitle());
listOfTracks.display(7);
}
}
トラックリスト クラス:
package assignment3;
public class tracklist
{
public String[] tracks;
public int numElements;
tracklist()
{
tracks = new String[99];
numElements = 0;
}
public boolean add(String track)
{
boolean result = true;
int index = 0;
while(tracks[index] != null)
{
index++;
}
tracks[index] = track;
numElements++;
if(numElements > 99)
result = false;
return result;
}
public int count()
{
return numElements;
}
public void display(int indent)
{
for(int i = 1; i < numElements; i++)
{
System.out.print(i);
if(i >= 10)
{
for(int j = 0; j < (indent - 1); j++)
{
System.out.print(" ");
}
}
else
{
for(int j = 0; j < indent; j++)
{
System.out.print(" ");
}
}
System.out.println(tracks[i]);
}
}
}
CDList クラス:
package assignment3;
import java.util.Scanner;
public class CDList
{
public int numOfCDs;
private CD[] CDList;
private int front,rear;
CDList(int size)
{
CDList = new CD[size];
numOfCDs = 0;
front = 0;
rear = size - 1;
}
boolean add()
{
boolean result;
Scanner input = new Scanner(System.in);
System.out.println("Enter the Artist Name and CD Title: ");
CD userCD = new CD(input.nextLine(), input.nextLine());
System.out.println("Enter the number of tracks: ");
int trackNumber = input.nextInt();
System.out.println("Enter your track titles: ");
for(int i = 0; i <= trackNumber; i++)
{
userCD.addTrack(input.nextLine());
}
if(rear == front)
result = false;
else
{
if(CDList[rear] != null)
rear--;
else
CDList[rear] = userCD;
result = true;
}
return result;
}
void delete()
{
Scanner input = new Scanner(System.in);
System.out.println("Enter artist and title to be deleted: ");
String artist = input.nextLine();
String title = input.nextLine();
for(int i = 0; i <= CDList.length - 1; i++)
{
if((CDList[i].getArtist().equals(artist)) &&
(CDList[i].getTitle().equals(title)))
{
System.out.println("Found CD of: " + CDList[i].getArtist() + " " +
CDList[i].getTitle());
System.out.println("Would you like to delete it? Y/1 N/0 ");
if(input.nextInt() == 1)
{
CDList[i] = null;
numOfCDs--;
}
}
else
System.out.println("CD not found.");
}
}
void SortArtist()
{
CD temp = new CD(" ", " ");
for(int i = 0; i < numOfCDs; i++)
if(CDList[i].getArtist().compareTo(CDList[i + 1].getArtist()) < 0)
{
temp = CDList[i];
CDList[i] = CDList[i + 1];
CDList[i + 1] = temp;
}
}
void SortTitle()
{
CD temp = new CD(" ", " ");
for(int i = numOfCDs; i > 0; i--)
{
int x = 0;
for(int j = 1; j <= i; j++)
{
if(CDList[i].getTitle().compareTo(CDList[i + 1].getTitle()) < 0)
x = j;
}
temp = CDList[x];
CDList[x] = CDList[i];
CDList[i] = temp;
}
}
void Display()
{
for(int i = 0; i <= numOfCDs; i++)
{
while(CDList[i] == null)
i++;
CDList[i].display();
}
}
int size()
{
return numOfCDs;
}
}