/** * Author: Josiah Yoder * Class: SE1011-011 * Lesson: Week 5, Day 3 */ import java.util.Scanner; public class Example011_5_3 { public static void main(String[] ignored){ Scanner in = new Scanner(System.in); String str; int i; // Find parenthesis with a while loop. // Based on the flowchart we did on the board. System.out.println("Enter a string."); str = in.next(); i = 0; while( !( i>=str.length()-1 || (str.charAt(i)=='(' && str.charAt(i+1)==')')) ) { i++; } if(i>=str.length()-1) { System.out.println("Not found"); } else { System.out.println("String found at "+i); } // Find parenthesis with a for loop // Translated from the while-loop example System.out.println("Enter a string."); str = in.next(); for(i=0; !( i>=str.length()-1 || (str.charAt(i)=='(' && str.charAt(i+1)==')') ); i++) { // do nothing in loop. Everything is handled in the header. } if(i>=str.length()-1) { System.out.println("Not found"); } else { System.out.println("String found at "+i); } // Find parenthesis with a for loop // Starting from scratch. // Using this method helped us find the error in our original program. System.out.println("Enter a string."); str = in.next(); for(i=0; i=str.length()-1) { System.out.println("Not found"); } else { System.out.println("String found at "+i); } } }