This is my code and I need to add a verification of input lo
This is my code and I need to add a verification of input loop to this so that the user only selects the cars that I have given them. This code is done through textpad and java.
import java.util.Scanner;
public class Worksheet_7
 { public static void main(String[]args)
    { Scanner Fred = new Scanner(System.in);
    System.out.println(\"Today we practice using switch statements \ \ \");
    char Ans;
    char DoMore = \'Y\';
    while(DoMore==\'Y\' || DoMore==\'y\')
    {
        // switch statement code goes here
    System.out.println(\"What kind of card do you drive?\");
    System.out.println(\"(F)ord, (C)hevy, (B)uick,(M)azda,(H)onda,(T)oyota,(S)ubaru\");
    String Word = Fred.next();
    Ans=Word.charAt(0);
    switch(Ans)
    {   case \'C\' : System.out.println(\"Chevy - the heartbeat of America\");
    System.out.println(\"Chevy is awesome\");
       break;
       case \'F\' : System.out.println(\"Ford tough\");
       System.out.println(\"Chevy is better\");
       break;
       case \'B\' : System.out.println(\"Buick - Luxury you can afford\");
       System.out.println(\"Chevy is better\");
       break;
       case \'M\' : System.out.println(\"Honda - Always dependable\");
       System.out.println(\"Chevy is better\");
       break;
       case \'T\' : System.out.println(\"Toyota - I have a toyota\");
       break;
       case \'S\' : System.out.println(\"Subaru - my friend has one\");
       break;
       case \'H\' : System.out.println(\"Need a better car....?\");
       System.out.println(\"Buy a Chevy\");
       break;
     } // switch
     System.out.println(\"Try again? (Y/N)\");
     Word = Fred.next();
     DoMore = Word.charAt(0);
 } // end loop
     System.out.println(\"\ \  done for now..\");
 } // end main
 } // end class
Solution
import java.util.Scanner;
public class Worksheet_7
 { public static void main(String[]args)
      { Scanner Fred = new Scanner(System.in);
      System.out.println(\"Today we practice using switch statements \ \ \");
      char Ans;
      char DoMore = \'Y\';
      while(DoMore==\'Y\' || DoMore==\'y\')
      {
          // switch statement code goes here
      System.out.println(\"What kind of card do you drive?\");
      System.out.println(\"(F)ord, (C)hevy, (B)uick,(M)azda,(H)onda,(T)oyota,(S)ubaru\");
      String Word = Fred.next();
      Ans=Word.charAt(0);
      //VERFICATION CODE...
      while(true )
      {
          if(Ans== \'H\'||Ans== \'S\' ||Ans== \'T\' ||Ans== \'M\' ||Ans== \'B\' ||Ans== \'C\' ||Ans== \'F\')
          {
          break;
          }
      System.out.println(\"Error: Please enter the car that is in the list\");
      System.out.println(\"What kind of card do you drive?\");
      System.out.println(\"(F)ord, (C)hevy, (B)uick,(M)azda,(H)onda,(T)oyota,(S)ubaru\");
      Word = Fred.next();
      Ans=Word.charAt(0);
      }
      switch(Ans)
      {    case \'C\' : System.out.println(\"Chevy - the heartbeat of America\");
      System.out.println(\"Chevy is awesome\");
          break;
          case \'F\' : System.out.println(\"Ford tough\");
          System.out.println(\"Chevy is better\");
          break;
          case \'B\' : System.out.println(\"Buick - Luxury you can afford\");
          System.out.println(\"Chevy is better\");
          break;
          case \'M\' : System.out.println(\"Honda - Always dependable\");
          System.out.println(\"Chevy is better\");
          break;
          case \'T\' : System.out.println(\"Toyota - I have a toyota\");
          break;
          case \'S\' : System.out.println(\"Subaru - my friend has one\");
          break;
          case \'H\' : System.out.println(\"Need a better car....?\");
          System.out.println(\"Buy a Chevy\");
          break;
      } // switch
      System.out.println(\"Try again? (Y/N)\");
      Word = Fred.next();
      DoMore = Word.charAt(0);
 } // end loop
      System.out.println(\"\ \  done for now..\");
 } // end main
 } // end class
OUTPUT:-
run:
 Today we practice using switch statements
 What kind of card do you drive?
 (F)ord, (C)hevy, (B)uick,(M)azda,(H)onda,(T)oyota,(S)ubaru
 F
 Ford tough
 Chevy is better
 Try again? (Y/N)
 Y
 What kind of card do you drive?
 (F)ord, (C)hevy, (B)uick,(M)azda,(H)onda,(T)oyota,(S)ubaru
 C
 Chevy - the heartbeat of America
 Chevy is awesome
 Try again? (Y/N)
 Y
 What kind of card do you drive?
 (F)ord, (C)hevy, (B)uick,(M)azda,(H)onda,(T)oyota,(S)ubaru
 B
 Buick - Luxury you can afford
 Chevy is better
 Try again? (Y/N)
 Y
 What kind of card do you drive?
 (F)ord, (C)hevy, (B)uick,(M)azda,(H)onda,(T)oyota,(S)ubaru
 M
 Honda - Always dependable
 Chevy is better
 Try again? (Y/N)
 Y
 What kind of card do you drive?
 (F)ord, (C)hevy, (B)uick,(M)azda,(H)onda,(T)oyota,(S)ubaru
 H
 Need a better car....?
 Buy a Chevy
 Try again? (Y/N)
 Y
 What kind of card do you drive?
 (F)ord, (C)hevy, (B)uick,(M)azda,(H)onda,(T)oyota,(S)ubaru
 T
 Toyota - I have a toyota
 Try again? (Y/N)
 Y
 What kind of card do you drive?
 (F)ord, (C)hevy, (B)uick,(M)azda,(H)onda,(T)oyota,(S)ubaru
 S
 Subaru - my friend has one
 Try again? (Y/N)
 Y
 What kind of card do you drive?
 (F)ord, (C)hevy, (B)uick,(M)azda,(H)onda,(T)oyota,(S)ubaru
 P
 Error: Please enter the car that is in the list
 What kind of card do you drive?
 (F)ord, (C)hevy, (B)uick,(M)azda,(H)onda,(T)oyota,(S)ubaru
 G
 Error: Please enter the car that is in the list
 What kind of card do you drive?
 (F)ord, (C)hevy, (B)uick,(M)azda,(H)onda,(T)oyota,(S)ubaru
 R
 Error: Please enter the car that is in the list
 What kind of card do you drive?
 (F)ord, (C)hevy, (B)uick,(M)azda,(H)onda,(T)oyota,(S)ubaru
 F
 Ford tough
 Chevy is better
 Try again? (Y/N)
 N
 done for now..
 BUILD SUCCESSFUL (total time: 59 seconds)



