954,549 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

the results is not like i want

hello, please i make a code that i want it to work as follows:
the user input 3 value and after an inspection(that i am not put it now, i want to try it now so i make the condition, if k%2==0)the input is be either spoof or valed,at the begining the state is alert, then i compute the (th)it is a threshold value, so if it is spoof and the th is less than T1 the o/p is accept, and still that(when the user enter other 3 values) untill the th be larger than T1 and then it is switch to the action state and the o/p is (if it is spoof the o/p is drop and if it is not spoof the o/p is forword)and still that (when the user enter other 3 values) until the th is less than T2


so i do my best and write that code but it's o/p is not like that i want, please can u do any help for me,
thank u very much in advance

import java.io.*;
import java.*;
import java.util.*;
import java.lang.*;

public class IP2HC_Inspect{
//instanceof ttt= new table;
public static void main(String[] args)  {
	int totalpkt=0,k=0,th=0,spoofpkt=0;
	double T1=0.3,T2=0.7;
	boolean done=true,spoof=false,sw1=true,sw2=false;
do{
System.out.println(" \n   Enter your packet that contain a valid IP Add with the field of the TTL and the connection type \n ");
++totalpkt;
Scanner keyboard = new Scanner(System.in);  
System.out.println("ip is");
String IP = keyboard.nextLine();
System.out.println("ttl is");
String TTL = keyboard.nextLine();
System.out.println("conn is");
String conntype = keyboard.nextLine();
if (conntype.equalsIgnoreCase("TCP"))
++k;
//int x = Integer.parseInt( scanner.nextLine() );DIC
int T=Integer.parseInt(TTL);
//long ss = IpConvert.ipToLong(IP);
//int s=Integer.parseInt(IP);
int T0=0,Hc;
if(T<=32)
T0=32;
else if(T>32 && T<=64)
T0=64;
else if(T>64 && T<=128)
T0=128;
else if(T>128 && T<=255)
T0=255;
System.out.println("T0 is  "+T0);

Hc=T0-T;
System.out.println("Hc is  "+Hc);
long s=IpConvert.ipToLong(IP);
System.out.println("ip in long is  "+s);
System.out.println("totalpkt is  "+totalpkt);
System.out.println("k is  "+k);
if(k%2==0)
{spoof=true;
++spoofpkt;
System.out.println("spoof is   "+spoof+"  spoofpkt is  "+spoofpkt);
}else
System.out.println("ligtmate");
th=spoofpkt/totalpkt;
System.out.println("th is   "+th);
if (sw1)
{
	if(th<T1)
	{System.out.println("alert state");
		System.out.println("pkt accepted");}
		if(spoof && th>T1)
		{sw2=true;
		sw1=false;
		}


if (sw2){
System.out.println("action state");
if (spoof && th>T1)
System.out.println("pkt is droped");
else if (!spoof && th>T1)
System.out.println("pkt in action is forword");
else if(!spoof && th<T2)
{System.out.println("back to alert");
sw1=true;sw2=false;
}
}
//if (k==10)
//break;
}
}while(done);


best regards

s.w.a
Junior Poster in Training
63 posts since Mar 2011
Reputation Points: 7
Solved Threads: 0
 

th=spoofpkt/totalpkt;

because th is an int, and assuming spoofpkt < totalpkt, th will always be zero (integer division is always rounded down to an integer value). You could make th a float to hold fractional values.

JamesCherrill
Posting Genius
Moderator
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

th=spoofpkt/totalpkt;

because th is an int, and assuming spoofpkt < totalpkt, th will always be zero (integer division is always rounded down to an integer value). You could make th a float to hold fractional values.


thanks for that note
although the th is become 1 when i enter some thing else than tcp the o/p is stick in the alert state
if u can try it is't o/p is either stick in alert or start in drop the stick in the alert how can i do that switching write?

thanks in advance
best regards

s.w.a
Junior Poster in Training
63 posts since Mar 2011
Reputation Points: 7
Solved Threads: 0
 

How about: sw1=true;sw2=false;
Those values only get changed at lines 59/60 if th>T1, so maybe that's never true (see previous post).
ps It's really hard to read complex if logic when the indentation is wrong. Get yourself a code editor that indents for you.

JamesCherrill
Posting Genius
Moderator
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

is that code work for u?
when i use the while statement instade of if statment is it more correct?
thanks

s.w.a
Junior Poster in Training
63 posts since Mar 2011
Reputation Points: 7
Solved Threads: 0
 

th=spoofpkt/totalpkt;

because th is an int, and assuming spoofpkt < totalpkt, th will always be zero (integer division is always rounded down to an integer value). You could make th a float to hold fractional values.


sorry now the th is still 0.0 even when i define it as float, so please why that?
thanks in advance

s.w.a
Junior Poster in Training
63 posts since Mar 2011
Reputation Points: 7
Solved Threads: 0
 

Add some debug code to print out the values of all the variables used in the expression and post them so we can see what the values going into the expression are.
Copy and paste the console output here.

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 

hello, i cast the one of the tw int as follows

th=(float)spoofpkt/totalpkt;

and the th is now true


now the problem is the o/p is like this Enter your packet that contain a valid IP Add with the field of the TTL and the connection type

ip is
10.0.0.2
ttl is
23
conn is
tcp
T0 is 32
Hc is 9
ip in long is 167772162
totalpkt is 1
k is 1
ligtmate
spoof is false spoofpkt is 0
th is 0.0
alert state
pkt accepted

Enter your packet that contain a valid IP Add with the field of the TTL and the connection type

ip is
10.0.0.2
ttl is
54
conn is
tcp
T0 is 64
Hc is 10
ip in long is 167772162
totalpkt is 2
k is 2
spoof is true spoofpkt is 1
th is 0.5
action state
pkt is droped

Enter your packet that contain a valid IP Add with the field of the TTL and the connection type

ip is
10.0.0.2
ttl is
43
conn is
tcp
T0 is 64
Hc is 21
ip in long is 167772162
totalpkt is 3
k is 3
ligtmate
spoof is true spoofpkt is 1
th is 0.33333334

Enter your packet that contain a valid IP Add with the field of the TTL and the connection type

ip is
10.0.0.2
ttl is
43
conn is
tcp
T0 is 64
Hc is 21
ip in long is 167772162
totalpkt is 4
k is 4
spoof is true spoofpkt is 2
th is 0.5

it doesn't say any thing in the third time

thank u in advance for any help

s.w.a
Junior Poster in Training
63 posts since Mar 2011
Reputation Points: 7
Solved Threads: 0
 

Please explain where the third line is and why it is incorrect?
Edit the output and put some comments in it showing where the error is and what the output should be.

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 

thank u for reply
i meam that the o/p in

Enter your packet that contain a valid IP Add with the field of the TTL and the connection type

ip is
10.0.0.2
ttl is
43
conn is
tcp
T0 is 64
Hc is 21
ip in long is 167772162
totalpkt is 4
k is 4
spoof is true spoofpkt is 2
th is 0.5 //here i want the o/p say where are i am now (in which state and what is happen)

that is what i have problem with
is that what u want please, if any thing not clear please tell me
thanks in advance

s.w.a
Junior Poster in Training
63 posts since Mar 2011
Reputation Points: 7
Solved Threads: 0
 
//here i want the o/p say where are i am now (in which state and what is happen)


What would correct output look like?
Where in your code is that output supposed to be generated?
What variables control the output? What values do they have when the code executes incorrectly?

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 
What would correct output look like? Where in your code is that output supposed to be generated? What variables control the output? What values do they have when the code executes incorrectly?


ok, the th value is control the swithing
when it is less than T1 it must switch to second state, that is the code i consern with:

if (sw2){
System.out.println("action state");
if (spoof && th>=T1)
System.out.println("pkt is droped");
else if (!spoof && th>=T1)
System.out.println("pkt in action is forword");
else if(!spoof && th<T2)
{System.out.println("back to alert");
sw1=true;sw2=false;
}


the o/p must be in the action state and the pkt is droped


best regards
thank u in advance

s.w.a
Junior Poster in Training
63 posts since Mar 2011
Reputation Points: 7
Solved Threads: 0
 

What variables control the output? What values do they have when the code executes incorrectly?

Use printlns to show the values of the variables that control the output.

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 

What about these two cases?
(spoof && th=T1 || th

JamesCherrill
Posting Genius
Moderator
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

What about these two cases? (spoof && th=T1 || th

i adjust the T1 to be 0.5 and T2 to be 0.3 so these values can used to switch from state to another is that what u want please?

regards

s.w.a
Junior Poster in Training
63 posts since Mar 2011
Reputation Points: 7
Solved Threads: 0
 

I don't understand what your application is supposed to do or what values different variables should have. That is up to you.

I'm trying to point out ways for YOU to debug your code so it does what you want it to do.
One way to debug code is by using println statements to show the values of variables.
You know what the values of the variables should be at different points in the program.
If you print them out, you will see if the code is computing them correctly or if there is a problem.

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 

I personally don't "want" anything. I was just pointing out that there are two cases where your if tests don't print anything out. Since you were looking for an explanation of why nothing was printed for some data, I thought this could be relevant.

JamesCherrill
Posting Genius
Moderator
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 
I personally don't "want" anything. I was just pointing out that there are two cases where your if tests don't print anything out. Since you were looking for an explanation of why nothing was printed for some data, I thought this could be relevant.


sorry i misunderstand u
what do u mean by the 2 latest posts?
english is not my mother language

do u mean that i must test those or remove it?

regards

s.w.a
Junior Poster in Training
63 posts since Mar 2011
Reputation Points: 7
Solved Threads: 0
 

I mean
if (spoof && th=T1 || thMaybe your 3rd line, where nothing was printed, is because one or the other of these two things happened. You can check that by looking at spoof, th, T1 and T2 for that line.
If that is the problem, then you could add these cases to your set of if tests, or you could have a final else clause that prints "none of the above", so you know what's happening.

If I'm using English words you don't know, please ask. I'm English but I live in France, so I know how hard it can be to work in a foreign language.

JamesCherrill
Posting Genius
Moderator
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

thank u very much for ur kind and ur reply
so now i understand what u mean and i will try it and reply if any


best regards

s.w.a
Junior Poster in Training
63 posts since Mar 2011
Reputation Points: 7
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You