0% found this document useful (0 votes)
17 views3 pages

MadLibs Game Implementation in Java

The document is a Java program for a MadLibs game where users provide words and phrases to fill in a story. It includes functionalities to create a mad-lib from an input file and view the mad-lib. The program utilizes a menu system for user interaction and handles file input/output operations.

Uploaded by

navaneevenu
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views3 pages

MadLibs Game Implementation in Java

The document is a Java program for a MadLibs game where users provide words and phrases to fill in a story. It includes functionalities to create a mad-lib from an input file and view the mad-lib. The program utilizes a menu system for user interaction and handles file input/output operations.

Uploaded by

navaneevenu
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

package madLibs;

import [Link].*;
import [Link].*;

public class MadLibs {

public static void main(String[] args)throws FileNotFoundException {


// TODO Auto-generated method stub
boolean playGame = true;
Scanner console = new Scanner([Link]);

[Link]("Welcome to the game of MadLibs.");


[Link]("I will ask you to provide various words ");
[Link]("and phrases to fill a story.");
[Link]("The result will be written to an output file.");

while(playGame == true){
playGame = menu(console);
}

[Link]("Thanks for playing!");

public static boolean menu(Scanner console) throws FileNotFoundException{

[Link]("(C)reate mad-lib, (V)iew mad-lib, (Q)uit?");


String answer = [Link]();
if([Link](0) == 'c' || [Link](0) == 'C'){
create(console);
return true;
}
if([Link](0) == 'v' || [Link](0) == 'V' ){
view(console);
return true;
}
if([Link](0) == 'q' || [Link](0) == 'Q'){
return false;
}
else{
[Link]("Invalid answer. Please try again.");
return true;
}
}
public static void create(Scanner console)throws FileNotFoundException{

[Link]("Input file name: ");


String nameIn = [Link]();
File f1 = new File(nameIn);
while(![Link]()){
[Link]("File not found. Try again: ");
nameIn = [Link]();
//f1 = new File(nameIn);
}
[Link]("Output file name: ");
String nameOut = [Link]();
File out = new File(nameOut);
PrintStream output = new PrintStream(out);
Scanner input = new Scanner(f1);

while([Link]()){
String text = [Link]();

//[Link](text);
//processLine(console, f1, nameOut);
if([Link]("<") && [Link](">")){
//String x = [Link]();
char a = [Link](1);
String anora = aOrAn(a);
text = [Link]('<', ' ');
text = [Link]('>', ' ');
text = [Link]('-', ' ');
[Link]("Please type" + anora + text);
String in = [Link]();
[Link](" " + in + " ");
}
else{
[Link](" " + text + " ");
}
}

public static void view(Scanner console)throws FileNotFoundException{


Scanner input = new Scanner(new File("[Link]"));
while([Link]()){
String text = [Link]();
//[Link](text);
//processLine(text);

}
[Link]();
}
/*public static void processLine(Scanner console, File inName, String
outName)
throws FileNotFoundException{
Scanner madL = new Scanner(new File([Link]()));
//int length = [Link]().length();
PrintStream output = new PrintStream(new File(outName));
String tx = [Link]();
[Link](tx);

if([Link]().startsWith("<") && [Link]().endsWith(">")){


String x = [Link]();
char a = [Link](1);
String anora = aOrAn(a);
[Link]('<', ' ');
[Link]('>', ' ');
[Link]('-', ' ');
[Link]("Please type" + anora + x);
String in = [Link]();
[Link](" " + in + " ");

}
else{
String y = [Link]();
[Link](" " + y + " ");
}

}*/

public static String aOrAn(char check){


String a;
if(check == 'a' || check == 'A' || check == 'i' || check == 'I'
|| check == 'i' || check == 'e' || check == 'E' ||
check == 'o' || check == 'O' || check == 'u' || check == 'U'){
a = " an";
}
else{
a = " a";
}
return a;
}

Common questions

Powered by AI

The commented-out 'processLine' method suggests potential code refactoring for more modular design by separating line-by-line processing logic from main creation logic. Its implementation could improve maintainability by allowing easier updates and isolating changes to specific functionalities, enhancing code readability and reducing duplication .

The MadLibs program prompts the user to re-enter their choice if they provide an invalid menu selection. This approach ensures the user remains within the menu loop until a valid command ('c', 'v', or 'q') is provided, thereby preventing program errors or unintended lapses in execution flow .

The MadLibs program uses the Scanner class to read user inputs from the console and from input files line-by-line. It uses the PrintStream class to direct outputs to an output file, thereby capturing the story created with user inputs. This combination allows dynamic processing of data and output logistics .

Input validation in the 'create' method ensures that the program receives correct and expected inputs, such as file names that exist. Omitting input validation could lead to runtime errors, program crashes, and a negative user experience due to unhandled exceptions, ultimately degrading the reliability and usability of the application .

The initial interaction in the MadLibs program, which includes a welcoming message and a brief explanation of the game mechanics, effectively sets a friendly and instructional tone. This approach not only makes the user comfortable but also provides immediate clarity on how to proceed, thus enhancing the overall user experience .

The program verifies the existence of an input file to prevent FileNotFoundException errors and ensure that the program can successfully read from an existing file. This verification is crucial for maintaining program stability and for ensuring that the necessary source material is available to create a MadLib .

The MadLibs program ensures output coherence by replacing placeholders within the input text with user-provided inputs according to a structured template. This is managed through file streams, where placeholders are identified and replaced in sequence, preserving the original format .

The 'playGame' loop allows continuous interaction by repeatedly invoking the menu until the user decides to quit. This design increases user freedom to explore game functionalities and prolongs program engagement by letting users perform multiple actions in a single run session .

The 'menu' method serves as the central navigation point for user interaction within the MadLibs game. It allows users to choose between creating a new MadLib, viewing an existing one, or quitting the game. This method's design ensures orderly execution and consistent control flow through user-directed choices .

The 'aOrAn' function determines whether to use "a" or "an" based on the first letter of the word that follows a placeholder in the MadLibs program. This enhances user interaction by guiding them to provide grammatically correct inputs during the creation process .

You might also like