Results 1 to 10 of 10
  1. #1
    manowar689 is offline Member
    Join Date
    Jun 2010
    Posts
    16
    Rep Power
    0

    Default help with not equal to operator !=

    ok so im having a problem im tryin to write a java program that can work out subnets based on user input but cant get this loop to work like assuming the answer to Q2=128 it will do the if part and run readbit but then the loop starts again instead of continuing :eek: lol any help would be appricated

    Java Code:
    //--------------------------------------------subnet class C address----------------------------------------
    class subnet{
    
    	public static void main (String[] args){
    
    	int Q1;
    	int Q2;
    
    //----------------------------------------------CLASS C---------------------------------------------
    System.out.print ("\nThis is a program that will workout class c addresses for subnetting\n");
    System.out.print ("\nplease input the method you wish to workout ip addresses\n");
    Q1 = kdr.valgetint("\n1:hosts per subnet\n2:bits borrowed\n3:total subnets\nplease input answer : ");
    
    //--------------------------------------------Q1----------------------------------------------------
    if (Q1==1)
    {
    	do{
    // ensures that a valid int is got before trying to check it 
    		Q2 = kdr.valgetint("\nplease input number of hosts per subnet: ");
    			if(Q2==128 || Q2==64 || Q2==32 || Q2==16 || Q2==8 || Q2==4)
    			{
    			readbit();
    			}
    			else{
    			System.out.print ("\nThis is the wrong value it needs to be 128,64,32,16,8,4,2,1");
    			};
    	}while(Q2!=128 || Q2!=64 || Q2!=32 || Q2!=16 || Q2!=8 || Q2!=4);
    }
    //--------------------------------------------------------------------------------------------------
    //--------------------------------------------Q2----------------------------------------------------
    if (Q1==2)
    {
    do{
    Q2 = kdr.valgetint("\nplease input number of bits borrowed: ");
    if(Q2<=6)
    {
    readbit();
    }
    else{
    System.out.print ("\nThis is the wrong value it needs to be 1,2,3,4,5,6");
    };
    
    }while(Q2<=6);
    
    if(Q2==1)
    {
    }
    if(Q2==2)
    {
    }
    if(Q2==3)
    {
    }
    if(Q2==4)
    {
    }
    if(Q2==5)
    {
    }
    if(Q2==6)
    {
    }
    }
    //--------------------------------------------------------------------------------------------------
    
    	}
    	static void readbit() {
    
    int count;
    count = 0;
    double bit[] ;
        bit = new double [count];
    
    
    do{
    do{
    System.out.print ("\nplease input the value of octet "+(count+1)+":");
    bit[count]= Keyboard.readDouble () ;
    if (bit[count]>=256)
    {
    System.out.print ("\nThis is the wrong size for a bit max is 255\n");
    }
    }while(bit[count]>=256);
    count++;
    }while(count>3);
    
    }
    
    }

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,800
    Rep Power
    25

    Default

    cant get this loop to work
    Is it one of these:
    do{
    do{
    System.out.print ("\nplease input the value of octet "+(count+1)+":");
    bit[count]= Keyboard.readDouble () ;
    if (bit[count]>=256)
    {
    System.out.print ("\nThis is the wrong size for a bit max is 255\n");
    }
    }while(bit[count]>=256);
    count++;
    }while(count>3);
    Try debugging the code by adding enough println() statements to show all the values used.

  3. #3
    Sno's Avatar
    Sno
    Sno is offline Senior Member
    Join Date
    Apr 2010
    Posts
    195
    Rep Power
    5

    Default

    Why kdr.valgetint

    why not scanner, I dont believe valgetint is correct, Shouldn't it be val.getInt()?
    what is the value of Q1 and Q2
    instead of while, try else if statement. While statement will keep looping until the statement is false.

    Java Code:
     System.out.print ("\nThis is a program that will workout class c addresses for subnetting\n");
    System.out.print ("\nplease input the method you wish to workout ip addresses\n");
    Q1 = kdr.valgetint("\n1:hosts per subnet\n2:bits borrowed\n3:total subnets\nplease input answer : ");
    :rolleyes: ~ Sno ~ :rolleyes:
    '-~ B.S. Computer Science ~-'

  4. #4
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    307
    Rep Power
    6

    Default

    Quote Originally Posted by manowar689 View Post
    Java Code:
    	do{
    // ensures that a valid int is got before trying to check it 
    		Q2 = kdr.valgetint("\nplease input number of hosts per subnet: ");
    			if(Q2==128 || Q2==64 || Q2==32 || Q2==16 || Q2==8 || Q2==4)
    			{
    			readbit();
    			}
    			else{
    			System.out.print ("\nThis is the wrong value it needs to be 128,64,32,16,8,4,2,1");
    			};
    	}while(Q2!=128 || Q2!=64 || Q2!=32 || Q2!=16 || Q2!=8 || Q2!=4);
    }
    Look closely at your while statement. Even if the Q2 value is a correct value, ie 128, it will still not be any of the other values so it will continue the while loop. Instead try using && instead of || as this will correct the logic.

  5. #5
    manowar689 is offline Member
    Join Date
    Jun 2010
    Posts
    16
    Rep Power
    0

    Default this part here

    its just this part here that is giving me the prob everything is working otherwise i just want this do while loop to loop unless the value is 128,64,32,16,8,4 and 2 if it does that then i can continue to the printout of the subnet addresses by making all of the answers refer to a printout if thats right lol but ye its the while line thats the prob

    Java Code:
    do{
    // ensures that a valid int is got before trying to check it 
    		Q2 = kdr.valgetint("\nplease input number of hosts per subnet: ");
    			if(Q2==128 || Q2==64 || Q2==32 || Q2==16 || Q2==8 || Q2==4)
    			{
    			readbit();
    			}
    			else{
    			System.out.print ("\nThis is the wrong value it needs to be 128,64,32,16,8,4,2,1");
    			};
    	}while(Q2!=128 || Q2!=64 || Q2!=32 || Q2!=16 || Q2!=8 || Q2!=4);

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,800
    Rep Power
    25

    Default

    while(Q2!=128 || Q2!=64 || Q2!=32 || Q2!=16 || Q2!=8 || Q2!=4)
    If Q2 = 128 then Q2 is not equal to 64 etc, so the rest of the tests will always be true.

    You want to continue if Q2 != 128 AND Q2!=64 AND the rest of the list of tests
    In other words, fall out of the loop for Q2 equal to any of the values shown in the tests

    Did you read what StormyWaters posted?

  7. #7
    manowar689 is offline Member
    Join Date
    Jun 2010
    Posts
    16
    Rep Power
    0

    Default

    iif i leave it like this then i cant get it to reask the question if the input is incorrect thats what the loop was for how would i fix this bit to validate that it is 128,64,32 etc etc.....
    Java Code:
    	Q2 = kdr.valgetint("\nplease input number of hosts per subnet: ");
    		if(Q2==128 || Q2==64 || Q2==32 || Q2==16 || Q2==8 || Q2==4)
    			{
    			readbit();
    			}
    			else{
    			System.out.print ("\nThis is the wrong value it needs to be 128,64,32,16,8,4,2,1");
    			};

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,800
    Rep Power
    25

    Default

    i cant get it to reask the question if the input is incorrect
    have you tried it? What does it do?

  9. #9
    manowar689 is offline Member
    Join Date
    Jun 2010
    Posts
    16
    Rep Power
    0

    Default

    hey stormywaters one worked thanks man your a god :)

  10. #10
    manowar689 is offline Member
    Join Date
    Jun 2010
    Posts
    16
    Rep Power
    0

Similar Threads

  1. equal() method
    By need_helpp in forum New To Java
    Replies: 3
    Last Post: 03-09-2010, 06:57 PM
  2. ComparisonFailure on equal(to the eye) strings
    By staffan in forum New To Java
    Replies: 2
    Last Post: 03-12-2009, 03:57 PM
  3. Problem using equal() method
    By ookie833 in forum New To Java
    Replies: 3
    Last Post: 11-18-2008, 06:19 AM
  4. checking if there are equal numbers
    By nalinda in forum New To Java
    Replies: 1
    Last Post: 11-18-2007, 07:21 AM
  5. checking if there are equal numbers
    By nalinda in forum New To Java
    Replies: 0
    Last Post: 11-18-2007, 03:13 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •