Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Need help with this fortune telling java program: It works sometimes depending o

ID: 3562875 • Letter: N

Question

Need help with this fortune telling java program: It works sometimes depending on what the user inputs:

for example this is an outcome that I received and not sure how to fix it.

select a number: 123
3
select the color from red,blue,green,yellow
Enter 1 for RED
2 for BLUE
3 for GREEN
4 for YELLOW
3

And it just ends there with out spitting out a fortune. Also I just wanted the user to pick a number between 0-2. Need help fixing that too.

import java.util.Random;
import java.util.Scanner;
public class Fortune_Tell {

   static Random rand=new Random();
   static Scanner sc = new Scanner(System.in);
  
   public static void main(String[] args) {
       // variables
   int choice;
   int color = 0,number = 0;

   // if choice is 0 -> 0,1,2
   // if choice is 1 -> 1,2,3
   // if choice is 2 -> 2,3,4
   // if choice is 3 -> 3,4,5
   // if choice is 4 -> 4,5,6
   choice=rand.nextInt(4);
   switch(choice)
   {
   case 0: System.out.println("select a number: "+choice+""+(choice+1)+" "+ " "+(choice+2));number=sc.nextInt();

                    System.out.println("select the color from red,blue,green,yellow");

                    System.out.println("Enter 1 for RED 2 for BLUE 3 for GREEN 4 for YELLOW");

                    color=sc.nextInt();

                    break;

           case 1:System.out.println("select a number: "+choice+""+(choice+1)+""+(choice+2));

                number=sc.nextInt();

                System.out.println("select the color from red,blue,green,yellow");

                System.out.println("Enter 1 for RED 2 for BLUE 3 for GREEN 4 for YELLOW");

                color=sc.nextInt();

                break;

           case 2:System.out.println("select a number: "+choice+""+(choice+1)+""+(choice+2));

           number=sc.nextInt();

           System.out.println("select the color from red,blue,green,yellow");

           System.out.println("Enter 1 for RED 2 for BLUE 3 for GREEN 4 for YELLOW");

           color=sc.nextInt();

           break;
         
           case 3:System.out.println("select a number: "+choice+""+(choice+1)+""+(choice+2));

           number=sc.nextInt();

           System.out.println("select the color from red,blue,green,yellow");

           System.out.println("Enter 1 for RED 2 for BLUE 3 for GREEN 4 for YELLOW");

           color=sc.nextInt();

           break;
         
           case 4:System.out.println("select a number: "+choice+""+(choice+1)+""+(choice+2));

           number=sc.nextInt();

           System.out.println("select the color from red,blue,green,yellow");

           System.out.println("Enter 1 for RED 2 for BLUE 3 for GREEN 4 for YELLOW");

           color=sc.nextInt();

           break;
         
         

            

                      

            

           }

           //start value of number is 0 and color is 1


           switch((number+1)*color)

           {

           case 1: System.out.println("Going to be a great for you!");

                    break;

           case 2:System.out.println("You will hear good news today!");

                 break;

           case 3:System.out.println("You will make a new friend today");

                 break;

           case 4:System.out.println("Today will not be a good day for you");

                 break;

           case 5:System.out.println("You'll hear from an old friend");

                 break;

               

            

            

           }

           }

  

   }

Explanation / Answer

import java.io.*; import java.util.*; import javafoundations.*; //import javafoundations.exceptions.*; public class CootieCatcher { private LinkedList numbers; //will be parsed to Integers later private LinkedList colors; private LinkedList fortunes; private AdjMatGraph optiongraph; private LinkedQueue actionqueue; private final String[] switchsteps = {"Open Vert", "Close Vert", "Open Horz", "Close Horz"}; //in the gui, this needs to be a list that somehow contains or references the different gif files /* ****** * CootieCatcher constructor. * Creates an empty CootieCatcher. * Must "fill in the values" with setDefault() or setCustomized() before using in CootieCatcherGUI * ******/ public CootieCatcher(){ numbers = new LinkedList(); colors = new LinkedList(); fortunes = new LinkedList(); optiongraph = new AdjMatGraph(); actionqueue = new LinkedQueue(); } /* ****** * sets CootieCatcher values to default values * - 4 numbers (1 through 4) * - 8 colors (Red, Green, Black, Purple, Blue, Yellow, Orange, White) * - 24 possible outcomes (8 Fortune objects, each with 3 outcomes) * * While real-life fortune teller operates on mod 2, this fortune teller operates on mod 4, i.e., * switching the fortune teller 4 times does not reveal the same results as switching 2 times. * * This is the constructor that will be used if the player selects "Start" in the opening screen * of the GUI application. * ******/ public void setDefault(){ for(int i = 0; i < 4; i++){ actionqueue.enqueue(switchsteps[i]); } numbers.add("1"); numbers.add("2"); numbers.add("3"); numbers.add("4"); colors.add("Red"); colors.add("Green"); colors.add("Black"); colors.add("Purple"); colors.add("Blue"); colors.add("Yellow"); colors.add("Orange"); colors.add("White"); try{ Scanner scan = new Scanner(new File("defaultfortunes.txt")); fortunes.add(new Fortune(Element.FIRE, scan.nextLine(), scan.nextLine(), scan.nextLine())); fortunes.add(new Fortune(Element.EARTH, scan.nextLine(), scan.nextLine(), scan.nextLine())); fortunes.add(new Fortune(Element.AIR, scan.nextLine(), scan.nextLine(), scan.nextLine())); fortunes.add(new Fortune(Element.WATER, scan.nextLine(), scan.nextLine(), scan.nextLine())); fortunes.add(new Fortune(Element.WATER, scan.nextLine(), scan.nextLine(), scan.nextLine())); fortunes.add(new Fortune(Element.EARTH, scan.nextLine(), scan.nextLine(), scan.nextLine())); fortunes.add(new Fortune(Element.FIRE, scan.nextLine(), scan.nextLine(), scan.nextLine())); fortunes.add(new Fortune(Element.AIR, scan.nextLine(), scan.nextLine(), scan.nextLine())); } catch (IOException e) { System.out.println("Default fortunes cannot be read."); } for(int i = 0; i