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.
[10:41 am]
ManCubuS -- Seems like something's broken regarding the site then. Sadge...
[07:36 am]
Oh_Man -- have i ever told u guys my SC2 pro-gamer story
[11:30 pm]
Ultraviolet -- That's strange, it looks like you targeted the base image file (sometimes people make the mistake of just using the imgur page link which doesn't work for embedding) but it still didn't work it sounds like.
[2026-7-26. : 3:34 pm]
ManCubuS -- Used : " [img=https://i.imgur.com/gpOjTuD.jpeg] "
[2026-7-26. : 3:28 pm]
ManCubuS -- Even tried with imgur, doesn't attach.
[2026-7-26. : 3:14 pm]
ManCubuS -- I should try a workaround, thanks! Sadly imgur is blocked in my country for some unknown reason.
[2026-7-26. : 3:05 pm]
NudeRaider -- I think SEN has trouble with https or hotlinking is forbidden. It certainly is nothing about the size, it just fails to get any size. You can get it working if you use imgur and include the actual image link for the img tag. Like this: [img=https://i.imgur.com/ddYqXZa.jpeg]
[2026-7-26. : 11:15 am]
ManCubuS -- Hey fellas! Anyone knows why when I try to upload an image it says "failed to get image size"? I even tried making it so small. Imgur isn't working here and I tried postimg no luck.
[2026-7-23. : 6:40 am]
Oh_Man -- true
[2026-7-23. : 3:16 am]
RIVE -- Still annoyed there was never a trigger to stop or pause wavs.
Please log in to shout.


Members Online: Dem0n