Making a MadLib in Java - User Input Erroring - java

I am VERY new to Java and I'm trying to make a madlib that takes user input and inserts it into a sentence. So far, I'm not having much luck. When I run the program, it prompts me for the first input, but after that it errors out. Do you know why? I think it has something to do with data types (int, string, etc).
Bonus question: How would I make the inputs appear bold once inserted into the paragraph? Assuming I can get this code to work in the first place.
Here is my code:
import java.util.Scanner;
public class Madlib
{
public static void main(String[] args)
{
int firstName, childAge, colorToy, typeToy;
Scanner input = new Scanner(System.in);
System.out.println("Enter someone's first name:");
firstName = input.nextInt();
System.out.println("Enter a child's age:");
childAge = input.nextInt();
System.out.println("Enter a color:");
colorToy = input.nextInt();
System.out.println("Enter a toy:");
typeToy = input.nextInt();
System.out.println("\"I am" + childAge + "years old, so that means I get to have the" + colorToy + typeToy + "!\" exclaimed the little girl.");
System.out.println("\"Share with your sister,\"" + firstName + "grovelled, barely peering over their large, Sunday newspaper.");
}
}

You have this error because
firstName, childAge, colorToy, typeToy
should all not supposed to be an integer. They are supposed to be String You should be doing
firstName = input.nextLine();
childAge = input.nextLine();
colorToy = input.nextLine();
typeToy = input.nextLine();
Also, this is not part of your question; however, when you do
System.out.println("\"I am " + childAge + " years old, so that means I get to have the " + colorToy + " " + typeToy + "!\" exclaimed the little girl.");
System.out.println("\"Share with your sister,\"" + " " + firstName + " grovelled, barely peering over their large, Sunday newspaper.");
It should look like that with spaces included in places that you didn't have them in. I hope this answered your question!

Related

Reverse a line that has multiple values

I am working on a program that allows the user to input an integer, double, character, and a string. I have used variables to store the numbers in I am using BlueJ as my IDE, and my question is how I can reverse a system.out.println line that has variables in it?
Here is my code below:
import java.util.Scanner;
import java.lang.String;
public class Lab1
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
//Entering a integer
int money = 0;
System.out.println("Enter an integer:");
money = input.nextInt();
//entering a double
double cost = 10;
System.out.println("Enter a double:");
cost = input.nextDouble();
//Entering a character
char a;
System.out.println("Enter a character:");
a = input.next().charAt(0);
//Entering a string
System.out.println("Please enter a string:");
String string = input.next();
System.out.println();
//Single line separated by spaces
int num = money;
double price = cost;
char b = a;
String text = string;
System.out.println("Single line:");
System.out.print(num + " " + price + " " + b + " " + text);
//Values in reverse
System.out.println();
System.out.println();
System.out.println("Values in reverse:");
}
}
Note: I am using BlueJ for this, and I have tried reversing the variables but, I couldn't.
My output:
Enter an integer:
45
Enter a double:
98.32
Enter a character:
a
Please enter a string:
welcome
Single line:
45 98.32 a welcome
The reverse of '45 98.32 a welcome' should be:
Welcome a 98.32 and 45.
Thank you and have a great day.
System.out.println("Values in reverse:");
System.out.print(text + " " + b + " " + price + " " + num)
For this simple question, I thought you need to change the order of output then it would be fine?
Anyway, if this is not your expected output, do tell me so.
In case this is what you are looking for, reverse() method is for the StringBuilder class: String class does not have reverse() method, we need to convert the input string to StringBuilder, which is achieved by using the append method of StringBuilder which meant you can only reverse the string output rather than the println in Java.

what line of code do I use to print out the input a user types in

I am new to coding, and I have been testing my skills with writing a small program to get better. I am trying to take the user input in, and print it back out to them. For example, if someone types their name into the system and the program responds with "there name" and continue saying whatever is next I programmed the program to say. So if anyone sees the problem I am running into and have any helpful input that would be great. Thanks to everyone for the help.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// Scanner
Scanner scanner = new Scanner(System.in);
//Taking input using Scanner
System.out.println("Hello, How may I assist you today?");
String name1 = scanner.nextLine();
System.out.println("where are you diving to today?");
String name2 = scanner.nextLine();
System.out.println("what is your name?");
String name3 = scanner.nextLine();
System.out.println("Ok " + "new Scanner(System.in)");
String name5 = scanner.nextLine();
System.out.println("What time do you want to arrive to your destination?");
String name6 = "new Scanner(System.in)";
System.out.println(" + ", "new Scanner(System.in)" , " + " great, what time are you leaving? ");
String name7 = scanner.nextLine();
System.out.println(" + " "new Scanner(System.in)" ," + ", "Good deal.");
}
}
Use below statement for name1,
System.out.println("your string" + name1);
Similarly for other Strings.
So, your final code will look like
System.out.println("Hello, How may I assist you today?");
String name1 = scanner.nextLine();
System.out.println("user input is: " + name1);
System.out.println("where are you driving to today?");
String name2 = scanner.nextLine();
System.out.println("User input is: " + name2);
System.out.println("I apolgize but I forgot to get your name?");
String name3 = scanner.nextLine();
System.out.println("Thanks" + name3);

Java: only first word of string being output?

