index no.student room
2	3	D/BARU
2	9	D/BARU
2	1	D/BARU
4	44	A005
4	1	A002
4	1	A002
4	19	A002
4	19	A002
4	35	A001
4	29	BSK 2/3
4	25	A106/107
4	10	A005
4	0	B202
4	10	B208/209
4	20	D/BARU
4	54	A 101
4	0	A 101
4	50	A 118
4	30	A 102
4	19	A 202

i got a problem to compare the data of index and room before adding value of no.student

this is part of my code:


int countFor = 0;
int bilStudent[] = new int[10000];
int index[] = new int[10000];
int student[] = new int[10000];
String room[] = new String[10000];
int count = 0; // this count is number of row or total data. currently count = 2637
for(int m=0; m<count; m++)
		{
			if(index[m] == index[m+1])
			{
				if(room[m].equalsIgnoreCase(room[m+1]))
				{
					countFor++;
				}
				else
				{
					for(int n=0; n<=countFor; n++)
					{
						if(countFor == 0)
						{
							bil
						}
						else
						{
							bilStudent[n]
						}
					}
				}
			}
		}

the problem is to compare index[m] with index[m+1].
if data for index[m] and index[m+1] is same then
it will check room whether same or not for room[m] and room[m+1]
if same then it will add the data from same room and same index
eg : 13 for first 3 index for D/BARU
and if room is not equal even index is same then it will calculate only for same room on same index..
im stuck here and still confused on how to add no.student and add it to array billStudent[].

Recommended Answers

All 10 Replies

Could you explain the logic of the program again using words instead of array references?
You're to compare two adjacent index entries in a table for equality.
If they are the same
then check if the same values for rooms are equal
then add ??? something here
else if the room values are different ????
And I'm lost.

yeah to calculate the total number of student in the same room for same index..
example :
13 for D/BARU at index 2
44 for A005 at index 4
40 for A002 at index 4
35 for A001 at index 4

i want that data to be something like this..

Could you explain the logic of the program. You've posted what its supposed to do.
Now can you explain how the program is going to do it?
You read in data from a file and what do you do with it?

okay..
the program start with text based file that store index, no.student and room..
i will take this data and store in array index[], student[] and room[].
i will used the data that been save in that array to determine the total number of student with same index and same room..
let say monday as 2 in index and 4 as wednesday..
so i wanna calculate the number of student for monday in the same room. same too goes for wednesday and forward..
and after that, the data will be stored and save in another text file for later used..

sorry for late reply..

Ok.
Can you explain what problems you are having?

How can we help you to write your code?

I've got a question about the logic, before I address the code. For your final outcome, do you want to have the total of "no. students" for each "room"/"index" pair? If not that, what do you want to do with these totals when you get them?

Concretely, you say you want A005 for index 4 to be 44. However, you also have a "no. students" value of 10 for A005, index 4 (see line 13 of your data). I would imagine you'd want to add these, even though they're not adjacent rows. If you don't do that, you'll probably end up overwriting the 44 with the 10 (unless you do some subindexing).

I'm guessing that the point of this is to total the number of students in a room at any given time (index number is a class period, or a schedul block of some sort?), when you might have different class sections. (ie, CS110 might be listed by lab sections, but all of the lab sections are in room A005 for the lecture). Am I close on this?

yeah this what i want jon.kiparsky..
this -->"For your final outcome, do you want to have the total of "no. students" for each "room"/"index" pair?"

ohh for your 2nd paragraph, like u say there are same index and room but adjacent row..
this also become a problem cause i dont know how to compare it from adjacent row.

Okay. Let's start by getting you room/index pairs, and we'll improve it from there.

Well, if you could turn each room into an array[MAX_INDEX] of ints, you could use "index" as an index into the array, and simplay add the "no. students" column to the current value of room[index]. That'll take care of your adjacency problem - it won't matter if line 5 and 13 are adjacent or not - it'll add 44 to zero (in line 5) and then add 10 when you get to line 13.

Do you understand me so far?

don't try to code this yet - there's still a problem. The problem is going to be that you need to access the right variable for "room". How do you think you can do that?

My way to fufill the staticstics task is implemented via different arrays. The final result is represented via a two dimension array. The column fields are indicated by different rooms while the rows name by different indexs. The final result is shown via the 2-D array:
indD/BARU|A005|A002|A001|BSK 2/3|A106/107|B202|B208/209|A 101|A 118|A 102|A 202|
2 13 0 0 0 0 0 0 0 0 0 0 0
4 20 54 40 35 29 25 0 10 54 50 30 19


The data represented in this format may be easily converted into the format indicated by shahreza
13 for D/BARU at index 2
54 for A005 at index 4
40 for A002 at index 4
35 for A001 at index 4
….

In order to define the 2D array we may have to create two arrays showing different rooms (Elements in String type) and index (Elements in int). In these array, the elements are requested to show up once only, that is, no duplication (repeated element) is allowed. Thus, I have defined two methods in terms of polymorphism.
public static String[] singleCheck(String [] a){..} and
public static int[] singleCheck(int [] a){…}
to complete the task: to create such two arrays of non-duplicated elements: s and ind.
When executing these methods, another two methods are called in the way of polymorphism.
public static int search(String st[], String s, int n){.}, and
static int search(int st[], int s, int n){…}
The origina data are stored in 3 different arrays:
static String room[]={"D/BARU","D/BARU","D/BARU","A005","A002","A002","A002",
"A002","A001","BSK 2/3","A106/107","A005","B202","B208/209","D/BARU","A 101","A 101","A 118","A 102","A 202"};
static int index[]={2,2,2,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4};
static int data[] ={3,9,1,44,1,1,19,19,35,29,25,10,0,10,20,54,0,50,30,19};
The program is written as follows.

