Answer to Question #240840 in Java | JSP | JSF for java1

Question #240840

Write a program named StringCompare.java which will prompt the user for three Strings, his/her favorite color, favorite fruit and favorite flower. 


Use Scanner object to read in user's strings in corresponding order (color, fruit, flower). (I will expect you to use three next() methods for three inputs!)


Conduct two separate checks in the following order:

1. If either of the values entered for color or fruit is 'orange', convert both color and fruit to UPPERCASE.

2. If either of the values entered (for color or flower) is 'violet', convert both values for color and flower to "vIoLeT" (alternating lower and upper case letters).  

Then print each value on its own line: color, then fruit, then flower.


Sample I/O (user input is in orange bold-italics):


Enter your favorite color, fruit and flower:  

pink orange rose

PINK

ORANGE

rose




1
Expert's answer
2021-09-23T17:47:55-0400
import java.util.Scanner;
public class Main
{
	public static void main(String[] args) {
	    Scanner input = new Scanner(System.in);
	    System.out.println("Enter your favorite color: ");
	    String color=input.next();
	    System.out.println("Enter your favorite fruit: ");
	    String fruit=input.next();
	    System.out.println("Enter your favorite flower: ");
	    String flower=input.next();
	    System.out.print(color+" "+ fruit+" "+ flower);
	    String upper_str = color.toUpperCase();
	    String upper1_str = fruit.toUpperCase();
	    String upper2_str = flower.toUpperCase();
	    System.out.print("\n"+upper_str+"\n"+upper1_str+"\n"+flower);
	    if(color=="orange" || fruit=="orange"){
	        System.out.println("String in uppercase: " + upper_str);
	        System.out.println("String in uppercase: " + upper1_str);
	       
	    }
	    if(color=="violet"||flower=="violet"){
	       color = color.toLowerCase();
      char[] ch = color.toCharArray();
      for(int i=0; i<ch.length; i=i+2){
         ch[i] = Character.toUpperCase(ch[i]);  
	    }
		System.out.println(new String(ch));
	}
}
}

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
APPROVED BY CLIENTS