Hi,

I have this really important project where in, I must perform the following tasks.

VIEW - view records that are present in the database using a "primary key"
ADD - add new records to the database
EDIT - a specific line in the said record within the database
Under edit, user should be able to choose what to edit: Student ID, Name, Course, Address and School.
DELETE - delete a record

Other requirements:
1. Database should NOT be a text file, I should ONLY use an array to store the data.
2. Can use, bufferedReader or JOptionPane, but preferably, I'm using JOptionPane.
3. For example, if I have 2 set of records:

Student ID: 1
Name: Mark Wall
Course: BSBA
Address: San Diego, California
School: UCLA

Student ID: 2
Name: Jim Raynor
Course: BSAS
Address: Los Angeles, California
School: UCLA

During the VIEW/I will input the Student ID that I am looking for. If i input 1. It should show this:
Student ID: 1
Name: Mark Wall
Course: BSBA
Address: San Diego, California
School: UCLA

If not existent, it will prompt to try again.

So far, here's what I've got:

import javax.swing.JOptionPane;

public class StudRecvoid 
{
	public void Info ()
	{
	// main switch dec start
	int num = 0;
	String str = "";

	
		// main case 1 dec start
		int numa = 0;
		String stra = "";
		// 	main case 1 dec end
		
		// 	main case 2 dec start

		int numb = 0;
		String strb = "";
		String strb1 = "";
		String strb2 = "";
		String strb3 = "";
		String strb4 = "";
		
		// 	main case 2 dec end
		
		// main case 3 dec start
		int numc = 0;
		String strc = "";
		// main case 3 dec end
		
		// main case 4 dec end
		int numd = 0;
		String strd = "";
		// main case 4 dec end
		
	// main switch dec end
		
			
	System.out.println("What do you want to do?");
	System.out.println("1 -> View");
	System.out.println("2 -> Add");
	System.out.println("3 -> Edit");
	System.out.println("4 -> Delete");
		
	JOptionPane.showMessageDialog(null, "Please choose a number from 1 to 4");
	
	
	str = JOptionPane.showInputDialog("Choose an action to take:");
	num = Integer.parseInt(str);

	switch (num) // main switch start
		{
		case 1: // main case 1
			JOptionPane.showMessageDialog(null, "You have chosen to VIEW Student Records");
			stra = JOptionPane.showInputDialog("Input the entry's Student ID:");
			numa = Integer.parseInt(stra);
		
				switch (numa) // start of Subcases for main case 1
					{
					case 1: // subcase 1.1
						JOptionPane.showMessageDialog(null, "Record 1");
						break;
					case 2: // subcase 1.2
						JOptionPane.showMessageDialog(null, "Record 2");
						break;
					case 3: // subcase 1.3
						JOptionPane.showMessageDialog(null, "Record 3");
						break;
					case 4: // subcase 1.4
						JOptionPane.showMessageDialog(null, "Record 4");
						break;
					default: // sub default 1
						JOptionPane.showMessageDialog(null, "RECORD does not exist, please run the program again.");
						break;
					}	//end of subcases for main case 1
								
			break;
		case 2: // main case 2
			JOptionPane.showMessageDialog(null, "You have chosen to ADD a new Student Record");
			
			strb = JOptionPane.showInputDialog("Input Student ID:");
			numb = Integer.parseInt(strb);

			strb1 = JOptionPane.showInputDialog("Input Full Name:");
						
			strb2 = JOptionPane.showInputDialog("Input Course:");
						
			strb3 = JOptionPane.showInputDialog("Input Address:");
						
			strb4 = JOptionPane.showInputDialog("Input School:");
			
			JOptionPane.showMessageDialog(null, "Record succesfuly added!");
			int arrayinfonum[]={numb};
			String arrayinfostr[]={strb1, strb2, strb3, strb4};
						
		
				System.out.println ();
				System.out.println ("Student ID: " + numb);
				System.out.println ("Name:       " +strb1);
				System.out.println ("Course:     " +strb2);
				System.out.println ("Address:    " +strb3);
				System.out.println ("School:     " +strb4);
				
			break;
		case 3: // main case 3
			System.out.println("Edit");
			
			break;
		case 4: // main case 4
			System.out.println("Delete");
			
			break;
		default: // main default
			System.out.println("Please choose from 1 to 4 only");
			break;
		
		} // main switch end
	}
}

