Follow us on Twitter!
Understanding is the answer, hatred is the problem, and hackers are the slaves abused and destroyed in the process of peace online - Deshouleres
Monday, April 27, 2015
HellBoundHackers Main:
HellBoundHackers Find:
HellBoundHackers Information:
HellBoundHackers Exploit:
HellBoundHackers Programming:
HellBoundHackers Think:
HellBoundHackers Track:
HellBoundHackers Patch:
HellBoundHackers Other:
HellBoundHackers Need Help?
Members Online
Total Online: 29
Guests Online: 28
TeamSpeak Online : 5 Members Online: 1

Registered Members: 88073
Newest Member: madatoyo
Latest Articles
Our sponsor needs your help!

One of our sponsors needs your help,
Go check out their video and Don't forget to vote !

~ Thanks for your help
View Thread

HellBound Hackers | Computer General | Programming


Java Programming Help


Posts: 13
Location: USA
Joined: 05.04.12
Posted on 30-09-12 00:22
I am currently going to class for programming, and our challenge this week was all about classless objects. Not that it matters, but I decided to do mine on Linux Distro\\\\\\\\\\\\\\\'s; and a portion of the project was to let the user select a range, and then divide that range into 10 equal increments. Then display a histogram ( bar graph ) that displays those that fall into each of the increments.

I have gotten my code to work, no problem there. The problem is I believe there is a more efficient way to do it, and I just cannot see it. It is just bugging the hell out of me, and I just can\\\\\\\\\\\\\\\'t look at this one section of code without thinking that there has to be a better way of doing it without utilizing a 11 branch if statement. Was just hoping that someone else could see what I can\\\\\\\\\\\\\\\'t.


   for( int ix=0; ix<length; ix++ ){
      if( distro[ix] ._sizeMB < lowB ){
      }else if( distro[ix] ._sizeMB < (lowB + increment) )
      else if( distro[ix] ._sizeMB < (lowB + (increment*2)) )
      else if( distro[ix] ._sizeMB < (lowB + (increment*3)) )
      else if( distro[ix] ._sizeMB < (lowB + (increment*4)) )
      else if( distro[ix] ._sizeMB < (lowB + (increment*5)) )
      else if( distro[ix] ._sizeMB < (lowB + (increment*6)) )
      else if( distro[ix] ._sizeMB < (lowB + (increment*7)) )
      else if( distro[ix] ._sizeMB < (lowB + (increment*8)) )
      else if( distro[ix] ._sizeMB < (lowB + (increment*9)) )
      else if( distro[ix] ._sizeMB < highB )


What the function is doing is loading up an array with the portions of data that fall into that realm. Increment is equal to (highB - lowB)/10, the length of the distro[] array can be any number, lowB and highB and user input, and occur[] is just an array I created to store the occurances of each of the increments, and distro[] .sizeMB is equal to the size of the Linux Distro in MB.

I love efficient code, and I know there has to be a way to execute what this does with a lot less code involved. I keep thinking it has to do with changing out the (increment*#) and the occur[#] with an equation from ix, but since the length is undefined I just can\\\\\\\\\\\\\\\'t see the equation...

This is in Java by the way. Probably should have mentioned that earlier..

Edited by tuXthEhxR on 30-09-12 02:53

RE: Java Programming Help


Posts: 13
Location: USA
Joined: 05.04.12
Posted on 02-10-12 22:37
Alright, I figured it out. I ended up just nesting a for loop inside the original for loop:


for( int ix=0; ix<length; ix++ ){
   for(int ixx=0; ixx<10; ixx++){

      if(distro[ix] ._sizeMB >= lowB &&
            distro[ix] ._sizeMB < (lowB + increment ) ){

      }else if(distro[ix] ._sizeMB >= lowB &&
         distro[ix] ._sizeMB < (lowB + (increment * (ixx+1) ) ) ){


In my mind, this is much more elegant then how I orginially had it; hate having more then 3 or 4 if statements, it just looks ugly. Sad that I spent so much time on what ended up being quite simple in a way.

It was still bugging me because technically it would still take the same amount of processor time to run the improvement that I wrote above as a regular 11 branch if statement. Think I found the most optimized way to write the code.


for( int ix=0; ix<length; ix++ ){
   if(distro[ix] ._sizeMB > lowB && distro[ix] ._sizeMB < highB){
      indexD = (distro[ix] ._sizeMB) - lowB;
      index = (int) (indexD / increment);

Edited by tuXthEhxR on 03-10-12 05:18