-2

The current issue that the user is supposed to choose if they want to do a Madlib then would be asked to choose a number for a lib, put inputs for the mad lib and output the final lib and ask if the user would like to write some more in a loop. Now the issue is that it will not loop.

Some websites used: https://www.geeksforgeeks.org/nested-if-in-java/

https://jenkov.com/tutorials/java/while.html

https://github.com/blondiebytes/30-Days-of-Code/blob/master/Video%20Series%20Code/Day%206/MadLibs/src/madlibs/MadLibs.java

Expected result: Would you like to write some Madlibs? (y/n) y

Great! Pick a number for a story! 1/2/3 1

Enter a noun: (Noun)

Enter a living noun or name: (Noun or Name)

Enter a place: (Place)

here is your story! [ A , (Noun or Name) , jumped over , (Noun) , and landed in , (Place) ] Would you like to write some Madlibs? (y/n)

Actual result: Would you like to write some Madlibs? (y/n) y

import java.util.ArrayList;
import java.util.Scanner;
public class MadLibs
{
  public static void main(String[] args)
  {
  Scanner sc = new Scanner(System.in);

//Begining Option for the user

   System.out.println("Would you like to write some madlibs? (y/n)"); 
   String userStartStop = sc.nextLine();

  if(userStartStop.equals("y"))
  {
    System.out.println("Great! Pick a number for a story! 1/2/3");
    String StoryChoice = sc.nextLine();

    if(StoryChoice == "1"){

   System.out.print("Enter a noun: ");
   String userInput = sc.nextLine();
   
   System.out.print("Enter a living noun or name: ");
   String userInput1 = sc.nextLine();
   
   System.out.print("Enter a place: ");
   String userInput2 = sc.nextLine();

/*
This stores the users' input to be put into an array and will be reused for the other stories
*/

    String str1 = "\na";
   String str11 = "\njumped over";
   String str12 = "\nand landed in ";

// This is the pre-typed text that the user fills in. This will be reused

   ArrayList<String> lib1 = new ArrayList<String>();

   lib1.add(str1);
   lib1.add(userInput1);
   lib1.add(str11);
   lib1.add(userInput);
   lib1.add(str12);
   lib1.add(userInput2);
   System.out.println("here is your story! \n" + lib1);

// The user input is integrated with the ArrayList. This will be reused

    }
else if(StoryChoice.equals("2"))
{

  System.out.print("Enter a noun: ");
   String userInput = sc.nextLine();
   
   System.out.print("Enter a living noun or name: ");
   String userInput1 = sc.nextLine();
   
   System.out.print("Enter a place: ");
   String userInput2 = sc.nextLine();

//same as before this will take user input

   String str2 = "\nOnce there was a boy who loved";
   String str21 = "\nand take it to the";
   String str22 = "\n";

//pre-typed text for user

   ArrayList<String> lib2 = new ArrayList<String>();
   lib2.add(str2);
   lib2.add(userInput1);
   lib2.add(str21);
   lib2.add(userInput);
   lib2.add(str22);
   lib2.add(userInput2);
   System.out.println("here is your story! \n" + lib2);

//final story printed here
 
}
else if(StoryChoice.equals ("3"));{

  System.out.print("Enter a noun: ");
   String userInput = sc.nextLine();
   
   System.out.print("Enter a living noun or name: ");
   String userInput1 = sc.nextLine();
   
   System.out.print("Enter a place: ");
   String userInput2 = sc.nextLine();

String str3 = "\nAt the battle of the";
   String str31 = "\n our commander";
   String str32 = "\nlifted our spirits with his trusty college the";

   ArrayList<String> lib3 = new ArrayList<String>();
   lib3.add(str3);
   lib3.add(userInput2);
   lib3.add(str31);
   lib3.add(userInput1);
   lib3.add(str32);
   lib3.add(userInput);
   System.out.println("here is your story! \n" + lib3);

}
}while (!userStartStop.equals("n"));

  }
}
Ronin
  • 1
  • 1
  • Don't compare Strings using `==` or `!=`. Use the `equals(...)` or the `equalsIgnoreCase(...)` method instead. Understand that `==` checks if the two *object references* are the same which is not what you're interested in. The methods on the other hand check if the two Strings have the same characters in the same order, and that's what matters here. – Hovercraft Full Of Eels May 16 '22 at 00:24
  • So, not `if(userStartStop == "y")` but rather, `if (userStartStop.equalsIgnoreCase("y"))` – Hovercraft Full Of Eels May 16 '22 at 00:24

0 Answers0