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();
Related
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')
Why does my code have a runtime error?(java)
import java.util.Scanner;
public class StudentID
{
static int gradeLevel;
static int id;
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
System.out.println("---Student ID---");
System.out.print("Enter your first name: ");
String firstName = keyboard.next();
System.out.print("\nEnter your last name: ");
String lastName = keyboard.next();
System.out.print("\nEnter your grade level: ");
gradeLevel = keyboard.nextInt();
System.out.print("\nEnter your id: ");
id = keyboard.nextInt();
System.out.print("\nThe text for your student id is:");
String result = getIDText(firstName, lastName, gradeLevel, id);
System.out.print(result);
}
public static String getIDText(
String firstName,
String lastName,
int gradeLevel,
int id)
{
String result =
"\n\nName: " + lastName + ", " + firstName +
"\nGrade: " + gradeLevel +
"\nID: " + id;
return result;
}
}
I can enter my my data just fine, but after type in my ID and press enter, my program crashes saying i have an error at id = keyboard.nextInt();
My error is this:
java.util.InputMismatchException at
java.util.Scanner.throwFor(Scanner.java:864) at
java.util.Scanner.next(Scanner.java:1485) at
java.util.Scanner.nextInt(Scanner.java:2117) at
java.util.Scanner.nextInt(Scanner.java:2076) at
StudentID.main(StudentID.java:18)
Code works fine in case you print integer value for ID. If you print String value, like e.g. aaa, you get this java.util.InputMismatchException. If you would like to check incorrect data, you have to read always String an then in the code manually convert it.
P.S. You should close Scanner isntance somehwere: keyboard.close()
There are 2 ways to resolve this:
1. use keyboard.nextLine() instead of keyboard.next if your string input has spaces in between as the input is considered only till it encounters first space.
2. if you want to use keyboard.next() only, the check whether the input is int when you want for e.g:
if(keyboard.hasNextInt())
{
id = keyboard.nextInt();
}
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();
I am asked to take input from a user, their first and last name in one string.
Then output their name, with their last name first then their first name.
I have finished everything but the part I am stuck on is how to show their first name then their last name, please help.
This is my code so far as requested
package chapter2Codes;
import java.util.Scanner;
public class StPrac1 {
public static void main(String[] args) {
String name;
System.out.println("Please your full name");
Scanner kbd = new Scanner( System.in );
name = kbd.nextLine();
System.out.print(name.substring(5,8) + (", ") + name.substring(0,));
}
}
If Firstname and Lastname are seperated by white-space then try this
var fullname = "Firstname Surname";
var fname = fullname.Split(" ")[0];
var sname = fullname.Split(" ")[1];
string output = sname + ", " + fname;
OR if you want to Avoid middle name
string fullName = "Firstname MidName LastName";
string[] names = fullName.Split(' ');
string fname = names.First();
string lname = names.Last();
string output = lname + ", " + fname;
OR use your delimeter in .Split('addyourdelimeterhere')
AS per your Code
public class StPrac1 {
public static void main(String[] args) {
String name;
System.out.println("Please your full name");
Scanner kbd = new Scanner( System.in );
name = kbd.nextLine();
string fname = name.Split(" ")[0];
string sname = name.Split(" ")[1];
string output = sname + ", " + fname;
System.out.print(output);
}
}
This is just an example not a perfect solution, It will be easy to help you if you can Post more detail
So my goal is to rearrange a string that is inputted into the program so that it outputs the same info but in a different order. The input order is firstName middleName, lastName, emailAddress and the intended output is lastName, firstName first letter of middleName .
For example the input
John Jack,Brown,JJB#yahoo.com
would output
Brown, John J .
Here's what I have so far
import java.util.Scanner;
public class NameRearranged {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter a name like D2L shows them: ");
String entireLine = keyboard.nextLine();
String[] fml = entireLine.split(",");
String newName = fml[0].substring(7);
String newLine = fml[1] + "," + newName + ".";
System.out.println(newLine);
}
public String substring(int endIndex) {
return null;
}
}
I can't figure out how to separate the firstName and middleName so I can substring() the first letter of the middleName followed by a .
This meets your required output.
import java.util.Scanner;
public class NameRearranged {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter a name like D2L shows them: ");
String entireLine = keyboard.nextLine();
String[] fml = entireLine.split(","); //seperate the string by commas
String[] newName = fml[0].split(" "); //seperates the first element into
//a new array by spaces to hold first and middle name
//this will display the last name (fml[1]) then the first element in
//newName array and finally the first char of the second element in
//newName array to get your desired results.
String newLine = fml[1] + ", " + newName[0] + " "+newName[1].charAt(0)+".";
System.out.println(newLine);
}
}
Check this.
public class NameRearranged {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter a name like D2L shows them: ");
System.out.println(rearrangeName(keyboard.nextLine()));
}
public static String rearrangeName(String inputName) {
String[] fml = inputName.split(" |,"); // Separate by space and ,
return fml[2] + ", " + fml[0] + " " + fml[1].charAt(0) + ".";
}
}
You need to delimit the string for spaces as well. And don't forget the alternate "|" character. Try the following.
String[] fml = entireLine.split(" |, ");