update
This commit is contained in:
parent
1f1ad49427
commit
6f050a029e
2496 changed files with 37609 additions and 3031 deletions
69
Task/Mad-Libs/Java/mad-libs.java
Normal file
69
Task/Mad-Libs/Java/mad-libs.java
Normal file
|
|
@ -0,0 +1,69 @@
|
|||
import java.util.Map;
|
||||
import java.util.HashMap;
|
||||
import java.util.Scanner;
|
||||
import java.util.StringTokenizer;
|
||||
|
||||
public class MadLibs
|
||||
{
|
||||
public static void main(String[] args)
|
||||
{
|
||||
Scanner s=new Scanner(System.in);
|
||||
String line;
|
||||
StringBuffer storybuffer=new StringBuffer();
|
||||
|
||||
//Accept lines until empty line is entered
|
||||
while(!(line=s.nextLine()).isEmpty())
|
||||
storybuffer.append(" "+line);
|
||||
|
||||
//Remove first space
|
||||
storybuffer.delete(0, 1);
|
||||
String story=storybuffer.toString();
|
||||
//Split
|
||||
StringTokenizer str=new StringTokenizer(story);
|
||||
String word;
|
||||
StringBuffer finalstory=new StringBuffer();
|
||||
|
||||
//Store added elements
|
||||
Map<String,String> hash=new HashMap<String,String>();
|
||||
while(str.hasMoreTokens())
|
||||
{
|
||||
word=str.nextToken();
|
||||
if(word.contains("<"))
|
||||
{
|
||||
String add="";
|
||||
//Element prompt could be more than one word
|
||||
if(!word.contains(">"))
|
||||
{
|
||||
//Build multi-word prompt
|
||||
String phrase="";
|
||||
do{
|
||||
phrase+=word+" ";
|
||||
}while(!(word=str.nextToken()).contains(">"));
|
||||
word=phrase+word;
|
||||
}
|
||||
//Account for element placeholder being immediately followed by . or , or whatever.
|
||||
if(word.charAt(word.length()-1)!='>')
|
||||
add=word.substring(word.lastIndexOf('>')+1);
|
||||
|
||||
//Store id of element in hash table
|
||||
String id=word.substring(0,word.lastIndexOf('>')+1);
|
||||
String value;
|
||||
|
||||
if(!hash.containsKey(id))
|
||||
{
|
||||
//New element
|
||||
System.out.println("Enter a "+ id);
|
||||
value=s.nextLine()+add;
|
||||
hash.put(id, value);
|
||||
}
|
||||
//Previously entered element
|
||||
else
|
||||
value=hash.get(id);
|
||||
word=value;
|
||||
}
|
||||
finalstory.append(word+" ");
|
||||
}
|
||||
System.out.println(finalstory.toString());
|
||||
s.close();
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue