java restaurant address displays incorrectly - java

I have an assignment that asks us to input our favorite restaurant and address. I cannot get the address to print out right on the same line.
import java.util.Scanner;
public class Restaurant {
public static void main(String[] args) {
Scanner user_input = new Scanner(System.in);
String name;
System.out.print("Enter your Favorite Restaurant: ");
name = user_input.next();
String street;
System.out.print("Enter the Street Address: ");
street = user_input.next();
String city;
System.out.print("Enter the City: ");
city = user_input.next();
String state;
System.out.print("Enter the State: ");
state = user_input.next();
String zip;
System.out.print("Enter the Zip Code: ");
zip = user_input.next();
String restaurant = name + "\n" + city + ", " + state + ", " + " " " + zip;"
+ "";
}
}
I get an output that does not let me input the whole address properly. There must be an easier way to do this.
Enter your Favorite Restaurant: Uncle Bubs
Enter the Street Address: Enter the City: 444 Spender St
Enter the State: Enter the Zip Code: BUILD SUCCESSFUL (total time: 19 seconds)

Scanner.next is used for one word without any spaces.
Any words after that will be saved for the next call to Scanner.next.
Instead, use Scanner.nextLine:
street = scanner.nextLine();

Related

How to make "un-entered" user input show "not available" -- Java

I am just a beginner, so any help would be appreciated.
So here is my code:
import java.util.Scanner;
public class AccountScanner {
public static void main (String[] args) {
String name, course;
int age;
Scanner input = new Scanner(System.in);
System.out.print("Enter name: ");
name = input.nextLine();
System.out.print("Enter age: ");
age = input.nextInt();
System.out.print("Enter Course: ");
course = input.next();
System.out.println();
System.out.println("Name:" +name);
System.out.println("Age:" +age);
System.out.println("Course:" +course);
System.exit(0);
}
}
but what if the user did enter one of the details. What can I do to make it display "Not Available" instead
example:
enter name: Zack
enter age:
enter course: BSCS
output would be:
Name: Zack
Age: Not available
Course: BSCS
You can use a conditional operator to check if the inputs are empty and show the result correspondingly as follows:
String name, course, age;
Scanner input = new Scanner(System.in);
System.out.print("Enter name: ");
name = input.nextLine();
System.out.print("Enter age: ");
age = input.nextLine();
System.out.print("Enter Course: ");
course = input.nextLine();
System.out.println();
System.out.println("Name:" +(name.isEmpty()?"Not Available":name));
System.out.println("Age:" +(age.isEmpty()?"Not Available":new Integer(age)));
System.out.println("Course:" +(course.isEmpty()?"Not Available":course));
System.exit(0);

how can i call a variable from another function/method?

