How can I ask for more inputs from the user in Java without undoing the work I\'
ID: 3583643 • Letter: H
Question
How can I ask for more inputs from the user in Java without undoing the work I've already done? I'm currently working on a project, but I'm having a little difficulty getting the results to be exactly as I would like. What I would like to do is:
- introduce myself (which I've done)
- ask the user for their name (not done)
- display a list of puppies available (not done)
- ask the user to pick a puppy and display it (not done)
- display a list of quotes about puppies (done)
- ask the user to pick a quote and display it (done)
- display a random quote based on words I've put in an array (done)
- finally display something like "Welcome, (their name)! I hope you like your new puppy (puppy name). I think you'll be an excellent fit." (not done)
I need to make sure I keep this in the four classes/files I have created, but I'm so new to this that I have no idea where to go from here to do what I would like. Here is what I have so far:
VirtualWorld.java
public class VirtualWorld {
public static void main(String[] args)
{
MyClone myclone=new MyClone();
myclone.setFirstName("Name");
myclone.setLastName("Here");
myclone.introduction();
Puppy myDog=new Puppy(2,"Dog","Tom");
myDog.setPuppyTag(1);
myDog.setPuppyType("Dog");
myDog.setPuppyName("Stuart");
System.out.println("PUPPY ID:"+myDog.getPuppyTag());
System.out.println("PUPPY NAME:"+ myDog.getPuppyName());
MyShoutBox bx=new MyShoutBox();
System.out.println("The saying is: "
+ bx.shoutOutCannedMessage());
System.out.println("Your random message is: "
+ bx.shoutOutRandomMessage());
}
}
MyClone.java
public class MyClone {
private String firstName;
private String lastName;
// our first and last names will be set to private Strings
// we will need to access them through additional means
public String getFirstName()
{ //how we get firstName
return firstName; // returns firstName
}
public String getLastName()
{ //how we get lastName
return lastName; //returns lastName to system
}
public void setFirstName(String newFirstName) { //how we will set first name
firstName = newFirstName;
}
public void setLastName(String newLastName)
{ //how we will set last name
lastName = newLastName;
}
public void introduction() { // our introduction
System.out.println("Hello there! My name is "+firstName+" "+lastName+", and this is my home.");
System.out.println("Pull up a seat, and pick a puppy to be your new best friend.");
}
}
MyShoutBox.java
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Random;
import java.util.Scanner;
public class MyShoutBox
{
private static Map<Integer, String> getCannedMessagesMap() {
//introduces the hm of our set phrases for user selection
Map<Integer, String> cannedMessages = new LinkedHashMap<>();
cannedMessages.put(1, "Lately, my dog's the only one around that listens to my problems.");
cannedMessages.put(2,
"Who let the dogs out?");
cannedMessages.put(3, "Dogs are man's best friends.");
cannedMessages.put(4,
"No one will love you are strongly or purely as a dog.");
cannedMessages.put(5, "Love is a puppy licking your face, even after you left him alone all day.");
cannedMessages.put(6, "A puppy is just a part of your life; you are their entire life.");
cannedMessages.put(7,
"Knick-knack, paddy whack, give the dog a bone.");
cannedMessages.put(8,
"Bow-wow.");
cannedMessages.put(9, "What is it, Lassie? Is little Timmy stuck in the well?");
cannedMessages.put(10, "I didn't cry when Old Yeller died, at least not in front of my friends.");
return cannedMessages;
}
public static String shoutOutCannedMessage() {
int id;
String message = "";
// this allows for the iteration over our collection
// allows us to access each element
Iterator<Entry<Integer, String>> msgIterator = getCannedMessagesMap().entrySet().iterator(); // utilize integer to display string
while (msgIterator.hasNext())
{ // as long as there is another in the collection
//this will continue
Map.Entry msgPair = (Map.Entry) msgIterator.next();
System.out.println("[" + msgPair.getKey() + "] "
+ msgPair.getValue());
//this puts [] around each of our numbers
}// will then end
try
{ //setting up a try/catch to handle exceptions
System.out.print("Please select the number of your favorite message: ");
// prints out the prompt for input to select
Scanner sc = new Scanner(System.in); //input for selection
System.out.println(" ");
id = sc.nextInt(); //input will be an Int
message = getCannedMessagesMap().get(id);
//this directly relates our choice to our above sentences
}
catch (Exception e)
{
// if we deviate from the expected input, we get this
System.out.println("You missed the mark! Oops! Your error is: " + e.getMessage());
}
return message; // returns the selection
}
public static String shoutOutRandomMessage() {
int i;
// This sets our words for potential random selection
String[] subject = { "You", "Someone", "A relative", "A friend", "A stranger" };
String[] verb = { " is", " was", " will be", " might be", " won't be", " can't be" };
String[] adjective = { " rich", " successful", " educated", " friendly", " handsome" };
String[] object = { " with some help", " with a wife", " with his son", " with a friend", " with money" };
String[] adverb = { " in the end. ", " today. ", " someday. ",
" in some years. ", " tomorrow." };
Random k = new Random(); // this initializes Random
int chosenMessage = k.nextInt(subject.length);
String randomMessage = "";
for (i = 1; i <= 1; i++)
// our for loops which will allow for us to iterate
//through the potential words rather than staying stagnant
{
//sets randomMessage so it will iterate through the length of each list of words
// random will allow for this selection to be random
randomMessage = subject[k.nextInt(subject.length)]
+ verb[k.nextInt(verb.length)]
+ adjective[k.nextInt(adjective.length)]
+ object[k.nextInt(object.length)]
+ adverb[k.nextInt(adverb.length)];
}
return randomMessage;
}
public static void main(String[] args) {
System.out.println("Your selected doggy saying is: "
+ shoutOutCannedMessage()); // displays our selection
System.out.println("Your random message is: "
+ shoutOutRandomMessage()); // displays each word at random
//one from each list
}
}
Puppy.java
public class Puppy
{
private int puppyTag; //sets puppyTag to private int value
private String puppyType; // puppyType will be private String
private String puppyName; // puppyName will be private string
public Puppy() {
this.puppyTag = 1; //sets puppyTag of first dog
this.puppyType= "Dachshund"; //sets puppyType of first dog
this.puppyName = "Slinky"; //sets puppyName of first dog
}
public Puppy(int puppyTag, String puppyName, String puppyType)
{
//^ sets type and order when utilizing Puppy()
super(); // constructor allowing us to take arguments from DogTypes subclass
this.puppyTag = puppyTag;
this.puppyType = puppyType;
this.puppyName = puppyName;
}
public int getPuppyTag() //getPuppyTag will be a public int
{
return puppyTag; //returns puppyTag in int format
}
public void setPuppyTag(int puppyTag) //sets puppyTag to int
{
this.puppyTag = puppyTag;
}
public String getPuppyType() //gets puppyType
{
return puppyType;
}
public void setPuppyType(String puppyType) //sets puppyType to String type
{
this.puppyType = puppyType;
}
public String getPuppyName() //gets puppyName variable
{
return puppyName;
}
public void setPuppyName(String puppyName) //setter for String puppyName
{
this.puppyName = puppyName;
}
public void displayPuppyInfo()
{
System.out.println("Puppy Tag#: " + puppyTag);
System.out.println("Puppy Name: " + puppyName);
}
}
I really appreciate any help I can get with this! Thank you!
Explanation / Answer
1. Asking the user for their name:
System.out.println("Enter your name: ");
Scanner scan = new Scanner(System.in);
String username = scan.nextLine();
2. List of puppies:
for ( int i = 0; i < puppies.length; i++)
{
System.out.println(puppies[i]);
]
3. Ask the user to pick a puppy:
System.out.println("Select puppy number: ");
Scanner scan2 = new Scanner(System.in);
int index = scan.nextInt();
System.out.println("The puppy selected by the user is: ");
System.out.println(puppies[i]);
4. System.out.println("Welcome, (their name)! I hope you like your new puppy (puppy name). I think you'll be an excellent fit");
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.