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);
Related
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.
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).
I'm currently doing a fairly simple project for my class, but I encounter this weird problem.
Here's the code:
import java.util.Scanner;
public class StudentMain {
static Scanner kb = new Scanner(System.in);
public static void main(String[] args) {
System.out.println("This program allows you to organize a student's info in a clear, coherent form");
System.out.println("Press [1] to continue");
int x = kb.nextInt();
if (x==1){
Name();
ID();
String IDen = ID();
String name = Name();
System.out.println(name);
System.out.println(IDen);
}
}
private static String Name(){
String SName;
System.out.println("Enter Student name: ");
SName = kb.nextLine();
return SName;
}
private static String ID(){
String Sid;
System.out.println("Enter Student I.D: ");
Sid = kb.nextLine();
return Sid;
}
}
My intention for the program is listed in print statements above, but this is my biggest concern right now.
Whenever I run the program: I get this result (using it in Eclipse):
This program allows you to organize a student's info in a clear, coherent form
Press [1] to continue
1
Enter Student name:
Enter Student I.D:
John
Enter Student I.D:
1034172
Enter Student name:
John
John
1034172
As you can see, I used "John" and "1034172" as just examples, but its executing the two return methods twice. Any insight on this? All replies are welcome and greatly appreciated. Thanks!
Because you are calling it twice as result of first method calls has not been used so those are not needed.
Name();//<-------------(1)
ID();//<-------------(1)
//You can remove these lines
String IDen = ID();//<-------------(2)
String name = Name();//<-------------(2)
Note:You should follow the naming conventions as method name/variable name(except constant) should start with small case.
Here Name(); is method call and it's upto you whether you use the returned value or not.
Name();//First Call
String name =Name();//Second Call
I started a java course a few weeks ago and today we were given a question sheet we will be doing in class tomorrow. I wanted to have a go at it myself tonight but alas I'm stuck on question 1.
We covered methods of the String class today but I can't figure out which if any of them to use.
So finally here's the question:
Q. Write and test a program which will prompt the user to enter their name and write to the screen just the surname. (Where the surname is interpreted as everything after the first white space)
And here's what I have so far:
import java.util.Scanner;
public class Question1 {
public static void main(String[] args) {
String fullName;
String surname;
Scanner keyboard = new Scanner(System.in);
System.out.println("Please enter your name: ");
fullName = keyboard.nextLine();
if(fullName.contains(" ")) {
// not sure what goes in here?
}
}
}
I've been messing around with the if statement above and also various methods of the String class like subString(), charAt() etc but I don't know how to find the first space and then print out the String after the first space.
You can do it by making use of indexOf() and subString() methods of String class: For e.g.,
public String substringAfter(String str) {
int pos = str.indexOf(" ");
if (pos == -1) {
return "";
}
return str.substring(pos + 1);
}
You could use what's in the comments, but you could use the split method too. It takes two parameters (or one, the second one is optional): the delimeter String and the limit. It returns the String array of splitted Strings. Example:
String[] splitted = fullName.split(" ", 2);
Now splitted[0] is the first name (part before first white space) and splitted[1] is the rest.
More info here: http://docs.oracle.com/javase/8/docs/api/java/lang/String.html
And in general you can find a lot in the Java API documentation.
UPDATE
1) I mistaked the limit, it should be 2 instead of 1
For clarification, here is the test:
public class Test
{
public static void main(String[] args)
{
String fullName = "Luiggi SecondName ThirdName NotMyLastNameYet StillNotMyLastNameCuzImaTroll";
String surname;
String[] splitted = fullName.split(" ", 2);
surname = splitted[1];
System.out.println(splitted[1]);
}
}
Output:
SecondName ThirdName NotMyLastNameYet StillNotMyLastNameCuzImaTroll
Would this work for you or do you only want String methods?
public static void main(String[] args) {
String fullName;
String surname;
Scanner keyboard = new Scanner(System.in);
System.out.println("Please enter your name: ");
fullName = keyboard.nextLine();
if(fullName.contains(" ")) {
Scanner nameScanner = new Scanner(fullName);
String name = nameScanner.next();
surname = nameScanner.nextLine();
}
}
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
I have a question regarding how to use variables that I have already used in a method into another method and how to set a limit on what a user can input, for example, for one of my java homework assignments I need to write a program that takes 3 to 6 names and then I need to create a new static method called geneateNewName() and it has to use the 3 to 6 names to display the names as well as take each of the names' second letter and produce a new name, how can I do this, thank you. Also, here is the code that I have so far:
public class Testing{
public static void main(String args[]){
Console c = System.console();
String str1;
String str2;
String str3;
String str4;
String str5;
String str6;
str1 = c.readLine("Please enter a family member's name: ");
str2 = c.readLine("Please enter a family member's name: ");
str3 = c.readLine("Please enter a family member's name: ");
str4 = c.readLine("Please enter a family member's name: ");
str5 = c.readLine("Please enter a family member's name: ");
str6 = c.readLine("Please enter a family member's name: ");
}
public static void generateNewName(){
System.out.println((str1.charAt(1)));
System.out.print(str22.charAt(1));
System.out.print(str33.charAt(1));
System.out.print(str44.charAt(1));
System.out.print(str55.charAt(1));
System.out.print(str66.charAt(1));
}
}
When I run this program on the command line, it only asks for six names, it doesn't display the newly generated name that is coded in the generateNewName() static method.
You never call generateNewName(). You should stick it in main somewhere. if you want it to be run.
you should call generateNewName method after the last read line like this:
str6 = c.readLine("Please enter a family member's name: ");
generateNewName();
}
Like the others have said, you never call generateNewName() so it doesn't run. But there are much bigger problems here.
You should be using Scanner to read user input. You're also declaring your string variables in main() which limits their scope so generateNewName() can't see them. There are several ways you can make your strings available to generateNewName(), including passing in all the strings individually or storing them all in an array and passing that in. However for your purposes you can just declare all the strings as static class variables.
class Testing
{
public static String str1;
public static String str2;
public static String str3;
public static String str4;
public static String str5;
public static String str6;
public static void main(String args[])
{
Scanner scanner = new Scanner(System.in);
System.out.println("Please enter a family member's name: ");
str1 = scanner.next();
System.out.println("Please enter a family member's name: ");
str2 = scanner.next();
System.out.println("Please enter a family member's name: ");
str3 = scanner.next();
System.out.println("Please enter a family member's name: ");
str4 = scanner.next();
System.out.println("Please enter a family member's name: ");
str5 = scanner.next();
System.out.println("Please enter a family member's name: ");
str6 = scanner.next();
generateNewName();
}
public static void generateNewName()
{
System.out.print(str1.charAt(1));
System.out.print(str2.charAt(1));
System.out.print(str3.charAt(1));
System.out.print(str4.charAt(1));
System.out.print(str5.charAt(1));
System.out.print(str6.charAt(1));
}
}
Since I'm assuming you're still a beginner the above code will work for you just fine. But I'd like to show you how you could cut way back on the number of lines of code you have using techniques you've probably already learned or hopefully will learn very soon. By using a for loop and string array you don't have to write out all those lines each time you want to prompt and collect data from the user. From there you can pass the array of names to generateNewName() and use another for loop to print out your new name. Now you don't need to declare your strings individually and you can control how many times the user is prompted to provide input by simply changing the size of the array.
class Testing
{
public static void main(String args[])
{
Scanner scanner = new Scanner(System.in);
int arraySize = 0;
while(arraySize > 6 || arraySize < 3)
{
//Get user input here.
}
String[] names = new String[arraySize];
for(int i = 0; i < names.length; i++)
{
System.out.println("Please enter a family member's name: ");
names[i] = scanner.next();
}
generateNewName(names);
}
public static void generateNewName(String[] names)
{
for(int i = 0; i < names.length; i++)
{
System.out.print(names[i].charAt(1));
}
}
}
To print out the generated name on one line make sure you use System.out.print() and not System.out.println().