I really need help on this. Because this will basically decide if I pass or fail for this semester in my major. Thank you very much!!!

Okay? So what problems are you having with it? BTW we are not just going to give you any "missing" pieces.

Okay? So what problems are you having with it? BTW we are not just going to give you any "missing" pieces.

Thanks, I fully understand that. What I'm having a problem about is how I could use the entry "Student ID" as a "primary key" to display all the other entries with it as 1 whole record. And how to store the values into 1 array. I'm having a really hard time explaining this, I just started Java several weeks ago. And my last programming subject was 3 years ago, so I'm sorry for being such a noob. Thanks!

All of the information that pertains to a "Student" is a separate class "Student", and you store the information in a Student[], of course, and the "StudentID" would then be the array index.

All of the information that pertains to a "Student" is a separate class "Student", and you store the information in a Student[], of course, and the "StudentID" would then be the array index.

So, it's like a index within an array within a class. Okay, I'll search on that. Uhm, but how do I search the "primary key" which in this case is the Student ID?

Just simply save the Student in the same index of the array as the StudentsID is. Regardless of whuich was assigned first. And, since the Student exists in the array at the index that matches the StudentID, and it is StudentID you need to "search" on, you simply retreive that index from the array.

Just simply save the Student in the same index of the array as the StudentsID is. Regardless of whuich was assigned first. And, since the Student exists in the array at the index that matches the StudentID, and it is StudentID you need to "search" on, you simply retreive that index from the array.

Thanks man! Would you mind to show me a simple example of the index within an array?Then probably, I can go on with it. If that would not be too much to ask.

Thanks man,

My program is still saving just 1 set of arrays. Here's what I've got now:

import javax.swing.JOptionPane;

public class StudRecvoid
{
	public int index = 0;		//index of 
	
	// 	main case 2 starts
	public int mxl = 999;
	
	public int numb[] = new int[mxl];
	public String strb[] = new String[mxl]; //ID
	public String strb1[] = new String[mxl]; //Full name
	public String strb2[] = new String[mxl]; //Course
	public String strb3[] = new String[mxl]; //Address
	public String strb4[] = new String[mxl]; // School
	// 	main case 2 end
	

	public void display()
	{
		System.out.println ();
		System.out.println ("Student ID: " + strb[index]);
		System.out.println ("Name:       " + strb1[index]);
		System.out.println ("Course:     " + strb2[index]);
		System.out.println ("Address:    " + strb3[index]);
		System.out.println ("School:     " + strb4[index]);
		System.out.println ();
	}
	
	
	public void confirm ()
	{
		// Start of confirmation
		String conf = "";	
		conf = JOptionPane.showInputDialog("would you like to go back to the menu?Y/N");
		
		if (conf.equalsIgnoreCase("Y"))
		{
			Info ();
		}
		else
		{
			System.out.println();
		}
		// End of confirmation
	}
	
	
	public void Info ()
	{
	// main switch start
	int num = 0;
	String str = "";
	
		// main case 1 start
		int numa = 0;
		String stra = "";
		// 	main case 1 end
		
			//sub case 1 start
			int subnum = 0;
			//sub case 1 end
		
		// main case 3 start  -- unused yet
		int numc = 0;
		String strc = "";
		// main case 3 end
		
		// main case 4  end -- unused yet
		int numd = 0;
		String strd = "";
		// main case 4 end
		
	// main switch end

		
	//Actual first print
	System.out.println("What do you want to do?");
	System.out.println("1 -> View");
	System.out.println("2 -> Add");
	System.out.println("3 -> Edit");
	System.out.println("4 -> Delete");
		
	JOptionPane.showMessageDialog(null, "Please choose a number from 1 to 4 - StudRecvoid");
	
	str = JOptionPane.showInputDialog("Choose an action to take:");
	num = Integer.parseInt(str);
	
	switch (num) // main switch start
		{

		case 1: // main case 1
			
			JOptionPane.showMessageDialog(null, "You have chosen to VIEW Student Records");
			stra = JOptionPane.showInputDialog("Input the entry's Student ID:");
			numa = Integer.parseInt(stra);
			index = 0;
			
			for (int j = index; j < index; j++)
				
			{
				int a = j;
				System.out.println ("Info " +strb[a + 1]);
			}						
						System.out.println ("Student ID: " +strb[index]);
						System.out.println ("Name:       " +strb1[index]);
						System.out.println ("Course:     " +strb2[index]);
						System.out.println ("Address:    " +strb3[index]);
						System.out.println ("School:     " +strb4[index]);
													
						confirm ();
			break;
			
		case 2: // main case 2
			index = index ++;
		
			JOptionPane.showMessageDialog(null, "You have chosen to ADD a new Student Record");
			
			// numb[index = JOptionPane.showInputDialog("Input Student ID:");
			
			strb[index] = JOptionPane.showInputDialog("Input Student ID:");
			numb[index] = Integer.parseInt(strb[index]);
			strb1[index] = JOptionPane.showInputDialog("Input Full Name:");			
			strb2[index] = JOptionPane.showInputDialog("Input Course:");
			strb3[index] = JOptionPane.showInputDialog("Input Address:");
			strb4[index] = JOptionPane.showInputDialog("Input School:");
			
			JOptionPane.showMessageDialog(null, "Record succesfuly added!");

				display ();

				confirm ();  // run rerun lol
				
				break;
				
		case 3: // main case 3 -- unused yet
			System.out.println();
			System.out.println("Edit has no functions yet");
			
			confirm ();  // run rerun lol
			
			break;
			
		case 4: // main case 4 -- unused 
			System.out.println();
			System.out.println("Delete has no functions yet");
			
			confirm ();  // run rerun lol
			
			break;
			
		default: // main default
			System.out.println();
			System.out.println("Please choose from 1 to 4 only");
			
			confirm ();  // run rerun lol
			
			break;
		
		} // main switch end
	}
	
	
}

