Hi,

I am trying to store details in a Vector and then recall them using different search categories. I'm getting no errors but the details I enter are either not being stored or not being recalled. I don't know what one it is.

I attached the code rather than copy and paste it here as its a bit lengthy but please don't let that put you off having a look. It's a beginner's project and its 250 lines which isn't too long (I think). :confused:

If anyone could help me out on this it would be very much appreciated. :cheesy:

Thanks for your time,

Crisko.

Attachments
import java.io.*; //import API to allow input of characters
import java.util.Vector; //import API to allow Vector functionality

class Menu {

	public static void menudisplay() 
		
		{ //main method containing menu system
	
		String input = null; //initialising input string
		Integer iInput;
	
		BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
	
		System.out.println("Welcome to your CD Collection"); //commands to display menu on screen
		System.out.println("Select an option from the menu:");
		System.out.println("1: Add a new CD");
		System.out.println("2: Display CDs by artist");
		System.out.println("3: Display CDs by genre");
		System.out.println("4: Display all albums ascending by year");
		System.out.println("5: Display all albums descending by rating");
		System.out.println("6: Exit");
		
		try { //Reads keyboard input and acts accordingly
			input = stdin.readLine();
		}catch (IOException ex) {
			System.out.println("Problem reading from the keyboard");
		}
		iInput = Integer.valueOf(input); // convert String to Integer
		 
		
		switch (iInput) {
		case 1 : Inputter tempinputter = new Inputter();
			 		Inputter.newcd(); break;
		case 2 : ADisplay tempadisplay = new ADisplay();
			 		tempadisplay.artistdisplay(); break;
		case 3 : GDisplay tempgdisplay = new GDisplay();
			 		tempgdisplay.genredisplay(); break;
		}
	}
}

class YDisplay {
	
	int i;
	
	static String yearsearch;
	static Integer iyearsearch;

	public static void yeardisplay(){
		
		Details disc = new Details(); 
		
			BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
		
			System.out.println("What year would you like to search from? Results will be displayed in ascending order from searched year: ");
			
			try { //Reads keyboard input and acts accordingly
				yearsearch = stdin.readLine();
			}catch (IOException ex) {
				System.out.println("Problem reading from the keyboard");
			}
			
			iyearsearch = Integer.valueOf(yearsearch);
			
			for (int i=0; i<CDSystem.v.size(); i++) {
				disc = (Details)CDSystem.v.get(i);
			if ((iyearsearch).equals(disc.year)){
				
		System.out.println("Found detail(s):");
		System.out.println("CD Name: " + disc.name);
		System.out.println("CD Artist: " + disc.artist);
		System.out.println("CD Year: " + disc.year);
		System.out.println("CD Genre: " + disc.genre);
		System.out.println("CD Rating: " + disc.rating);
			
			}
		else{
		
		System.out.println("No CD info found. Returning to main menu");
}
}
Menu.menudisplay();		
 }
}

class GDisplay {
	
	int i;
	
	static String genresearch;

	public static void genredisplay(){
		
		Details disc = new Details(); 
		
			BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
		
			System.out.println("What genre would you like to search for?: ");
			
			try { //Reads keyboard input and acts accordingly
				genresearch = stdin.readLine();
			}catch (IOException ex) {
				System.out.println("Problem reading from the keyboard");
			}
			
			if ((genresearch).equals(disc.genre)){
			
			for (int i=0; i<CDSystem.v.size(); i++) {
				disc = (Details)CDSystem.v.get(i);
			
				
		System.out.println("Found details:");
		System.out.println("CD Name: " + disc.name);
		System.out.println("CD Artist: " + disc.artist);
		System.out.println("CD Year: " + disc.year);
		System.out.println("CD Genre: " + disc.genre);
		System.out.println("CD Rating: " + disc.rating);
			
			}}
		else{
		
		System.out.println("No CD info found. Returning to main menu");
}
Menu.menudisplay();			}

}

class ADisplay {
	
	int i;
	
	static String artistsearch;

