Staredit Network > Forums > Technology & Computers > Topic: Java Problem
Java Problem
Mar 12 2009, 5:08 am
By: MillenniumArmy  

Mar 12 2009, 5:08 am MillenniumArmy Post #1



So for one of my classes we are to write a java program which creates a catalog of song titles and artists from our file "songs.txt". I keep running into this problem but I have no idea how to resolve it. Any help appreciated :)

Quote from SongCatalog.java
import java.util.*;
import java.io.*;

class Song implements Comparable
{
public String artist;
public String title;

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

public boolean equals (Object obj)
{
if (obj instanceof Song)
{
Song aSong = (Song) obj;
return artist.equals(aSong.artist) &&
title.equals(aSong.title);
}
else
{
return false;
}
}

public String toString ()
{
String str = "Artist: " + artist + "\n" + "Title: " + title;
return str;
}

public int compareTo (Object obj)
{
Song aSong = (Song) obj;
int comp = artist.compareTo(aSong.artist);
return ((comp == 0) ? title.compareTo(aSong.title) : comp);
}
}

class SongList
{
private Song[] catalog;
private int numSongs;

public SongList ()
{
catalog = new Song [100];
numSongs = 0;
createCatalog();
}

// Increase size of array if more songs are added
private void incrCatalog ()
{
Song[] newCatalog = new Song[catalog.length + 50];
for (int i = 0; i < catalog.length; i++)
{
newCatalog[i] = catalog[i];
}
catalog = newCatalog;
}

// This method reads the file and populates the array
private void createCatalog () throws IOException
{
File inFile = new File ("songs.txt");
Scanner sc = new Scanner(inFile);
while (sc.hasNextLine());
{
String artist = sc.nextLine();
artist = artist.substring(8);
String songTitle = sc.nextLine();
songTitle = songTitle.substring(7);
String space = sc.nextLine();

catalog[numSongs] = new Song(artist, songTitle);
numSongs++;
}
sc.close();
}

// This method reads data from a file
public void readFile (String aFile) throws IOException
{
File inFile = new File (aFile);
Scanner sc2 = new Scanner(inFile);
while (sc2.hasNextLine());
{
String artist = sc2.nextLine();
artist = artist.substring(8);
String songTitle = sc2.nextLine();
songTitle = songTitle.substring(7);
String space = sc2.nextLine();

catalog[numSongs] = new Song(artist, songTitle);
numSongs++;
}
sc2.close();
}

// This method adds songs to the array
public void addSong (String artist, String title)
{

}

// This method deletes all songs by an artist
public void deleteByArtist (String artist)
{

}

// This method deletes a given song
public void deleteByTitle (String title)
{

}

// This method searches for all songs by an artist
public void searchByArtist (String artist)
{

}

// This method searches for a given song
public void searchByTitle (String title)
{

}

// This method displays all entries in the collection
public void displayCatalog ()
{
for (int i = 0; i < numSongs; i++)
{
System.out.println (catalog[i]);
System.out.println ();
}
}

// This method over-writes the file
public void writeFile ()
{

}
// Other methods that you may need
}

