I've only been learning java for a few weeks so I'm still a noob. I want the next line to print "mr/miss." + firstname + lastname;
however, i don't want to type the gender of the person in. I want there to be a list of male names (long list over 1000 names) and the program to detect if the firstname input is one of those male names. The program can then assign the correct title (mr/ms.)
How do I do this without making a normal arraylist and typing out each name individually (which'll take agessss).
Thanks in advance!
public static void main (String [] args){
Scanner scanner = new Scanner(System.in);
System.out.println("Hello, will you be checking out today? (Y/N)");
String CheckOutYesOrNo = scanner.nextLine();
if (CheckOutYesOrNo.equalsIgnoreCase("y")) { x();}
else if (CheckOutYesOrNo.equalsIgnoreCase("n")) {System.out.println("Okay then. Enjoy the rest of your stay at the Rizty Hotel!");}
}
public static void x(){
System.out.println("Sure, would you mind telling me your last name?");
Scanner scanner = new Scanner(System.in); //how can I avoid making a new scanner?
String lastname = scanner.nextLine();
System.out.println("And your first name?");
String firstname= scanner.nextLine();
}
}
Sounds like a typical mapping. You could read all your names into a Map where the key is the name and the gender is the value. To resolve the gender just do something like genderMap.get(name).
Related
I am trying to get NetBeans to print full name of customer with capital letters for the start of the names but I'm only get initials
package question1;
import java.util.Scanner;
public class Question1 {
public static void main(String[] args) {
// TODO code application logic here
String First;
String Second;
String Fullname;
char UpperCaseFirst;
char UpperCaseSecond;
Scanner input=new Scanner (System.in);
System.out.println ("enter First name");
First=input.next();
System.out.println ("enter Second name");
Second=input.next();
UpperCaseFirst=Character.toUpperCase(First.charAt(0));
UpperCaseSecond=Character.toUpperCase(Second.charAt(0));
Fullname=UpperCaseFirst+" "+UpperCaseSecond;
System.out.println("custoemr:\n");
System.out.println(Fullname);
Please follow Java variable naming conventions, firstName, lastName and fullName (not Firstname, that looks like a class). Second, you shouldn't declare your variables until you need to. And you can use String.substring(int) to get the rest of the String. You can also use String formatting, with String.format, System.out.printf or both. Don't forget to call toLowerCase() at some point (if you want consistency). Like,
System.out.println("enter First name");
String firstName = input.next();
System.out.println("enter Second name");
String lastName = input.next();
String fullName = String.format("%c%s %c%s",
Character.toUpperCase(firstName.charAt(0)),
firstName.substring(1).toLowerCase(),
Character.toUpperCase(lastName.charAt(0)),
lastName.substring(1).toLowerCase());
System.out.printf("customer:%n%s%n", fullName);
First off here is the code with the appropriate descriptions for each command. (Note: the last line is what gives the code error and what I need help fixing).
What is happening on the last line of the code pertains to my question. how is the 'fullName' variable going to be changed to uppercase when it already has an input inside it. how do I go about replacing it later in the code? Thank you
import java.util.Scanner; // Needed for the Scanner class
public class NumericTypes {
public static void main (String [] args) {
//TASK #2 Create a Scanner object here
//Reading from system.in
Scanner keyboard = new Scanner(System.in);
//prompt user for first name
System.out.println("Enter your first name: ");
//scans the next input as a double
String firstName = keyboard.nextLine();
//prompt user for last name
System.out.println("Enter your last name: ");
//scans the next input as a double
String lastName = keyboard.nextLine();
//concatenate the user's first and last names
String fullName = (firstName + " " + lastName);
//print out the user's full name
System.out.println(fullName);
//task 3 starts here
//get first initial from variable 'fullName'
char firstinitial = fullName.charAt(0);
System.out.println("the first initial is: " + firstinitial);
//use the 'toUpperCase' method to change fullName variable to caps
// and store into the fullName variable
String fullName = fullName.toUpperCase()
}
}
You are trying to create the variable fullName which already exists. Change the variable name to something else.
String upperFullName = fullName.toUpperCase();
or omit the declaration
fullName = fullName.toUpperCase();
If you are getting an error, change
String fullName = fullName.toUpperCase()
to
fullName = fullName.toUpperCase();
First off, you didn't write a semicolon ; at the end of the statement.
Second, you can't declare two variables with the same name, which you were doing here. Removing String from this sentence changes the value of the variable fullName.
im fairly sure the mistake I am making within this code is short sighted.
so this program starts by getting the first name and last name of the user and storing them as independent strings. the next part is for the program to manipulate that value into getting the first initial of the first name, which is where im having my problem (I have little experience with the CharArray function and have spent enough independent research time for me to opt to asking here lmao)
import java.util.Scanner; //Needed for the Scanner class
public class NumericTypes {
public static void main (String [] args) {
//TASK #2 Create a Scanner object here
//Reading from system.in
Scanner keyboard = new Scanner(System.in);
//prompt user for first name
System.out.println("Enter your first name: ");
//scans the next input as a double
String firstName = keyboard.nextLine();
//prompt user for last name
System.out.println("Enter your last name: ");
//scans the next input as a double
String lastName = keyboard.nextLine();
//concatenate the user's first and last names
String fullName = (firstName + " " + lastName);
//print out the user's full name
System.out.println(fullName);
//task 3 starts here
//get first initial from variable 'fullName'
String firstinitial = fullName.CharAt(0);
System.out.println("the first initial is: " + firstinitial);
}
}
my desired output is for the last set of lines to display the first initial of the first name (user input). any help would be greatly, greatly appreciated
This can be done in two ways -:
1.) Replace String firstinitial with char firstinitial
2.) Wrap fullName.charAt(0) with String.valueOf like this:
String firstinitial = String.valueOf(fullName.charAt(0));
Both will work just fine.
This question already has answers here:
Prompt user to enter name on one line and print it out as "Last, First"
(4 answers)
Closed 6 years ago.
So far I have the following code:
import java.util.*;
public class Names {
public static void main(String [] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter your name: ");
String name = input.next();
changeNameFormat(name);
}
public static void changeNameFormat(String name) {
System.out.println(name.substring(0,20));
}
}
I am not sure, but I think I need to use an array, however I am unsure of how to go about this seeing as I will not know what the user input will be every time.
Example of what I want to happen:
User inputs:
John Smith
I want the program to output it as this:
Smith, John
Lets suppose you ask the user to enter the first name and last name with a comma separation.
For example: FirstName,LastName
In this case you can use comma as the delimiter for identifying first name and last name.
public static void changeNameFormat(String name) {
String [] splitName = name.split(",");
//splitName[0] holds first name and splitName[1] holds last name
System.out.println(splitName[1] + ", " + splitName[2]);
}
An easier way without manipulating / splitting one String is to use the Scanner's default delimiter (whitespace):
Scanner input = new Scanner(System.in);
System.out.println("Enter your name: ");
// assuming the input consists in name surname[return]
String name = input.next();
String surname = input.nextLine();
System.out.printf("%s, %s%n", surname.trim(), name.trim());
input.close();
You can do the following in the changeNameFormat
System.out.println(name.replaceAll("^(\\w+)\\s+(\\w+)$", "$2, $1"));
Also, remember to change the input.next() to input.nextLine() to be able to read multiple words in one line. For more detail on the regex, please refer to the following link:
https://regex101.com/r/sA4xJ5/2
im trying to learn Java, meaning that my experience and knowledge is almost none, and i hope you can help me with this:
(The thought i had was like how you can name yourself in videogames, with the option to display the name in future instances without any deeper meaning later on)
import java.util.Scanner;
public class Playername {
public static void main(String[] args) {
public String getName();
Scanner Charname = new Scanner (System.in);
System.out.print ("What is your name?");
Charname.nextLine();
String getName = Charname;
System.out.print (Charname);
}
}
i probably messed this up, reading only the early chapters of a book about java and then trying to use commands and things in way's they were not supposed to be used.
(i wanted to save the name of the scanner and then copypaste it to the String variable)
thank you
PS: The error i get is "cannot convert from scanner to string", basically stating that my question would be how the idea i had can, in the most simplistic way possble, be realised.
Try this
Scanner Charname = new Scanner (System.in);
System.out.print ("What is your name?");
String input = Charname.nextLine();
String getName = input;
System.out.print (input);
You cannot assign scanner object directly to string reference.
You are trying to assign Charname which is a Scanner to a String
What you want to do is to assign Charname.nextLine() which is a String to your variable
Scanner Charname = new Scanner (System.in);
System.out.print ("What is your name?");
String getName = Charname.nextLine();
System.out.print(getName);
Welcome to StackOverflow!
Firstly, I'd like to start you off with some code conventions.
These help others read your code, since even though you may know what is going on; we may not.
Now, for your question.
Scanner scannerName = new Scanner (InputStream inputStream);
This line creates a Scanner object with the InputStream coming from the an input stream, and we name it scannerName. Generally, you want to label your objects what they are, or some abbreviation of that.
Scanner scanner1 = new Scanner(System.in);
This is sufficient for our purposes. Since we want to get information from the System.in inputstream, or console, we use System.in as our InputStream.
Now, this scanner object that we've created can receive input from the console. It does this through the use of methods like
scanner1.nextLine();
The above piece of code returns a String value.
Alone, it's pretty useless. So we'll assign a String object to take and store that value.
String characterName = scanner1.nextLine();
What this does is it sets the String's value to be equivalent to the value of the scanner1's inputstream, after hitting enter.
So if a user enters "Johnathan Nathan", and then presses Enter,the String named characterName will be set to "Johnathan Nathan".
A full working example is:
Scanner scanner1 = new Scanner(System.in);
System.out.println("Please enter your name: ");
String characterName = scanner1.nextLine();
System.out.println("Hello, " + characterName());
This would ask for the person's name, then say hello to them directly after.
Finally, if you have any questions regarding how a class is used, you can always look up the Javadoc of the class you're having problems with.