	public static void artistdisplay(){
		
		Details disc = new Details(); 
		
			BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
		
			System.out.println("What artist would you like to search for?: ");
			
			try { //Reads keyboard input and acts accordingly
				artistsearch = stdin.readLine();
			}catch (IOException ex) {
				System.out.println("Problem reading from the keyboard");
			}
			
			for (int i=0; i<CDSystem.v.size(); i++) {
				disc = (Details)CDSystem.v.get(i);
			if ((artistsearch).equals(disc.artist)){
				
		System.out.println("Found detail(s):");
		System.out.println("CD Name: " + disc.name);
		System.out.println("CD Artist: " + disc.artist);
		System.out.println("CD Year: " + disc.year);
		System.out.println("CD Genre: " + disc.genre);
		System.out.println("CD Rating: " + disc.rating);
			
			}
		else{
		
		System.out.println("No CD info found. Returning to main menu");
}
}
Menu.menudisplay();			}

}
class Details {
	
		 String name = null;
		 String artist = null;
		 String year = null;
		 String genre = null;
	     String rating = null;
	     
}
class Inputter{

		static Integer iyear = 0;
	    static Integer irating = 0;

		public static void newcd() {
	
		BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
		
		Details disc = new Details ();
		
		System.out.println("CD Name: ");
		
		try { //Reads keyboard input and acts accordingly
			disc.name = stdin.readLine();
		}catch (IOException ex) {
			System.out.println("Problem reading from the keyboard"); }
			
		System.out.println("Artist: ");
		
		try { //Reads keyboard input and acts accordingly
			disc.artist = stdin.readLine();
		}catch (IOException ex) {
			System.out.println("Problem reading from the keyboard"); }
			
		System.out.println("Year of album: ");
		
		try { //Reads keyboard input and acts accordingly
			disc.year = stdin.readLine();
		}catch (IOException ex) {
			System.out.println("Problem reading from the keyboard"); }
			
		iyear = Integer.valueOf(disc.year);
		
		System.out.println("Genre: ");
		
		try { //Reads keyboard input and acts accordingly
			disc.genre = stdin.readLine();
		}catch (IOException ex) {
			System.out.println("Problem reading from the keyboard"); }
			
		System.out.println("Rating (1-5): ");
		
		try { //Reads keyboard input and acts accordingly
			disc.rating = stdin.readLine();
		}catch (IOException ex) {
			System.out.println("Problem reading from the keyboard"); }
			
		irating = Integer.valueOf(disc.rating);
		
		switch (irating) {
		case 1 : System.out.println("CD details entered successfully. Returning to main menu."); break;
		case 2 : System.out.println("CD details entered successfully. Returning to main menu."); break;
		case 3 : System.out.println("CD details entered successfully. Returning to main menu."); break;
		case 4 : System.out.println("CD details entered successfully. Returning to main menu."); break;
		case 5 : System.out.println("CD details entered successfully. Returning to main menu."); break;
		default: System.out.println("Invalid number. Please enter a number between 1-5 with 1 being min and 5 being max: "); break;
		}
		
		CDSystem.v.add(disc);
		
Menu.menudisplay();	}
}
	

public class CDSystem { //main class

 static Vector v = new Vector();

	public static void main (String [] args) {
	
Menu.menudisplay();}
		
}

Apologies to anyone who took the time to look over my code but I've figured out the problem myself. I know that seems like I didn't spend enough time on it myself before posting but I had spent hours all afternoon trying to solve it. The offending code was here:

for (int i=0; i<CDSystem.v.size(); i++) {
                disc = (Details)CDSystem.v.get(i);
            if ((iyearsearch).equals(disc.year)){

I should have closed the "for" loop before I started the "if" statement. Like this:

for (int i=0; i<CDSystem.v.size(); i++) {
                disc = (Details)CDSystem.v.get(i);}
            if ((iyearsearch).equals(disc.year)){

Sorry if I inconvenienced anyone and thanks for taking the time to look over it anyway if you did. I'm still an unexperienced programmer so I was instinctively going after the major problems and subconsciously taking the compile errors semi-literally.

Crisko.

This question has already been answered. Start a new discussion instead.