hello every body, how are you all
please i want some one to help me in my prgram this ,as i think, is the piece where the problem occur,
the output is being to give only the last value i enter from the keboard
where me need is to insert many values of ip and for each one i need a hop count and mask

public static void insertfirst(table tt)
{
	int intstate, mask, hop;
	long longip;
	Scanner keyboard = new Scanner(System.in);
	//BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
	String strip;
	strip="11";
	do
	{
		System.out.println("Enter IP address");
		/*try{strip=br.readLine();
		}
		catch(IOException e) {
			e.printStackTrace();
		}*/
		 strip = keyboard.nextLine();
		longip = conv.ip2long(strip);
		System.out.println("Enter Mask");
		String strmask;strmask="11";
	/*	try{strmask=br.readLine();
		}
		catch(IOException e) {
			e.printStackTrace();
		}*/
	 strmask = keyboard.nextLine();
		mask = Integer.parseInt(strmask);
		System.out.println("Enter Hop count");
		String strhop;strhop="11";
	/*	try{strhop=br.readLine();
		}
		catch(IOException e) {
			e.printStackTrace();
		}*/
		 strhop = keyboard.nextLine();
		hop = Integer.parseInt(strhop);
		tt.inserthop(longip,hop,mask);
		System.out.println("Are you want to insert a new element:"+"\t"+"1  for OK"+"\t"+"0  for stop");
		String state;state="1";
	/*	try{state=br.readLine();
		}
		catch(IOException e) {
			e.printStackTrace();
		}*/
		state = keyboard.nextLine();
		intstate=Integer.parseInt(state);
	}
	while(intstate==1);
	
}

here is what output to me

Enter IP address
10.0.0.0
Enter Mask
25
Enter Hop count
123
Are you want to insert a new element: 1 for OK 0 for stop
1
Enter IP address
10.0.0.1
Enter Mask
26
Enter Hop count
134
Are you want to insert a new element: 1 for OK 0 for stop
1
Enter IP address
10.0.0.2
Enter Mask
27
Enter Hop count
234
Are you want to insert a new element: 1 for OK 0 for stop
0
Are you want to display the table: 1 for all table 2 for specified IP 0 for continue
1
IP is 10.0.0.0 Hop count is 234 Mask is 27
Are you want to display the table: 1 for all table 2 for specified IP 0 for continue
2
Enter the IP
10.0.0.1
Hop count is: 234
Are you want to display the table: 1 for all table 2 for specified IP 0 for continue

and here is the inserthop function

void inserthop(long add, int hop, int mask)
  {
  	int flag =0;
    for (int i =0; i<M.m; i++)
      if ((this.t[i] != null) && ((this.t[i].add & 0xffffff00) == (add & 0xffffff00)))
      {insertaddhop(t[i], add,mask,24,hop);
      flag=1;
      break;
      }
      if (flag==0)
    for (int i =0; i<M.m; i++)
      if (t[i] == null)
      {
      	t[i]=new node();
      	insertaddhop(t[i], add,mask,24,hop);
      break;
      }
    
  }

thank you very much in advance for any help
best regards

Recommended Answers

All 5 Replies

the output is being to give only the last value i enter

Are you saying that tt only contains the last value you enter?

What is a table object? What does the insertaddhop method do?
Is the insertaddhop() method being called? Add a println to that method to show what it is doing.

Where is the code that would add a new object/value to the table?

thanks for reply
this is the insertaddhop()

void insertaddhop(node n, long add, int mask, int mask2, int hop)
{
if (mask > mask2)
{
	n.add=(add & 0xffffffff << (32-mask2));
	n.mask=mask2;
if((add & 1 << (31-mask2))==0)
{
	if(n.right==null)
		n.right=new node();
		insertaddhop(n.right, add, mask, mask2+1, hop);
}
else
{
	if(n.left==null)
		n.left=new node();
		insertaddhop(n.left, add, mask, mask2+1, hop);
}
}
else
if(mask==mask2)
{
	n.hop=hop;
	n.add=(add & 0xffffffff << (32-mask2));
	n.mask=mask2;
}

}

the table is class and contain nodes that save the ip and hop counts like a tree

thank you in advance
best regards

Try debugging the code by adding printlns to the places where the new value is create and added to the table.
What is in the node class? Does it have static variables?

How many elements are in the t array?
Where is the t array defined?

BTW t is a poor name for a variable. There is no way to do a Search for the locations in the code where t is used without finding lots of places the letter t is used.

this is the code please if there is any wrong tell me

class M{public static int m=10;}
class node {

  long add;
  int mask;
  int hop;
  int count;
  node right;
  node left;

node()
{
right=null;
left=null;
}
}
class table {

