/* * Problem Set 2 * * File: StringTest.java * * Author: Christine Papadakis-Kanaris * Coutse: CS112, Boston University * * * Purpose: This is a sample program performing basic input and * output in Java */ import java.util.Scanner; public class StringTest { public static void main(String[] args) { String look_for = "CS112"; int num_times = inputString(look_for); System.out.print( look_for + " was entered " + num_times + " time" ); if (num_times == 0 || num_times > 1) System.out.println( "s!" ); else System.out.println( "!" ); } // main() public static int inputString( String str ) { // Print out welcome message System.out.println("\nWelcome to the String Test Program! Looking for " + str + "!"); // Declare a scanner object for user input Scanner userInput = new Scanner(System.in); int entered = 0; System.out.print("\nType in a line of text (a String) or \"quit\" to end: "); // Continue to receive user input until some // sentinal (i.e. final) value is entered. // In this case, the user must enter the // word "quit". while (userInput.hasNextLine()) { String line = userInput.nextLine(); if ( line.equals("quit") ) break; else { System.out.println("You input: " + line); if (line.equals( str )) entered++; System.out.print("\nType in a line of text (a String) or \"quit\" to end: "); } } // while System.out.println("bye!"); userInput.close(); return(entered); } // inputString() } // class