Appreciate it thanks!

Thanks, but the array index should not be fixed, since there a number of records inputted varies.

What do you mean? The index is a position in the array, I said nothing about anything being fixed. Please elaborate on your understanding on that point. Your whole design is wrong for what you want to do. You could do what you want with all those arrays, a lot of loops and input, but you will get the exact same outcome in 90% less time and effort if you use my advice.

Also, If you mean the array should not be fixed, then you are wrong. An array has a fixed size.

Edited 5 Years Ago by Akill10: n/a

Hi, sorry for getting a bit confusing of what I was trying to tell, but anyways, I was able to make it on time. Here it is:

import java.io.*;
import javax.swing.*;
public class StudRecvoid {

	BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
	int index = 0;
	public String[] id = new String[20];
	public String[] name = new String[20];
	public String[] course = new String[20];
	public String[] address = new String[20];
	public String[] school = new String[20];
	String Search = " ";
	
	
	public void choices(){
		JOptionPane.showMessageDialog(null,"Please choose From 1 to 4 - working StudRecVoid");
		System.out.println("Select from the Choices");
		System.out.println("1. View");
		System.out.println("2. Add");
		System.out.println("3. Edit");
		System.out.println("4. Delete");
		System.out.println("Press any Exit");
		
			
		try{
			int choices;
			String choice = " ";
			choice = JOptionPane.showInputDialog(" Enter  Your choice");
			choices = Integer.parseInt(choice);
			if(choices == 1){
				
				JOptionPane.showMessageDialog(null, "You have chosen to SEARCH for a record");
				Search = JOptionPane.showInputDialog("Enter an INDEX"); 
				index = Integer.parseInt(Search);
								
				System.out.println(index);	
				System.out.println("View");	
				System.out.println("ID: " + id[index]);
				System.out.println("Name: " + name[index]);
				System.out.println("Course:" + course[index]);
				System.out.println("Address:" + address[index]);
				System.out.println("School:" + school[index]);
				validate();
				
				choices();
			}
			else if(choices == 2){
				JOptionPane.showMessageDialog(null, "You have chosen to ADD a record");
				index = index + 1;
				add();
			    choices();
			}
			else if(choices == 3){
			
				JOptionPane.showMessageDialog(null, "You have chosen to EDIT for a record");
				index = Integer.parseInt(Search);
				
				System.out.println(index);	
				System.out.println("View");	
				System.out.println("ID: " + id[index]);
				System.out.println("Name: " + name[index]);
				System.out.println("Course:" + course[index]);
				System.out.println("Address:" + address[index]);
				System.out.println("School:" + school[index]);
				validate();
				edit();
				choices();
			}
			else if(choices == 4){
				JOptionPane.showMessageDialog(null, "You have chosen to DELETE a record");
				 delete();
				 choices();
				}
			else {
				JOptionPane.showMessageDialog(null, "Program will now exit");
				System.exit(0);
			}
		}
		catch (Exception e){
		System.out.print(e);
		}
	}
	
	
	
