Write a Java code that uses an input dialog box to ask the user to enter a sentence that may or may not contain a substring, “Ruby”.
Use the an appropriate Java method (e.g. “contains()”) to check if the given string contains the word “Ruby”. If not, display “The substring 'Ruby' is not found.” in a message box. If found, replace every occurrence of the work “Ruby” with “Java”, and then display the revised sentence in a message box.
Test the program three times. First, test the program without the “Ruby” substring. Second, test the program with one occurrence of the “Ruby” substring. Third, test the program with two or more occurrences of the “Ruby” substring.
import java.util.Scanner;
public class Main
{
public static void main(String[] args) {
Scanner input =new Scanner(System.in);
Scanner scan =new Scanner(System.in);
String someSymbols="";
int counter=0;
do{
System.out.println("Please provide the word what contains or didn't symbols 'Ruby' and press Enter:");
someSymbols=input.nextLine();
if(someSymbols.contains("Ruby")){
String newValue=someSymbols.replaceFirst("Ruby","Java");
System.out.println(newValue);
}else if(!(someSymbols.contains("Ruby"))){
System.out.println("The substring 'Ruby' is not found.");
}
System.out.println("One more? If yes press 1 or not prress 2 and Enter:");
counter=scan.nextInt();
}while(counter!=2);
input.close();
scan.close();
}
}
Comments
Leave a comment