I have an assignment to do a CV that users will input on and display it. However, I don't know how I can call a variable to another to function to print/display.
Here is the code:
import java.util.Scanner;
public class curriculumVitae1{
public static String firstName;
public static String middleName, lastName, birthDate, maritalStatus, homeAddress, provincialAddress, mobileNumber, anotherMobile, landlineNumber, anotherLandline, primaryYears;
private static void main (String args[]){
Scanner input = new Scanner(System.in);
System.out.print("\nCurriculum Vitae");
System.out.print("\nInput your last name: ");
String lastName;
lastName = input.nextLine();
System.out.print("\nInput your first name: ");
String firstName;
firstName = input.nextLine();
System.out.print("\nInput your middle name: ");
String middleName;
middleName = input.nextLine();
System.out.print("\nInput your birthdate: ");
String birthDate;
birthDate = input.nextLine();
System.out.print("\nInput your marital status (Married, Widowed, Separated, Divorced, Single) : ");
String maritalStatus;
maritalStatus = input.nextLine();
System.out.print("\nInput your home address: ");
String homeAddress;
homeAddress = input.nextLine();
curriculumVitae1.cv();
}
private static void provincial(String args[]){
Scanner input = new Scanner(System.in);
System.out.print("\nDo you have a provincial address? Enter Y if yes, and N if no: ");
char provincialQuestion;
provincialQuestion = input.nextLine().charAt(0);
if (provincialQuestion=='Y'){
System.out.print("\nInput your provincial address: ");
String provincialAddress;
provincialAddress = input.nextLine();
}
else if(provincialQuestion=='N'){
}
}
private static void mobile(String args[]){
Scanner input = new Scanner(System.in);
System.out.print("\nContact Details ");
System.out.print("\nInput your mobile number: ");
String mobileNumber;
mobileNumber = input.nextLine();
System.out.print("\nDo you have another mobile number? Enter Y if yes, and N if no: ");
char mobileQuestion;
mobileQuestion = input.nextLine().charAt(0);
if (mobileQuestion=='Y'){
System.out.print("\nInput another mobile number: ");
String anotherMobile;
anotherMobile = input.nextLine();
}
else if(mobileQuestion=='N'){
}
}
private static void landline(String args[]){
Scanner input = new Scanner(System.in);
System.out.print("\nInput your landline number: ");
String landlineNumber;
landlineNumber = input.nextLine();
System.out.print("\nDo you have another landline number? Enter Y if yes, and N if no: ");
char landlineQuestion;
landlineQuestion = input.nextLine().charAt(0);
if (landlineQuestion=='Y'){
System.out.print("\nInput another mobile number: ");
String anotherLandline;
anotherLandline = input.nextLine();
}
else if (landlineQuestion=='N'){
}
}
private static String email(){
Scanner input = new Scanner(System.in);
System.out.print("\nInput your email address: ");
String emailAddress;
emailAddress = input.nextLine();
return emailAddress;
}
private static String tertiary(){
Scanner input = new Scanner(System.in);
System.out.print("\nEducation History ");
System.out.print("\nTertiary Education ");
System.out.print("\nInput your tertiary education course: ");
String tertiaryCourse;
tertiaryCourse = input.nextLine();
System.out.print("\nInput your tertiary education school: ");
String tertiarySchool;
tertiarySchool = input.nextLine();
System.out.print("\nInput your tertiary education inclusive years (xxxx-xxxx): ");
String tertiaryYears;
tertiaryYears = input.nextLine();
System.out.print("\nDo you have any honors/achivements received during your tertiary education? Enter Y if yes, and N if no: ");
char tertiaryQuestion;
tertiaryQuestion = input.nextLine().charAt(0);
if (tertiaryQuestion=='Y'){
System.out.print("\nInput your honor/s or achivement/s:");
String tertiaryAchievements;
tertiaryAchievements = input.nextLine();
return tertiaryAchievements;
}
else if (tertiaryQuestion=='N'){
return "------";
}
}
private static void secondary(String args[]){
Scanner input = new Scanner(System.in);
System.out.print("\nSecondary Education ");
System.out.print("\nInput your secondary education school: ");
String secondarySchool;
secondarySchool = input.nextLine();
System.out.print("\nInput your secondary education inclusive years (xxxx-xxxx): ");
String secondaryYears;
secondaryYears = input.nextLine();
System.out.print("\nDo you have any honors/achivements received during your secondary education? Enter Y if yes, and N if no: ");
char secondaryQuestion;
secondaryQuestion = input.nextLine().charAt(0);
if (secondaryQuestion=='Y'){
System.out.print("\nInput your honor/s or achivement/s:");
String secondaryAchievements;
secondaryAchievements = input.nextLine();
}
else if (secondaryQuestion=='N'){
}
}
public static void primary(String args[]){
Scanner input = new Scanner(System.in);
System.out.print("\nPrimary Education ");
System.out.print("\nInput your primary education school: ");
String primarySchool;
primarySchool = input.nextLine();
System.out.print("\nInput your primary education inclusive years (xxxx-xxxx): ");
String primaryYears;
primaryYears = input.nextLine();
System.out.print("\nDo you have any honors/achivements received during your primary education? Enter Y if yes, and N if no: ");
char primaryQuestion;
primaryQuestion = input.nextLine().charAt(0);
if (primaryQuestion=='Y'){
System.out.print("\nInput your honor/s or achivement/s:");
String primaryAchievements;
primaryAchievements = input.nextLine();
}
else{
System.out.print("------");
}
}
public static void cv(String args[]){
System.out.println(" Curriculum Vitae");
System.out.print("\nName:" + firstName + " " + middleName + " "+ lastName);
System.out.print("\nBirthdate:" + birthDate);
System.out.print("\nMarital Status:" + maritalStatus);
System.out.print("\nHome Address:" + homeAddress);
System.out.print("\nProvincial Address:" + provincialAddress);
System.out.print("\nMobile Number:" + mobileNumber );
System.out.print("\nAnother Mobile Number:" + anotherMobile);
System.out.print("\nLandline:" + landlineNumber);
System.out.print("\nYear: " + primaryYears);
}
}
However, I always get the error that
C:\Users\BEST\Desktop\wew>javac curriculumVitae1.java
curriculumVitae1.java:33: error: method cv in class curriculumVitae1 cannot be applied to given types;
curriculumVitae1.cv();
^
required: String[]
found: no arguments
reason: actual and formal argument lists differ in length
1 error
Please help me on how can I print out another variable from other function. Or some alternatives that I can do.
Your methods expect an (String[] args) however since you don't use them I would remove them. Try
public static void cv() {
The error details highlight that you're calling the method without providing the required parameters in the method signature public static void cv(String args[]):
The required part tells you what types of arguments are expected, here String[] and the found part tells you what it saw instead, here it saw you passed no arguments.
The reason tells you that the actual (what you provided) number of arguments differs from the formal (what the method signature defines) number of arguments expected, i.e. not enough or too many arguments were provided.
You can also get this from the original error message:
error: method cv in class curriculumVitae1 cannot be applied to given types;
curriculumVitae1.cv();
It doesn't explicitly state it, but from the line of code shown below you can see that the "given types" are nothing because the method was called with no arguments—nothing inside the parentheses.
Like Peter Lawrey said, you can just remove the String args[] from your method signature since you don't use it.
Hope this helps you understand error messages and what they're telling you a little better!
Make the following changes to your program:
Change the access specifier of main() method from private to public. Otherwise the code will throw the error - Does not contain a main method
Since you have firstName, middleName, lastName, birthDate etc. declared as static variables do not declare them as local variables in main method. Assign the values to the already declared static variables in the main() method as shown below:
public static void main (String args[]){
Scanner input = new Scanner(System.in);
System.out.print("\nCurriculum Vitae");
System.out.print("\nInput your last name: ");
//String lastName;
lastName = input.nextLine();
System.out.print("\nInput your first name: ");
//String firstName;
firstName = input.nextLine();
System.out.print("\nInput your middle name: ");
//String middleName;
middleName = input.nextLine();
System.out.print("\nInput your birthdate: ");
//String birthDate;
birthDate = input.nextLine();
System.out.print("\nInput your marital status (Married, Widowed, Separated,
Divorced, Single) : ");
//String maritalStatus;
maritalStatus = input.nextLine();
System.out.print("\nInput your home address: ");
//String homeAddress;
homeAddress = input.nextLine();
//System.out.println();
cv();
}
Remove the String[] args argument from cv() method as it is not being used.
Since cv() is a static method, it can be called directly from the main().
Return the string type varaible from tertiary() method as it is giving a compile error. You can do so by declaring tertiaryAchievements variable outside the if-else block and then returning it as shown below:
String tertiaryAchievements="";
if (tertiaryQuestion=='Y')

Scanner is not taking input of another string and skipping it

Why scanner is not taking input of another string and skepping it? I cant understand, here is my code:
import java.util.Scanner;
public class demo {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
String name;
String address;
int age;
System.out.println("Enter your name");
name = s.nextLine();
System.out.println("My name is :" + name);
System.out.println("Enter your age");
age = s.nextInt();
System.out.println("My age is :" + age);
System.out.println("Enter your address");
address = s.nextLine();
System.out.println("My address is :" + address);
}
}
Output :
Enter your namedkMy name is :dkEnter your age22My age is :22Enter your addressMy address is :
It is simple. You can use s.nextLine(); after the age = s.nextInt();
Scanner provides a method nextInt() to request user input and does not consume the last newline character (\n).
System.out.println("Enter your age");
age = s.nextInt();
s.nextLine(); // consumes the \n character
System.out.println("My age is :" + age);
You should close the scanner after the last system out
Place below statement after Statement.out.println("My age is",age);
s.nextLine();
Above statement will do flush, which will clear the cache location in order to accept new input.

