Brian
Brian

Reputation: 115

count incrementer is wrong

My assignment calls for the line number to be display with the output. The professor suggested I do it with a counter and as seeing Java doesn't have an easy way to print out the current line number, I just created a counter as suggested. The below code is as follows:

  //Count Increment
    for (count = 1; count<= 5; count++)
    {

    }   

    //Display information
    System.out.println(count + "." + " " + "Street:"+ " " + streetName + " " +  "#" + streetNumber);
    System.out.println(count + "." + " " + "Total Rooms:"+ " " + numofRooms);
    System.out.println(count + "." + " " + "Total Area:"+ " " + totalSqFt + " sq.ft");
    System.out.println(count + "." + " " + "The price per Sq. Ft is " + "$" + priceperSqFt);
    System.out.println(count + "." + " " + "The estimated property value is "+ "$" + estimatedPropertyvalue);

However, the output starts the line counter at six as demonstrated here:

6. Street: park avenue #44
6. Total Rooms: 5
6. Total Area: 2500.0 sq.ft
6. The price per Sq. Ft is $120.4
6. The estimated property value is $301000.0

Removing the brackets doesn't help either. How can I get the line count to correctly state 1,2,3,4,5?

Please ask for clarification if needed!! Thanks.

Upvotes: 0

Views: 142

Answers (3)

rdk1992
rdk1992

Reputation: 406

Your prints are outside of the for loop. Your for loop ends when the counter is "6" which is when it exits the for loop. This variable doesn't change so the current value is "6",that is why it always prints "6" below on your code. If you want to print the line number for each instruction you could do something like this:

        count = 0;
        System.out.println(++count + "." + " " + "Street:"+ " " + streetName + " " +  "#" + streetNumber);

"++count", you increment the variable the moment you write a line, in the first case it should print 1 then 2 etc. Hope this helped :)

The loop is not required cause you are only counting the lines one time each. If you put those lines in a loop that goes from 0 to 5 you will be counting each line 5 times. Since you only need to count each line ONE time you dont need the loop and just the simple increment I previously mentioned. Hope this clears out why the loop is not required

Upvotes: 2

questborn
questborn

Reputation: 2855

class Print{

    static int lineno = 0;

    private int static getLineNo(){
        lineno = lineno + 1;
        return lineno;
    }
}


//Display information
System.out.println(Print.getLineNo() + "." + " " + "Street:"+ " " + streetName + " " +  "#" + streetNumber);
System.out.println(Print.getLineNo() + "." + " " + "Total Rooms:"+ " " + numofRooms);
System.out.println(Print.getLineNo() + "." + " " + "Total Area:"+ " " + totalSqFt + " sq.ft");
System.out.println(Print.getLineNo() + "." + " " + "The price per Sq. Ft is " + "$" + priceperSqFt);
System.out.println(Print.getLineNo() + "." + " " + "The estimated property value is "+ "$" + estimatedPropertyv

Upvotes: 1

milam
milam

Reputation: 31

I assume that you have somewhere above this a line defining count:

int count;

So after the for loop, you've incremented count to 6 and then started printing with count left at the last incremented value from the for loop.

So, remove the for loop and just pre-increment the count variable for each line of ouput.

int count = 0;

//Display information
System.out.println( (++count) + "." + " " + "Street:"+ " " + streetName + " " +  "#" + streetNumber);

...

Upvotes: 1

Related Questions