public class SongCatalog
{
public static void main (String[] args) throws IOException
{
SongList album = new SongList();

// Create menu
boolean quitProgram = false;
while (!quitProgram)
{
System.out.println("Song Catalog Menu" + "\n");
System.out.println("1. Import songs from a file" + "\n");
System.out.println("2. Add a song" + "\n");
System.out.println("3. Delete a song" + "\n");
System.out.println("4. Search for a song" + "\n");
System.out.println("5. Display all songs" + "\n");
System.out.println("6. Exit program" + "\n");
System.out.println("Enter selection (1 - 6): ");
Scanner sc0 = new Scanner(System.in);
int menuSelect = sc0.nextInt();
if (menuSelect < 7 || menuSelect > 0)
{
if (menuSelect == 1)
{
System.out.println("Enter name of file to import songs from: ");
String checkFile = sc0.nextLine();
File nameOfFile = new File(checkFile);
if (nameOfFile.canRead() && nameOfFile.canWrite() && nameOfFile.exists())
{
album.readFile(checkFile);
}
else
{
System.out.println("Invalid file." + "\n");
}
}
else if (menuSelect == 2)
{
System.out.println("Enter name of Artist: ");
String addArtist = sc0.nextLine();
System.out.println("Enter name of Song: ");
String addSong = sc0.nextLine();
album.addSong(addArtist, addSong);
}
else if (menuSelect == 3)
{
String delRead;
do
{
System.out.println("Delete by artist or title (A or T): ");
delRead = sc0.nextLine();
if (delRead == "A" || delRead == "a")
{
System.out.println("\n" + "Enter Artist: ");
String delArtist = sc0.nextLine();
album.deleteByArtist(delArtist);
}
else if (delRead == "T" || delRead == "t")
{
System.out.println("\n" + "Enter Title: ");
String delTitle = sc0.nextLine();
album.deleteByTitle(delTitle);
}
else
{
System.out.println("Invalid selection. Try again.");
}
}
while (!(delRead == "A" || delRead == "a" || delRead == "T" || delRead == "t"));
}
else if (menuSelect == 4)
{
String searchRead;
do
{
System.out.println("Search by artist or title (A or T): ");
searchRead = sc0.nextLine();
if (searchRead == "A" || searchRead == "a")
{
System.out.println("\n" + "Enter Artist: ");
String searchArtist = sc0.nextLine();
album.searchByArtist(searchArtist);
}
else if (searchRead == "T" || searchRead == "t")
{
System.out.println("\n" + "Enter Title: ");
String searchTitle = sc0.nextLine();
album.searchByTitle(searchTitle);
}
else
{
System.out.println("Invalid selection. Try again.");
}
}
while (!(searchRead == "A" || searchRead == "a" || searchRead == "T" || searchRead == "t"));
}
else if (menuSelect == 5)
{
album.displayCatalog();
}
}
}
}
}
The error that I keep getting for the line highlighted in red (well actually, the line right below it) is "unreported exception java.io.IOException; mustbe caught or declared to be thrown.

I still have not finished coding the rest of it yet, but I want to at least get past this part.



None.

Mar 12 2009, 5:24 am cheeze Post #2



Yeah. It means you need to catch or throw it.
You know, TRY CATCH FINALLY blocks?



None.

Mar 12 2009, 6:15 am MillenniumArmy Post #3



Oh wait, nvm I got it.



None.

Mar 12 2009, 6:21 am mikelat Post #4



Doesn't java usually have the ability to explain a more detailed message? You put it in a try catch block like

try
{
//code
}
catch(exception e)
{
JOptionPane.showMessageDialog(null, e.Message);
}

I donno, something like that.

Just for the future.



None.

Options
  Back to forum
Please log in to reply to this topic or to report it.
Members in this topic: None.
[01:37 pm]
Vrael -- jesus christ that was 2005?
[09:19 am]
Linekat -- cool
[01:56 am]
Oh_Man -- cool bit of history, spellsword creator talking about the history of EUD ^
[09:24 pm]
Moose -- denis
[05:00 pm]
lil-Inferno -- benis
[2024-4-19. : 10:41 am]
v9bettel -- Nice
[2024-4-19. : 1:39 am]
Ultraviolet -- no u elky skeleton guy, I'll use em better
[2024-4-18. : 10:50 pm]
Vrael -- Ultraviolet
Ultraviolet shouted: How about you all send me your minerals instead of washing them into the gambling void? I'm saving up for a new name color and/or glow
hey cut it out I'm getting all the minerals
[2024-4-18. : 10:11 pm]
Ultraviolet -- :P
Please log in to shout.


Members Online: NudeRaider, fithplanet