	public void add(){
	try{
		
		
		id[index] =  JOptionPane.showInputDialog("Input ID: ");
		System.out.println("ID:" + id[index]);
		
		name[index] =  JOptionPane.showInputDialog("Input Name: ");
		System.out.println("Name:" + name[index]);
		
		course[index] =  JOptionPane.showInputDialog("Input Course: ");
		System.out.println("Course:" + course[index] );

		address[index] =  JOptionPane.showInputDialog("Input Address: ");
		System.out.println("Address:" + address[index]);
		
		school[index] =  JOptionPane.showInputDialog("Input School: ");
		System.out.println("School:" + school[index]);
		
		JOptionPane.showMessageDialog(null,"The Data has been added ");
	}
	catch(Exception e){
		System.out.print(e);
	}
	}
	
	public void delete(){
		try{
				
			Search = JOptionPane.showInputDialog("Search for the Data");
				index = Integer.parseInt(Search);
				
				 id[index] = null;
				 name[index] = null;
				 course[index] = null;
				 address[index] = null;
				 school[index] = null;
				index = 0;
				JOptionPane.showMessageDialog(null, "The Data has been deleted");
				choices();
		}
		catch (Exception e){
			System.out.print(e);
		}
	}
	
	public void validate(){
		 try{
			 if(id[index] == null && name[index]== null && course[index] == null 
						&& address[index] == null && school[index] == null){
					 JOptionPane.showMessageDialog(null, "No Data found");
		 }
			 else{
				 JOptionPane.showMessageDialog(null, "Data found");
			 }
		 }
		 catch( Exception e){
			 JOptionPane.showMessageDialog(null, "No Data has been Found");
		 }
	}
		
	public void edit(){
		try {
			JOptionPane.showMessageDialog(null, "Choose a field to edit. 1 = ID, 2 = Name, 3 = Course, 4 = Address, 5 = School");
			String a = " ";
			int select;
			a = JOptionPane.showInputDialog("Input data");
			select = Integer.parseInt(a);

		switch(select){
		case 1:
			id[index] = null;
			id[index] =  JOptionPane.showInputDialog("Input ID: ");
			System.out.print("ID: " + id[index]);
			break;
		case 2:
			
			name[index] = null;
			name[index] =  JOptionPane.showInputDialog("Input Name: ");
			System.out.print("Name: " + name[index]);
			break;
		case 3:
			
			 course[index] = null;
			 course[index] =  JOptionPane.showInputDialog("Input Course: ");
			 System.out.print("Course: " + course[index]);
			 break;
		case 4:
			
			address[index] = null;
			address[index] =  JOptionPane.showInputDialog("Input Address: ");
			System.out.print("Address: " + address[index]);
			break;
			
		case 5:

			school[index] = null;
			school[index] = JOptionPane.showInputDialog("Input School:");
			System.out.print("Address: " + school[index]);
			break;
		default:
			System.out.print(" Invalid Input");
			System.exit(0);
		}
		}
	catch(Exception e)
	{
		System.out.print(e);
	}
	}
}

Thanks again!

I would advise you to change the choices method, because I am sorry, but it is totally wrong. You might not see it, it might never happen but, an OutOfMemoryException is about to happen.

You keep on calling the choices inside the choices method. That gives you the behavior you want, (after you make a choice, the menu prints again), BUT:
When you recursively call the same method (choices) eventually you will run out of memory. If you want the menu to keep on printing you can try this:

public void choices() {
 while (true) {

   // put ALL of your code here and remove the calls of the choices method:
   ....
   ....
   
  if () { ......
  } else if(choices == 2){
    JOptionPane.showMessageDialog(null, "You have chosen to ADD a record");
    index = index + 1;
    add();

    // REMOVE ALL OF THOSE
    //choices();

  } else if () { ... }
  else {
    System.out.println("Invalid choice");
  }


 }
}

Edited 5 Years Ago by javaAddict: n/a

This article has been dead for over six months. Start a new discussion instead.