Skip to content
Advertisement

How would I get only the end result printed while also fixing the runtime error

I’m pretty new when it comes to coding in java (or coding in general), and I want the code below to print out only the end result (3), but it prints everything beforehand too, then once it reaches the end it gives me a runtime error

JavaScript

Advertisement

Answer

To get the final output, you need to move the print statement outside of the for loop. As for the runtime error, change the looping condition to i < sentence.length() instead of i <= sentence.length() because indexing starts from 0 and end at length-1.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement