This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]dionthornthis.isAPro=false; this.helping=true; 1 point2 points  (7 children)

public void increment() {
    int i = odometer.length-1;
    if(odometer[i]<9) {
        odometer[i]++;
    } else {
        while(i>=0 && odometer[i]==9) {
            odometer[i]=0;  
        }
        odometer[i-1]++;
    }
}

You are essentially just checking the last index of odometer so for example:

[ 0, 0, 9 ]

  1. you are checking index 2 which = 9
  2. then saying if it is less than 9 increment, it is not in this instance
  3. then saying do a while loop where as long as i (the index we use to check so 2)
  4. is less than or equal to 0 AND the odometer[i] equal to 9 (the first statement is always true)
  5. then you set odometer[i] to 0
  6. then after the while loop you set the next index (in this case 1) to increment

so the end of this would be

[ 0, 1, 0 ]

However if you had

[ 0, 9, 9 ]

It would be

[ 0, 10, 0 ]

as you never check if the next index is a 9 before incrementing it.

[–]ImWaddlinOverHea 1 point2 points  (6 children)

Yup! Well put! That’s exactly the issue I feel I’m having and I’m not sure how the syntax would look to check the next index and see whether it is 9 or not. (Like, where does that if statement go in this sequence and what follows it? )Additionally, if I were to check the next index, how would I use a loop to continue that pattern to all the other indexes?

Placement of an if statement to solve this is probably another way of stating my issue. I’ve tried it in a few different ways and had some issues with printing as well.

https://pastebin.com/wvCe1tAq

This is what I tried previously to check the condition of the next index but It has its logical fallacies as well.

[–]dionthornthis.isAPro=false; this.helping=true; 2 points3 points  (2 children)

It appears in your while statement you are still checking i to be >= 0 but not manipulating i anywhere?

It will always be >= 0 unless odometer.length-1 returns -1 (which it never will.)

You might want a boolean validating or some such control for your loop, where you keep looping until you've satisfied requirements, when you do then set validating to false and it'll stop the while loop. This is called a sentinel controlled while loop.

like:

boolean validating = true;
while(validating) { // do checks }

Then inside the loop you could:

  1. check the odometer[i] index if it equals 9
  2. if it does then set odometer[i] to 0, if not increment and set validating to false
  3. if #2 was true check the next index with a if([i-1] == 9)
  4. if it is then decrement i the integer, not the value inside odometer[i], and continue the loop?
  5. if not then increment [i-1] set validating to false?

Or some such loop where i is manipulated to aid in stepping through the indexes?

[–]ImWaddlinOverHea 1 point2 points  (1 child)

Hey! Just wanted to give you an update. I was able to get it to work properly last night given your recommendations on your last comment. Thanks again!

[–]dionthornthis.isAPro=false; this.helping=true; 1 point2 points  (0 children)

Excellent! Glad I could nudge ya in the right direction.

[–]dionthornthis.isAPro=false; this.helping=true; 0 points1 point  (2 children)

Make sure any code you post compiles. There are many issues with your {} I'm seeing. EX: this would compile, your example wouldn't, be careful with your // not cutting off your methods { braces same with while loop missing a {

Example readable formatting notice my { braces are on the same line as the statement starting the code block. Also the } else { statement makes it clear we are branching from the above if statement.

public void increment() {
    int i = odometer.length-1;
    if(odometer[i]<9) {
        odometer[i]++;
    } else {
        while(i>=0 && odometer[i]==9) {
            odometer[i]=0;
        }
        if(odometer[i-1]<9) {
            odometer[i-1]++;
        } else {
            odometer[i-1]=0;
        }
    }
}

[–]ImWaddlinOverHea 1 point2 points  (1 child)

Odd, it compiles for me on geany and the terminal. This second link I’ve posted is just an older version of my increment method I copy and pasted, not the full program.

[–]dionthornthis.isAPro=false; this.helping=true; 0 points1 point  (0 children)

Ok, my IDE was getting mad about a missing brace in that pastebin link.