Skip to content

Instantly share code, notes, and snippets.

@mihaichris
Last active January 20, 2023 19:14
Show Gist options
  • Save mihaichris/f60105f9617e651ece7531b0ffbf543e to your computer and use it in GitHub Desktop.
Save mihaichris/f60105f9617e651ece7531b0ffbf543e to your computer and use it in GitHub Desktop.
An example of a Java program that uses a while loop to check if a character is in a string of no more than 20 characters, and allows the user to continue searching for other characters or finish the process. The class is tested using two functional testing techniques: Equivalence Partitioning, Boundary Value Analysis and Category Partition.
import java.util.Scanner;
public class CharacterSearch {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String inputString;
char searchChar;
String searchAgain = "y";
while (searchAgain.equals("y")) {
// get the input string
System.out.print("Enter a string of no more than 20 characters: ");
inputString = scanner.nextLine();
while(inputString.length()>20){
System.out.println("String is too long, please enter a string of no more than 20 characters:");
inputString = scanner.nextLine();
}
// get the character to search for
System.out.print("Enter a character to search for: ");
searchChar = scanner.nextLine().charAt(0);
// search for the character in the string
int charIndex = inputString.indexOf(searchChar);
if (charIndex == -1) {
System.out.println("The character '" + searchChar + "' was not found in the string '" + inputString + "'.");
} else {
System.out.println("The character '" + searchChar + "' was found at position " + (charIndex + 1) + " in the string '" + inputString + "'.");
}
// ask the user if they want to search for another character
System.out.print("Search for another character? (y/n): ");
searchAgain = scanner.nextLine();
}
System.out.println("Thank you for using the Character Search program!");
}
public int indexOfCharInString(String inputString, char searchChar) {
if (inputString.length() > 20) {
return -1;
}
return inputString.indexOf(searchChar);
}
}
import org.junit.Test;
import static org.junit.Assert.*;
public class CharacterSearchTest {
CharacterSearch cs = new CharacterSearch();
@Test
public void testIndexOfCharInString2() {
// Test case for the minimum length of string
String inputString = "a";
char searchChar = 'a';
int expectedResult = 0;
int result = cs.indexOfCharInString(inputString, searchChar);
assertEquals(expectedResult, result);
// Test case for the maximum length of string
inputString = "abcdefghijklmnopqrsz";
searchChar = 'z';
expectedResult = 19;
result = cs.indexOfCharInString(inputString, searchChar);
assertEquals(expectedResult, result);
// Test case for the first character of the string
inputString = "abcdefghijklmnopqrst";
searchChar = 'a';
expectedResult = 0;
result = cs.indexOfCharInString(inputString, searchChar);
assertEquals(expectedResult, result);
// Test case for the last character of the string
inputString = "abcdefghijklmnopqrsz";
searchChar = 'z';
expectedResult = 19;
result = cs.indexOfCharInString(inputString, searchChar);
assertEquals(expectedResult, result);
// Test case for special characters
inputString = "!@#abcdefghijklmnopq";
searchChar = '#';
expectedResult = 2;
result = cs.indexOfCharInString(inputString, searchChar);
assertEquals(expectedResult, result);
}
}
import org.junit.Test;
import static org.junit.Assert.*;
public class CharacterSearchTest {
CharacterSearch cs = new CharacterSearch();
@Test
public void testIndexOfCharInString3() {
// Test case for strings with length less than 20
String inputString = "abc";
char searchChar = 'b';
int expectedResult = 1;
int result = cs.indexOfCharInString(inputString, searchChar);
assertEquals(expectedResult, result);
// Test case for strings with length equal to 20
inputString = "abcdefghijklmnopqrsx";
searchChar = 'x';
expectedResult = 19;
result = cs.indexOfCharInString(inputString, searchChar);
assertEquals(expectedResult, result);
// Test case for strings with length greater than 20
inputString = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz";
searchChar = 'z';
expectedResult = -1;
result = cs.indexOfCharInString(inputString, searchChar);
assertEquals(expectedResult, result);
}
}
import org.junit.Test;
import static org.junit.Assert.*;
public class CharacterSearchTest {
CharacterSearch cs = new CharacterSearch();
@Test
public void testIndexOfCharInString() {
//Positive Test Case
String inputString = "abcdefghijklmnopqrst";
char searchChar = 'a';
int expectedResult = 0;
int result = cs.indexOfCharInString(inputString, searchChar);
assertEquals(expectedResult, result);
//Negative Test Case
inputString = "abcdefghijklmnopqrstuvwxyz";
searchChar = 'z';
expectedResult = -1;
result = cs.indexOfCharInString(inputString, searchChar);
assertEquals(expectedResult, result);
// Test case for max length of string
inputString = "abcdefghijklmnopqrsz";
searchChar = 'z';
expectedResult = 19;
result = cs.indexOfCharInString(inputString, searchChar);
assertEquals(expectedResult, result);
// Test case for min length of string
inputString = "a";
searchChar = 'a';
expectedResult = 0;
result = cs.indexOfCharInString(inputString, searchChar);
assertEquals(expectedResult, result);
// Test case for special characters
inputString = "!@#abcdefghijklmnopq";
searchChar = '#';
expectedResult = 2;
result = cs.indexOfCharInString(inputString, searchChar);
assertEquals(expectedResult, result);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment