Question

In: Computer Science

in Java, I am writing a program where you need to pick a color. The color...

in Java, I am writing a program where you need to pick a color. The color must be red, blue, or green and is a user input. If they put in a different value I need the program to stop running. So I essentially need to write code that says, if variableColor does not equal "red", "blue", or "green" then I need to print the statement "invalid response" and exit the program. How can I write this?

Solutions

Expert Solution

import java.util.Scanner;

public class ColorChooser {  

public static void main(String[] args)
   {
       String color;
       Scanner scan = new Scanner(System.in);
       // input the color
       System.out.print("Enter a color: ");
       color = scan.nextLine();
      
       // validate color is in ["red", "blue" , "green"] in any case, if not display invalid response
       if(!color.equalsIgnoreCase("red") && !color.equalsIgnoreCase("blue") && !color.equalsIgnoreCase("green"))
           System.out.println("Invalid response");
       else
       {
           // perform the steps required
           System.out.println("Valid response");
       }
   }
}

//end of program

Output:


Related Solutions

I am writing a paper on Financial Restructuring. I am required to pick a company that...
I am writing a paper on Financial Restructuring. I am required to pick a company that is in [potential] trouble of defaulting/bankruptcy. I have chosen Toys R Us. I am required to: 1) Analyze the company's (Toys R Us) financial history and current financial situation. 2) Propose a financial restructuring proposal. It is my understanding that I need to look at all possible financial statements, income statements, cash flows, and use financial tools to "financially restructure" and propose a "fix"...
I am writing a matlab program where I created a figure that has a few different...
I am writing a matlab program where I created a figure that has a few different elements to it and now I need to move that figure into a specific excel file into a specific set of boxes in the excel file. With numbers and text I have always used the xlswrite function for this in order to put data into specific boxes. How do I do the same with this figure? The figure I have is called like this:...
I am trying to write a program in Java: Pick 4 cards and display the cards...
I am trying to write a program in Java: Pick 4 cards and display the cards and their total value (Ace = 1, King = 13, Queen = 12, and Jack 11...etc. , BUT you should check that no card is a duplicate... there is only one "Ace of Spades" per deck for example. I need to utilize the displayMenu so the program continues to run for the user without breaking. My program is not running correctly. import java.util.Scanner; import...
Hello, I am writing the initial algorithm and refined algorithm for a program. I just need...
Hello, I am writing the initial algorithm and refined algorithm for a program. I just need to make sure I did it correctly. I'm not sure if my formula is correct. I appreciate any help, thank you! TASK: Write a program that will calculate the final balance in an investment account. The user will supply the initial deposit, the annual interest rate, and the number of years invested. Solution Description: Write a program that calculates the final balance in an...
I'm working in Java and am working on a project where I need to find an...
I'm working in Java and am working on a project where I need to find an average. The catch is that for some of the values there is no data because they will be entered at a later date. I have variables assigned so that for each entry if there is an input I'll have it say _____available = 1, otherwise the variable will equal 0. I'll use an example to make this more clear. Let's say I am trying...
I need this in java using textpad. I am missing a few lines where I added...
I need this in java using textpad. I am missing a few lines where I added in comments. I don't know what I need to add in. Here are the two programs as pasteable code.The comments in the code say what I need done. The two programs are below. I need it to work with the generic version of SLLNode. It is posted at the bottom. public class ListDemoHw { public static void printLinkedList(SLLNode node) { // display all elements...
I need this in java using textpad. I am missing a few lines where I added...
I need this in java using textpad. I am missing a few lines where I added in comments. I don't know what I need to add in. Here are the two programs as pasteable code.The comments in the code say what I need done. The two programs are below. public class ListDemoHw { public static void printLinkedList(SLLNode node) { // display all elements in the linked list while(node != null) { System.out.print(node.info + " "); node = node.next; // move...
I am writing a shell program in C++, to run this program I would run it...
I am writing a shell program in C++, to run this program I would run it in terminal like the following: ./a.out "command1" "command2" using the execv function how to execute command 1 and 2 if they are stored in argv[1] and argv[2] of the main function?
I am writing a jave program. I have seen the topic before but I want to...
I am writing a jave program. I have seen the topic before but I want to do it this way. And I got an error says: BonusAndDayOffRew.java:14: error: variable monthlySales might not have been initialized getSales(monthlySales); ^ Here is the description:A retail company assigns a $5000 store bonus if monthly sales are $100,000 or more. Additionally, if their sales exceed 125% or more of their monthly goal of $90,000, then all employees will receive a message stating that they will...
hello, I am writing an article why pharmacists need to be utilized as precribers in the...
hello, I am writing an article why pharmacists need to be utilized as precribers in the U.S in effort to help accessibility for women to obtain birth control? any thoughts based on evidence? please provide reference of articles.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT