When you iterate over the string, once you reach the end of the screen, the substring function tries to look pats the end of the string. To fix this, you subtract the substring length from the word length, and iterate over the word.
Substring in a string hacks
public class VowelFinder {
public static void main(String[] args) {
String word = "iterate";
boolean found = false; // will be set to true once substring is found
for (int i = 0; i <= word.length()-1; i++) { //iterating forwards: starting at first index (0) and going to the length of the word.. let's try word.length
switch(word.charAt(i)){
case 'a':
found = true;
break;
case 'e':
found = true;
break;
case 'i':
found = true;
break;
case 'o':
found = true;
break;
case 'u':
found = true;
break;
default:
continue;
}
}
if (found)
System.out.println("substring is found within string!");
else
System.out.println("substring is NOT within string");
}
}
VowelFinder.main(null);
Break Popcorn Hacks
public class BreakHack {
public static void main(String[] args) {
int targetNumber = 42; //numb we want
int[] numbers = {10, 20, 30, 40, 50, 60, 70}; //numb array
Boolean found = false;
for (int number : numbers) {
if (number == targetNumber) {
System.out.println("Number is found");
found = true;
break;
}
}
if (found==false) {
System.out.println("Number iS NOT found");
}
}
}
BreakHack.main(null);
Pyramid Hacks
import java.util.Scanner;
public class InteractivePyramid {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the symbol you want to use: ");
char symbol = scanner.next().charAt(0);
System.out.print("Enter the number of rows for the pyramid: ");
int numRows = scanner.nextInt();
for (int i = 1; i <= numRows; i++) {
//print space before the symbol
for (int j = 1; j <= numRows - i; j++) {
System.out.print(" ");
}
//print
for (int k = 1; k <= 5* i - 1; k++) {
System.out.print(symbol);
}
System.out.println(); //next line
}
scanner.close();
}
}
InteractivePyramid.main(null)
What is a nested iteration, continue statement, and break statement (in your own words)?
nested iteration is when one for loop is "nested" inside another
a continue statement is when the code doesn't execute the rest of the code in a while or for loop, and goes to the beginning of the loop
a break statement is when the code completely exists the while or for loop
Create a simple example of a continue statement or break statement
public class breakLoop {
public static void main(String[] args) {
int i = 0;
while(i<=5) {
if (i==4) {
break;
}
System.out.println(i);
i++;
}
}
}
breakLoop.main(null);
4.5 Informal code answers
How many times will statement 1 execute?
Answer: 30
How many times will statement 2 execute?
Answer: 10
How many times will statement 3 execute?
Answer: 9
What is the min/max number of times statement 4 will execute?
Popcorn hacks #1
When you iterate over the string, once you reach the end of the screen, the substring function tries to look pats the end of the string. To fix this, you subtract the substring length from the word length, and iterate over the word.
Substring in a string hacks
public class VowelFinder { public static void main(String[] args) { String word = "iterate"; boolean found = false; // will be set to true once substring is found
Break Popcorn Hacks
public class BreakHack { public static void main(String[] args) { int targetNumber = 42; //numb we want int[] numbers = {10, 20, 30, 40, 50, 60, 70}; //numb array Boolean found = false; for (int number : numbers) { if (number == targetNumber) { System.out.println("Number is found"); found = true; break; } } if (found==false) { System.out.println("Number iS NOT found"); } } } BreakHack.main(null);
Pyramid Hacks
import java.util.Scanner;
public class InteractivePyramid { public static void main(String[] args) { Scanner scanner = new Scanner(System.in);
} InteractivePyramid.main(null)
What is a nested iteration, continue statement, and break statement (in your own words)? nested iteration is when one for loop is "nested" inside another a continue statement is when the code doesn't execute the rest of the code in a while or for loop, and goes to the beginning of the loop a break statement is when the code completely exists the while or for loop Create a simple example of a continue statement or break statement public class breakLoop { public static void main(String[] args) { int i = 0; while(i<=5) { if (i==4) { break; } System.out.println(i); i++; } } } breakLoop.main(null);
4.5 Informal code answers
How many times will statement 1 execute?
Answer: 30
How many times will statement 2 execute?
Answer: 10 How many times will statement 3 execute?
Answer: 9 What is the min/max number of times statement 4 will execute?
Answer: Min: 0, max: infinity
How many times will statement https://github.com/VINERAJ/CSAblog/issues/5 execute?
Answer: 12 times
How many times will statement #6 execute?
Answer: Infinity 4.5 Hacks 1000 times, 44 times 28 times a: // 3a code for (int i=0; i<10; i++) { System.out.println(i); } b: // 3b code int i = 0; while (i<10) { System.out.println(i); i++; } c: // 3c code int h = 0; for (int i = 0; i<5; i++) { for (int j = 0; j<2; j++) { System.out.println(h); h++; } }