8 Replies - 889 Views - Last Post: 01 March 2009 - 02:21 PM Rate Topic: -----

#1 paulmor  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 25-February 09

pass values from array to another array

Posted 27 February 2009 - 02:19 PM

I'm trying to pass the found value from the module array to the student array

public void assStuToMod()
	{
	scan = new Scanner(System.in);
	String searchMod1;
	String searchMod2;
	String searchMod3;
	boolean findMod1 = false;
   // boolean findMod2 = false;
   // boolean findMod3 = false;
	
	int loopCounter = 0;
	int mod1Count = 0;
	
	
	int j = 0;
	
	System.out.println("Please Enter student name:");
	searchName = scan.nextLine();
	
	while ((!found)&&(loopCounter < noOfStuds)) 
	{
		found = student[loopCounter].checkName(searchName);
		
		if(!found)
		{
			loopCounter++;
			System.out.println("Student "+searchName+ " not found");
		}
		else if (found == true) 
		{
		
		System.out.println("Select first module ");
		searchMod1 = scan.nextLine();
		
		while ((!findMod1)&&(mod1Count < noOfMods)) 
	{
		findMod1 = module[mod1Count].checkMod1(searchMod1);
		
		if(!findMod1)
		{
			mod1Count++;
			System.out.println("Module "+searchMod1+ " not found");
		}
		else
		{
		module[mod1Count] == (student[loopCounter].getMod1());
		}
		}



Is This A Good Question/Topic? 0
  • +

Replies To: pass values from array to another array

#2 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3124
  • View blog
  • Posts: 19,168
  • Joined: 14-September 07

Re: pass values from array to another array

Posted 27 February 2009 - 02:50 PM

//something like this:
for(int i = 0; i < arrSize; i++)
{
	 student[i] = module[i];
}
//do stuff with student


Was This Post Helpful? 0
  • +
  • -

#3 paulmor  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 25-February 09

Re: pass values from array to another array

Posted 27 February 2009 - 04:04 PM

have found the module just need to pass it to student array variable mod1
Was This Post Helpful? 0
  • +
  • -

#4 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3124
  • View blog
  • Posts: 19,168
  • Joined: 14-September 07

Re: pass values from array to another array

Posted 27 February 2009 - 04:22 PM

I have no idea what you just said. So mod1 (a variable that doesn't exist in your code) needs to be assigned in the student array from the module array?

What is this:

module[mod1Count] == (student[loopCounter].getMod1());



You're using the equality operator, but don't do anything with it. If you want to assign something use '='.
Was This Post Helpful? 0
  • +
  • -

#5 paulmor  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 25-February 09

Re: pass values from array to another array

Posted 27 February 2009 - 04:52 PM

public class Exercise4 {
	// Creates arrays of 5 elements for both student and module
	Student[] student = new Student[5];
	Module[] module = new Module[5];
	
	// Initialises counters for both students and modules
	int noOfStuds = 0;
	int noOfMods = 0;
	
	private Scanner scan;
	
	
	public static void main(String[] args) throws IOException
	{
		//Creates a new instance of the Exercise4 class
		Exercise4 studModProg = new Exercise4();
		//Call the Menu method
		studModProg.menu();
	}   
		
	
	public Exercise4()
	{
		//Calls the scanner class
		scan = new Scanner(System.in);	
	}	
	
	public void menu()
	{
		int ans = 0;
		
		System.out.println("MAIN MENU");
		System.out.println("(1) - Add Student");
		System.out.println("(2) - Delete Student");
		System.out.println("(3) - Add Module");
		System.out.println("(4) - Assign Student to Module");
		System.out.println("(5) - Display Student Details");
		System.out.println("(6) - Exit");
		System.out.println("Please Choose an option between 1-6");
		ans = scan.nextInt();
		
		switch(ans)
		{
			case 1:
				addStudent();
				break;
			case 2:
				deleteStudent();
				break;
			case 3:
				addModule();
				break;
			case 4:
			assStuToMod();
			//displaydetails();
				break;
			//case 5:
			//	displayStuDetails();
			//break;
			case 6:
				break;
			default: System.out.println("Incorrect choice");
			menu();
			break;
			 			
		}
	}
	
	
	
	//=====================================================================================
	public void addStudent()
	{
	if (noOfStuds < 5)
		{
		
		scan = new Scanner(System.in);
		
		student[noOfStuds] = new Student();
		System.out.println("Please enter Students name: ");
		student[noOfStuds].setName(scan.nextLine());
		System.out.println("Please enter enrollment date DD/MM/YY: ");
		student[noOfStuds].setDate(scan.nextLine());
		
		noOfStuds++;
		 
		}
	
		else
			
			{
			System.out.println("No More Students can be entered");	
			}
		//displaydetails();
		menu();
		}
	//=====================================================================================
	public void deleteStudent()
	{
	scan = new Scanner(System.in);
	String searchName;
	boolean found = false;
	int loopCounter = 0;
	int j = 0;
	
	System.out.println("Please Enter the student you wish to delete");
	searchName = scan.nextLine();
	
	
	while ((!found)&&(loopCounter < noOfStuds)) 
	{
		found = student[loopCounter].checkName(searchName);
		
		if(!found)
		{
			loopCounter++;
			System.out.println("Student "+searchName+ " not found");
		}
		else 
		{
		System.out.println("Found student at position "+loopCounter);
		
		student[loopCounter] = null;
		
		for(j = loopCounter; j < noOfStuds; j++)
		   student[j] = student[j+1];	 // replaced by following one  
	   
		System.out.println("Student deleted "+loopCounter);
		break;		// done exit for loo
		
		
		
		}
		
	}
	//displaydetails();
	menu();
	
	}
	
	
	
	//=====================================================================================  
	public void addModule()
	{
	if (noOfMods < 5)
		{
		
		scan = new Scanner(System.in);
		
		module[noOfMods] = new Module();
		System.out.println("Please enter Module name: ");
		module[noOfMods].setmodName(scan.nextLine());
		System.out.println("Please enter Semester: ");
		module[noOfMods].setmodSem(scan.nextInt());
		
		noOfMods++;
		
		}
	
		else
			
			{
				System.out.println("No More Modules can be entered");	
			}
		menu();
		}
	 //=====================================================================================
 	public void assStuToMod()
	{
	scan = new Scanner(System.in);
	String searchName;
	String searchMod1;
	String searchMod2;
	String searchMod3;
	boolean found = false;
	boolean findMod1 = false;
   // boolean findMod2 = false;
   // boolean findMod3 = false;
	
	int loopCounter = 0;
	int mod1Count = 0;
	
	
	int j = 0;
	
	System.out.println("Please Enter student name:");
	searchName = scan.nextLine();
	
	while ((!found)&&(loopCounter < noOfStuds)) 
	{
		found = student[loopCounter].checkName(searchName);
		
		if(!found)
		{
			loopCounter++;
			System.out.println("Student "+searchName+ " not found");
		}
		else if (found == true) 
		{
		
		System.out.println("Select first module ");
		searchMod1 = scan.nextLine();
		
		while ((!findMod1)&&(mod1Count < noOfMods)) 
	{
		findMod1 = module[mod1Count].checkMod1(searchMod1);
		
		if(!findMod1)
		{
			mod1Count++;
			System.out.println("Module "+searchMod1+ " not found");
		}
		else
		{
		System.out.println("Module "+searchMod1+ " is found");
		
		}
		}
		
	   
	}
		}
	}


public class Student 
	{
	private	String name;
	private	String date;
	private String mod1;
	private String mod2;
	private String mod3;
	

	
		public Student()
		{
		
		}
	
		public Student(String studentName,String enrolDate,String module1,String module2,String module3 )
		{
			name = studentName;
			date = enrolDate;
			mod1 = module1;
			mod2 = module2;
			mod3 = module3;
		}
		
		public void setName(String theName)
		{
			name = theName;
		}
	   	
	   	public String getName()
		{
			return name;
		}
		
		public void setDate(String date)
		{
			this.date = date;
		}
		public String getDate()
		{
			return date;
		}
	
		public void setMod1(String mod1)
		{
			
			this.mod1 = mod1;
		}
		public String getMod1()
		{
			return mod1;
		}
		
		public void setMod2(String mod2)
		{
			this.mod2 = mod2;
		}
		public String getMod2()
		{
			return mod2;
		}
		
		public void setMod3(String mod3)
		{
			this.mod3 = mod3;
		}
		public String getMod3()
		{
			return mod3;
		}
		
//=====================================================================================
		public boolean checkName(String searchName)
		{
			boolean checkVal = false;
			
			System.out.println("Name is "+name+" SearchName is "+ searchName);
			
				if(name.equals(searchName))
				{
				System.out.println("searchName does equal Name");
				checkVal = true;
				}
			
			return checkVal;
		}
	
	}



[code]
import java.util.*;
import java.*;

public class Module
{
private String modName;
private int modSem;

public Module()
{

}

public Module(String mName,int semester)
{
modName = mName;
modSem = semester;
}

public String getmodName()
{
return modName;
}

public void setmodName(String modName)
{
this.modName = modName;
}

public int getmodSem()
{
return modSem;
}

public void setmodSem(int modSem)
{
this.modSem = modSem;
}

public boolean checkMod1(String searchMod1)
{
boolean checkVal = false;

System.out.println("Module is "+modName+" FindMod1Name is "+ searchMod1) ;

if (modName.equals(searchMod1))
{
System.out.println("searchMod1Name does equal Name") ;
checkVal = true;
}

return checkVal;
}
}

apologies for my ignorance KYA am new to java! i need to put the module i just found into the student class or have a link to it to print out a report e.g. student takes this module (three modules have to be assigned) i've created the variable for them in the student class but can't assign the module to the student array. hope this makes it a little clearer. thanks for the assistance thus far.
Was This Post Helpful? 0
  • +
  • -

#6 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: pass values from array to another array

Posted 27 February 2009 - 04:55 PM

you mean that you want to add an instance variable of type module in your Student class?
Was This Post Helpful? 0
  • +
  • -

#7 paulmor  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 25-February 09

Re: pass values from array to another array

Posted 27 February 2009 - 05:08 PM

[quote name='mostyfriedman' date='27 Feb, 2009 - 03:55 PM' post='556627']
you mean that you want to add an instance variable of type module in your Student class?
[/quot
I need to pass the contents of the module array that i have searched for to the student array in order to print a report
Was This Post Helpful? 0
  • +
  • -

#8 paulmor  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 25-February 09

Re: pass values from array to another array

Posted 28 February 2009 - 02:49 PM

i need to pass the reference from the module array to mod1 variable in the student class so when i print the report the the student name, date, and module taken displays
Was This Post Helpful? 0
  • +
  • -

#9 paulmor  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 25-February 09

Re: pass values from array to another array

Posted 01 March 2009 - 02:21 PM

i'm getting Exception in thread "main" java.lang.NullPointerException
at Exercise4.assStuToMod(Exercise4.java:244) line is numbered in listing below.


	public void assStuToMod()
	{
	scan = new Scanner(System.in);
	
	String searchName;
	String searchMod1;
	String searchMod2;
	String searchMod3;
	
	boolean found = false;
	boolean findMod1 = false;
	boolean findMod2 = false;
	boolean findMod3 = false;
	
	int loopCounter = 0;
	int mod1Count = 0;
	int mod2Count = 0;
	int mod3Count = 0;
	
	int j = 0;
	
	System.out.println("Please Enter student name:");
	searchName = scan.nextLine();
	
	while ((!found)&&(loopCounter < noOfStuds)) 
	{
		found = student[loopCounter].checkName(searchName);
		
		if(!found)
		{
			loopCounter++;
			System.out.println("Student "+searchName+ " not found");
		}
		else if (found == true) 
		{
		
		System.out.println("Select first module ");
		searchMod1 = scan.nextLine();
		
		while ((!findMod1)&&(mod1Count < noOfMods)) 
	{
		findMod1 = module[mod1Count].checkMod1(searchMod1);
		
		if(!findMod1)
		{
			mod1Count++;
			System.out.println("Module "+searchMod1+ " not found");
		}
		else
		{
		System.out.println("Module "+searchMod1+ " is found");
 244   	student[noOfStuds].setMod1(module[mod1Count]);
		}
	}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1