import java.io.*;
public class Statistics1 {
static String room[]={"D/BARU","D/BARU","D/BARU","A005","A002","A002","A002","A002","A001","BSK 2/3",
"A106/107","A005","B202","B208/209","D/BARU","A 101","A 101","A 118","A 102","A 202"};
static int index[]={2,2,2,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4};
static int data[] ={3,9,1,44,1,1,19,19,35,29,25,10,0,10,20,54,0,50,30,19};

static int search(String st[], String s, int n){
	for (int i=0;  i<n ; i++)
	if (st[i].compareTo(s)==0)
	return i;
	return -1;
}

static int search(int st[], int s, int n){
	for (int i=0; i<n; i++)
	if (st[i]==s)
	return i;
	return -1;
}	
    public static String[] singleCheck(String [] a) {
    	String RoomName[] = new String[a.length];
    	int counter =0;
    	RoomName[0]=a[0];
    	for (int i=1; i< room.length; i++)
    	if ((RoomName[counter].compareTo(a[i])!=0) && (search(RoomName, a[i], counter+1)==-1))
    	RoomName[++counter]=a[i];
    	String result[]= new String[counter+1];
    	for (int i=0;i<counter+1;i++)
    	result[i]=RoomName[i];

    	return result;
 	}
 	
 	public static int[] singleCheck(int [] a){
    	int RoomName[] = new int[a.length];
    	int counter =0;
    	RoomName[0]=a[0];
    	for (int i=1; i< room.length; i++)
    	if ((RoomName[counter] != a[i]) && (search(RoomName, a[i], counter+1)==-1))
    	RoomName[++counter]=a[i];
    	int result[]= new int[counter+1];
    	for (int i=0;i<counter+1;i++)
    	result[i]=RoomName[i];

    	return result;
 	}
 	
 	public static void showElements(Object [] a, String s){
 		System.out.println(s);
 		for (int i=0;i<a.length;i++){
 			if (i== a.length-1)
 			System.out.print(a[i]);
 			else
 			System.out.print(a[i]+ "|");
 			if ((i+1)%15==0)
 			System.out.println();
 		}
 		System.out.println();
 	}
 	
 		
 	public static void showElements(int [] a, String s){
 		System.out.println(s);
 		for (int i=0;i<a.length;i++){
 			if (i== a.length-1)
 			System.out.print(a[i]);
 			else
 			System.out.print(a[i]+ "|");
 			
 			if ((i+1)%15==0)
 			System.out.println();
 		}
 		System.out.println();
 	}
 	

 	public static void main(String[] args){
 		String s[] = singleCheck(room);
 		showElements(s,"The different rooms are written as follows.");
 		int ind []= singleCheck(index);
 		showElements(ind,"The different indexes are written as follows.");
 
 		int ResultData[][] = new int[ind.length][s.length];
 		for (int i=0; i<data.length;i++){
 			int a= search(ind, index[i],ind.length);
 			int b= search(s,room[i], s.length);
 			if ((a != -1)&&(b !=-1))
 			ResultData[a][b] += data[i];
 		}
 		
 		System.out.print("ind");
 		for (int i=0; i<s.length;i++)
 		System.out.printf("%s|",s[i]);
 		System.out.println();
 		for (int i=0; i< ind.length;i++){
 		System.out.printf("%3d",(i+1)*2);
 		for (int j=0; j< s.length; j++)
 		System.out.printf("%6d",ResultData[i][j]);
 		System.out.println();
 		}
 		
 		for (int i=0; i<ind.length;i++)
 		for (int j=0; j<s.length;j++)
 		if (ResultData[i][j] !=0)
 		System.out.printf("%d  for Room %8s at index %d\n",ResultData[i][j], s[j],ind[i]);
 		
 		
 	}
}

The output is shown as follows:
The different rooms are written as follows.
D/BARU|A005|A002|A001|BSK 2/3|A106/107|B202|B208/209|A 101|A 118|A 102|A 202
The different indexes are written as follows.
2|4
indD/BARU|A005|A002|A001|BSK 2/3|A106/107|B202|B208/209|A 101|A 118|A 102|A 202|
2 13 0 0 0 0 0 0 0 0 0 0 0
4 20 54 40 35 29 25 0 10 54 50 30 19
13 for Room D/BARU at index 2
20 for Room D/BARU at index 4
54 for Room A005 at index 4
40 for Room A002 at index 4
35 for Room A001 at index 4
29 for Room BSK 2/3 at index 4
25 for Room A106/107 at index 4
10 for Room B208/209 at index 4
54 for Room A 101 at index 4
50 for Room A 118 at index 4
30 for Room A 102 at index 4
19 for Room A 202 at index 4

yeah thanks tong1 for the code..
i try it and it work fine for me^^..
if my work is complete, then ill add your name as my reference in my final year project..

other way :
jon.kiparsky, i understand what u wanna say for overall but im still blur about this statement --> (each room into an array[MAX_INDEX] of ints, you could use "index" as an index into the array,)

but i can handle adjacent row for later..
i hope i can solve total of "no. students" for each "room"/"index" pair
any way to solve this without thinking of adjacent row at the moment?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.