  node [] t = new node [M.m];

table()
{
for (int i =0;i<M.m;i++)
t[i]=null;

}

table(long add[], int mask[], int hop[])
{
int location, locationadd, locationnull;
for(int i =0;add[i] != 0;i++)
{
location=-1;
locationadd = -1;
locationnull=-1;
for(int j=0;j<M.m;j++)
if( (t[j] != null) && ( (t[i].add & 0xffffff00) == (add[i] & 0xffffff00) )) {locationadd = j; break;}
for(int j=0;j<M.m;j++)
if(t[j] == null){locationnull=j; break;}
if(locationadd != -1) location = locationadd;
else if(locationnull != -1) location = locationnull;
if(location != -1)
insertaddhop(t[location], add[i], mask[i], 24, hop[i]);
}
} //table

void insertaddhop(node n, long add, int mask, int mask2, int hop)
{
if (mask > mask2)
{
	n.add=(add & 0xffffffff << (32-mask2));
	n.mask=mask2;
if((add & 1 << (31-mask2))==0)
{
	if(n.right==null)
		n.right=new node();
		insertaddhop(n.right, add, mask, mask2+1, hop);
}
else
{
	if(n.left==null)
		n.left=new node();
		insertaddhop(n.left, add, mask, mask2+1, hop);
}
}
else
if(mask==mask2)
{
	n.hop=hop;
	n.add=(add & 0xffffffff << (32-mask2));
	n.mask=mask2;
}

}

  void inserthop(long add, int hop, int mask)
  {
  	int flag =0;
    for (int i =0; i<M.m; i++)
      if ((this.t[i] != null) && ((this.t[i].add & 0xffffff00) == (add & 0xffffff00)))
      {insertaddhop(t[i], add,mask,24,hop);
      flag=1;
      break;
      }
      if (flag==0)
    for (int i =0; i<M.m; i++)
      if (t[i] == null)
      {
      	t[i]=new node();
      	insertaddhop(t[i], add,mask,24,hop);
      break;
      }
    
  }

int gethop(long add)
{ int temp=0;
for(int i=0;i<M.m;i++)
{
	if((t[i] != null)&&((t[i].add & 0xffffff00)==(add & 0xffffff00)))
{
	temp = geth(t[i],add);
	break;
}
else temp = (-1);
}
return temp;
}

int geth(node n, long add)
{

if ((add & 1 << (31-n.mask))==0)
if (n.right != null)
return geth(n.right,add);
else
return n.hop;
else
if (n.left != null)
 return geth(n.left,add);
else
return n.hop;
}

void dispaddhop(node n)
{
	if (n!=null)
		if(n.right!=null)
		{
			dispaddhop(n.right);
		}
		if(n.left!=null)
		{
			dispaddhop(n.left);
		}
		if(n.right==null && n.left==null)
		{
			System.out.println("IP is "+conv.long2ip(n.add)+"\t"+" Hop count is "+n.hop+"\t"+"Mask is "+n.mask);
		}
}


void access(long add,int mask)
{
	int cc;
	for(int i=0;i<M.m;i++)
		if(((this.t[i].add & 0xffffff00)==(add& 0xffffff00))&&(t[i] != null))
		acc(t[i],add,mask);
}
void acc(node n, long add, int mask)
{
	if(mask > n.mask)
	{
		if((add & 1 << (31-n.mask))==0)
		{
			if(n.right!=null)
				acc(n.right,add,mask);
		}
		else
			if(n.left!=null)
				acc(n.left,add,mask);
	}
	if(mask==n.mask)
		n.count=n.count+1;
}

}
public class project {
    
 public static void insertfirst(table tt)
 {
 	int intstate, mask, hop;
 	long longip;
 	Scanner keyboard = new Scanner(System.in);
 	//BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
 	String strip;
 	strip="11";
 	do
 	{
 		System.out.println("Enter IP address");
 		/*try{strip=br.readLine();
 		}
 		catch(IOException e) {
 			e.printStackTrace();
 		}*/
 		 strip = keyboard.nextLine();
 		longip = conv.ip2long(strip);
 		System.out.println("Enter Mask");
 		String strmask;strmask="11";
 	/*	try{strmask=br.readLine();
 		}
 		catch(IOException e) {
 			e.printStackTrace();
 		}*/
 	 strmask = keyboard.nextLine();
 		mask = Integer.parseInt(strmask);
 		System.out.println("Enter Hop count");
 		String strhop;strhop="11";
 	/*	try{strhop=br.readLine();
 		}
 		catch(IOException e) {
 			e.printStackTrace();
 		}*/
 		 strhop = keyboard.nextLine();
 		hop = Integer.parseInt(strhop);
 		tt.inserthop(longip,hop,mask);
 		System.out.println("Are you want to insert a new element:"+"\t"+"1  for OK"+"\t"+"0  for stop");
 		String state;state="1";
 	/*	try{state=br.readLine();
 		}
 		catch(IOException e) {
 			e.printStackTrace();
 		}*/
 		state = keyboard.nextLine();
 		intstate=Integer.parseInt(state);
 	}
 	while(intstate==1);
 	
 }

thank you for any help
best regards

How would anyone know if there is anything wrong?

I don't see the printlns statements you need to add for debugging.

I see that you have not changed the variable name t and n to be names with meanings and uniqueness that can be searched for. If I want to see where t is used, its hopeless doing a find for t. Every word in the program has a t in it.

Also the posted does not have a main() method. How can it be tested?

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.