I've used the Scanner var = input.nextLine();
It works for my name variable but not address?
Here is my code:
Scanner input = new Scanner(System.in);
System.out.println("What is your name?");
String name = input.nextLine();
System.out.println("How old are you?");
int age = input.nextInt();
System.out.println("Where do you live?");
String address = input.nextLine();
System.out.println("Your name is " + name + ".\n You are " + age + " years old." + "\n You live at " + address + ".");
And here's what it's displaying:
What is your name?
Yassine assim
How old are you?
17
Where do you live?
Your name is Yassine assim.
You are 17 years old.
You live at .
int age = input.nextInt(); consumes the int you typed but that's it, you surely return to line after if BUT this char (return line) has not been consumed
And so input.nextLine(); of adress will do it directly
2 options :
int age = Integer.parseInt(input.nextLine()); take all line and convert to int
int age = input.nextInt(); input.nextLine(); take int and use, then consume return like

how do I make the program take the input before displaying results

I'm new to java and I made this program, the problem I'm asking the user to input their age and name then the program should use the input to print the statement. however the when I type the age then press enter the program executes all the statements without waiting for me to enter my name.. how can I fix this?
Scanner input = new Scanner(System.in);
int age;
String name;
System.out.println("Please enter your age ");
age = input.nextInt();
System.out.println("and now enter your name");
name = input.nextLine();
System.out.println("Your name is " +name + " and you're " +age + "years old");
You can use nextLine() for both:
System.out.println("Please enter your age ");
age = Integer.parseInt(input.nextLine());
System.out.println("and now enter your name");
name = input.nextLine();
The problem is that when you hit Enter after you input the age, the resulting newline is interpreted as the end of the input for the following nextLine().
You could also use next() instead of nextLine(), but then John Doe would be interpreted as just John because next() uses whitespace as a separator.
Use next() instead of nextLine() at name variable.
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int age;
String name;
System.out.println("Please enter your age ");
age = input.nextInt();
System.out.println("and now enter your name");
name = input.next();
System.out.println("Your name is " +name + " and you're " +age + " years old");
}

Need help declaring a variable within a for loop (Java)

I'm currently working on a program and ran into an error while trying to execute a for loop. I want to declare a variable in the for loop, then break once that variable obtains a certain value, but it returns the error "cannot be resolved to a variable."
Here's my code
int i = -1;
for (; i == -1; i = index)
{
Scanner scan = new Scanner(System.in);
System.out.println("Please enter your first and last name");
String name = scan.nextLine();
System.out.println("Please enter the cost of your car,"
+ "\nthe down payment, annual interest rate,"
+ "\nand the number of years the car is being"
+ "\nfinanced, in that order.");
DecimalFormat usd = new DecimalFormat("'$'0.00");
double cost = scan.nextDouble();
double rate = scan.nextDouble();
int years = scan.nextInt();
System.out.println(name + ","
+ "\nyour car costs " + usd.format(cost) + ","
+ "\nwith an interest rate of " + usd.format(rate) + ","
+ "\nand will be financed annually for " + years + " years."
+ "\nIs this correct?");
String input = scan.nextLine();
int index = (input.indexOf('y'));
}
I want to run the output segement of my program until the user inputs yes, then the loop breaks.
The variable index's scope is local to the block of the for loop, but not the for loop itself, so you can't say i = index in your for loop.
You don't need index anyway. Do this:
for (; i == -1;)
or even
while (i == -1)
and at the end...
i = (input.indexOf('y'));
}
Incidentally, I'm not sure you want input.indexOf('y'); an input of "blatherskyte" will trigger this logic, not just "yes", because there's a y in the input.
Instead of using a for loop, you can do-while(it suits much better for this scenario.
boolean exitLoop= true;
do
{
//your code here
exitLoop= input.equalsIgnoreCase("y");
} while(exitLoop);
for indefinite loop, i would prefer while.
boolean isYes = false;
while (!isYes){
Scanner scan = new Scanner(System.in);
System.out.println("Please enter your first and last name");
String name = scan.nextLine();
System.out.println("Please enter the cost of your car,"
+ "\nthe down payment, annual interest rate,"
+ "\nand the number of years the car is being"
+ "\nfinanced, in that order.");
DecimalFormat usd = new DecimalFormat("'$'0.00");
double cost = scan.nextDouble();
double rate = scan.nextDouble();
int years = scan.nextInt();
System.out.println(name + ","
+ "\nyour car costs " + usd.format(cost) + ","
+ "\nwith an interest rate of " + usd.format(rate) + ","
+ "\nand will be financed annually for " + years + " years."
+ "\nIs this correct?");
String input = scan.nextLine();
isYes = input.equalsIgnoreCase("yes");
}
You cannot do this. If the variable is declared inside of the loop, then it is re-created every run. In order to be part of the condition for exiting the loop, it must be declared outside of it.
Alternatively, you could use the break keyworkd to end the loop:
// Should we exit?
if(input.indexOf('y') != -1)
break;
Here you want to use a while loop. Usually you can decide which loop to use by saying your logic out loud to yourself, While this variable is(not) (value) do this.
For your problem, initialize the variable outside of the loop, and then set the value inside.
String userInput = null;
while(!userInput.equals("exit"){
System.out.println("Type exit to quit");
userInput = scan.nextLine();
}

Categories