Answer to Question #181323 in Java | JSP | JSF for Phil

Question #181323

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.


1
Expert's answer
2021-04-14T12:48:48-0400
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();
	}
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog