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
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));
}
}
}
Comments
Leave a comment