Im having an issue entering a last name to an input

I am writing a program to print out the name of a student and their grade. It works if I only enter their first name and I get an Error when I enter their last. I am wondering if I have overlooked something with the String name.
package project_2;
import java.util.Scanner;
public class Project_2 {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
System.out.print("Enter your name: ");
String name = input.next();
System.out.print("Enter Grade: \t");
double Grade = input.nextDouble();
if(Grade>=88)
System.out.println(name + "You have an A!");
else if (Grade<=87 && Grade>=80)
System.out.println(name + "You have a B!");
else if (Grade<=79 && Grade>=67)
System.out.println(name + "You have a C!");
else if (Grade<=66 && Grade>=60)
System.out.println(name + "You have a D!");
else
System.out.println(name + "You have a F!");
}
}
Just do this:
Scanner input = new Scanner(System.in);
System.out.print("Enter your name: ");
String name = input.nextLine();
System.out.print("Enter Grade: \t");
Then you can take the full name.
Output:
Enter your name: Jack Davidson
Enter Grade: 88
Please note that, Using next() will only return what comes before a space. nextLine() automatically moves the scanner down after returning the current line.

having trouble compling this code for a starwars name generator

I need to use first three letters of actual first name + first two letters of actual last name, and first two letters of mother's maiden name + first three letters of birth city. I also need to have the first letter be capitalized. Using toUpperCase() and toLowerCase(). Thanks!
import java.util.Scanner;
public class Assignment2
{
public static void main(String[] args)
{
System.out.printf("Enter your first name: ");
/* This should be string as your gettting name*/
String firstname = input.nextLine();
firstname = firstname.substring(0, 1).toUpperCase() + firstname.substring(1);
/* Don't need new variable use same and that also should be string. */
System.out.printf("Enter your last name: ");
String lastname = input.nextLine();
lastname = lastname.substring(0,2). toUpperCase() + lastname.substring(1);
System.out.printf("Enter your mother's maiden name: ");
String mothersname = input.nextLine();
mothersname = mothersname.substring(0,2);
System.out.printf("Enter the name of the city in which you were born: ");
String cityname = input.nextLine();
cityname = cityname.substring(0,3);
String StarWarsName = firstname+lastname+mothersname+cityname;
System.out.println("May the force be with you, " + StarWarsName );
}
}
//* Updated code
import java.util.Locale;
import java.util.Scanner;
public class Assignment2
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.printf("Enter your first name: ");
String firstname = input.nextLine();
firstname = firstname.substring(0, 1).toUpperCase() + firstname.substring(1).toLowerCase();
System.out.printf("Enter your last name: ");
String lastname = input.nextLine();
lastname = lastname.substring(0,1). toUpperCase() + lastname.substring(1).toLowerCase();
System.out.printf("Enter your mother's maiden name: ");
String mothersname = input.nextLine();
mothersname = mothersname.substring(0,2);
System.out.printf("Enter the name of the city in which you were born: ");
String cityname = input.nextLine();
cityname = cityname.substring(0,3);
String StarWarsName = firstname+lastname+" "+mothersname+cityname;
System.out.println("May the force be with you, " + StarWarsName );
}
}
Corrected the code and explained the changes in comments. Go through comments also. Don't just copy this code.
import java.util.Scanner;
public class test1
{
public static void main(String[] args)
{
Scanner input = new Scanner();
System.out.printf("Enter your first name: ");
/* This should be string as your gettting name*/
String firstname = input.nextLine();
/* Don't need new variable use same and that also should be string. */
firstname = firstname.substring(0,1);
System.out.printf("Enter your last name: ");
String lastname = input.nextLine();
lastname = lastname.substring(0,2);
System.out.printf("Enter your mother's maiden name: ");
String mothersname = input.nextLine();
mothersname = mothersname.substring(0,1);
System.out.printf("Enter the name of the city in which you were born: ");
String cityname = input.nextLine();
cityname = cityname.substring(0,2);
String StarWarsName = ( "firstname" + "lastname " + "mothersname " + "cityname");
System.out.println("May the force be with you, " + StarWarsName );
}
}
firstname cannot be resolved to a variable
You didn't declare firstname. Not only firstname you didn't declare lastname, mothersname, cityname. So you should declare all. Those should be String.
You didn't create Scanner object. Create Scanner class object.
Scanner input = new Scanner(System.in);
Next change. You declared cityname as two times and as inttype. But nextLine() returns String not int.
String cityname = input.nextLine();//returns String, not int
cityname = city.substring(0,3);//returns the first 3 characters as String, not int
String#substring() returns String not int. So check the entire code.
Change
String StarWarsName = ( "firstname" + "lastname " + "mothersname " + "cityname");
to
String StarWarsName = firstname+lastname+" "+mothersname+cityname;
All these are variables not values. So don't put in double quotes.
Edit: Capitalize the first letter of firstname
firstname = firstname.substring(0, 1).toUpperCase() + firstname.substring(1).toLowerCase();

Categories