Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java for loop won't run...?

Tags:

java

I am working on a homework assignment, and the crucial loop of the program is giving me trouble. My teacher has informed me that she will take off points if I use a while loop for a counter-controlled variable, so I'm anxious to get this right.

Here's what I would like to work, and what I feel in my heart should work:

for ( int check = 0; check == value; check++ ) {
    int octal = getOctal();
    int decimal = convertOctal( octal );
    System.out.printf( "%d:%d", octal, decimal );
}

However, this loop does not run. I tried doing it with a while loop, and it worked perfectly!

int check = 0;
while ( check < value )
{
    int octal = getOctal();
    int decimal = convertOctal( octal );
    System.out.printf( "%d:%d", octal, decimal );
    check++;
}

Here is the rest of the main method:

public static void main ( String args[] )
{
    int value = getCount();

    while ( value < 0 )
    {
        System.out.print( "\nYou must enter a positive number" );
        value = getCount();
    }

    if ( value == 0 )
    {
        System.out.print( "\n\nNo numbers to convert.\n\n" );
    }
    else
    {   
        int check = 0;
        while ( check < value )
        {
            int octal = getOctal();
            int decimal = convertOctal( octal );
            System.out.printf( "%d:%d", octal, decimal );
            check++;
        }
    }
}

Yes, this is an octal-to-decimal converter. I wrote the converter method myself from scratch and am ridiculously proud of it.

EDIT: MY QUESTION is, what's wrong here? EDIT part deux: Thanks all for your help in clearing up my misunderstanding. Onward to method documentation!

like image 910
thephfactor Avatar asked May 07 '26 08:05

thephfactor


1 Answers

for ( int check = 0; check == value; check++ )

This will only run if check == value. Modify to:

for ( int check = 0; check < value; check++ )
like image 132
Jean Waghetti Avatar answered May 09 '26 21:05

Jean Waghetti



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!