This question already has answers here:
How to capitalize the first letter of a String in Java?
(59 answers)
Closed 9 years ago.
Here is my code:
import java.util.Scanner;
class namedisplay {
public static void main(String args[]){
Scanner input = new Scanner(System.in);
System.out.println("Enter your name: ");
String name = input.nextLine();
String capital1 = name.substring(0).toUpperCase();
String capital2 = name.substring(5).toUpperCase();
System.out.println(capital1+capital2);
}
}
The program output:
Enter your name:
anna lee
ANNA LEELEE
What I want the program to do is to capitalize only the first letters of the first name and last name, for example, Anna Lee.
System.out.println("Enter your name: ");
String name = input.nextLine();
String newName = "";
newName += name.charAt(0).toUpperCase();
newName += name.substring(1, name.length());
System.out.println(newName);
To get the first letter and capitalize, you use this name.charAt(0).toUpperCase();.
Then add that to the newName.
Then you want to add the remaining letters from name to newName. You do that by adding a substring of name
name.substring(1, name.length()); // 1 mean the substring will start at the
// second letter and name.length means the
// substring ends with the last letter
Related
This question already has answers here:
Scanner is skipping nextLine() after using next() or nextFoo()?
(24 answers)
Closed 5 years ago.
When I run this code it skips the first line("Input first name:") and ask for the lastname. How do i fix this?
public static void newProduct() {
System.out.println("Input first name: ");
String fname = scan.nextLine();
System.out.println("Input last name: ");
String lname = scan.nextLine();
}
The output everytime I run this is:
Input first name: <User cannot input as this line gets skipped>
Input last name: <User can input>
Thanks in advance.
Try this
public class Readname {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("Give a First Name :");
String text = scan.nextLine();
System.out.print("Give a Last Name :");
String text2 = scan.nextLine();
System.out.println("Full Name: "+text+" "+text2);//to bond the last and first name
}
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
import java.util.Scanner;
public class Bigger{
public static void main(String [] args)
{
// declare variables
Scanner keyboardIn = new Scanner(System.in);
String userName = new String();
String fName = new String();
int numberLetters = 0;
int bigLetters=0;
char firstLetter;
// get user name from the user
System.out.print("Please enter your user name: ");
userName = keyboardIn.nextLine();
// get second name from the user
System.out.print("Please enter your second name: ");
fName = keyboardIn.nextLine();
// use an appropriate method to find the number of letters
numberLetters = userName.length();
bigLetters = fName.length();
if(numberLetters > bigLetters)
{
System.out.print("String 1 Is the longest string ");
}
else
{
System.out.print("String 2 Is the longest string ");
}
}
}
I need this to print out the actual string with the actual letters so if Denmark is the bigger string i need it to print this out to the user. How do i do this?
Regards,
Mark
You should actually also check to see if the entered strings are equal in length (The following can be shortened to check greater than or equal to using using >= or less than or equal to <= if required):
import java.util.Scanner;
class Bigger {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
// get user name from the user
System.out.print("Please enter your user name: ");
String userName = input.nextLine();
// get second name from the user
System.out.print("Please enter your second name: ");
String secondName = input.nextLine();
// use an appropriate method to find the number of letters and prompt user
if(userName.length() == secondName.length()) {
System.out.println(userName + " is equal in length than " + secondName);
} else if(userName.length() > secondName.length()) {
System.out.println(userName + " is longer in length than " + secondName);
} else {
System.out.println(userName + " is shorter in length than " + secondName);
}
}
}
Example Usage:
Please enter your user name: MarkDoherty
Please enter your second name: Denmark
MarkDoherty is longer in length than Denmark
Alternatively you could use string formatting like so:
// use an appropriate method to find the number of letters
if(userName.length() == secondName.length()) {
System.out.printf("%s (%d characters long) is equal in length than %s (%d characters long)\n", userName, userName.length(), secondName, secondName.length());
} else if(userName.length() > secondName.length()) {
System.out.printf("%s (%d characters long) is longer in length than %s (%d characters long)\n", userName, userName.length(), secondName, secondName.length());
} else {
System.out.printf("%s (%d characters long) is shorter in length than %s (%d characters long)\n", userName, userName.length(), secondName, secondName.length());
}
Example Usage:
Please enter your user name: MarkDoherty
Please enter your second name: Denmark
MarkDoherty (11 characters long) is longer in length than Denmark (7 characters long)
Try it here!
If I am understanding you correctly, the easiest way would be to just concatenate whatever string you need to print out with the variable inside of the System.out.print(). For example, if I have a variable of type String called myString I would use the line of code:
System.out.print("This is my string variable" + myString);
Also, unless you want everything on one line be sure you are appending newlines to the end of your string or use System.out.println().
Create a String variable and assign your full name to the variable.
Using the String's substring method print out your first name, middle name, and last name on three separate lines.
Modify your program so that it creates a "Scanner" object to allow the user to type in any three names and store it in the String variable.
Modify your program so that it will print out the three names on separate lines no matter what three names the user enters (Hint: use the String's indexof method).
So for this problem, I am doing it in Java. Here is what I have so far. Thank you!
package stringparser;
import java.util.Scanner;
public class StringParser
{
public static void main(String[] args)
{
String Name = "Billy Bob Joe";
String first = Name.substring(0,5);
String middle = Name.substring(6,12);
String last = Name.substring(13,16);
System.out.println("First name: " + first);
System.out.println("Middle name: " + middle);
System.out.println("Last name: " + last);
Scanner in = new Scanner(System.in);
System.out.print("Type any 3 names: ");
System.out.print("First name: ");
String a = in.nextLine();
System.out.print("Second name: ");
String b = in.nextLine();
System.out.print("Third name: ");
String c = in.next();
}
}
2 ways I interpret this question.
Use Scanner 3 times
Use indexOf to find the nearest space character of the one console input.
In all, I find it painfully inefficient to use String.indexOf
Quickest way, but not necessarily the best way.
public StringParser () {
Scanner in = new Scanner(System.in);
String name = in.nextLine();
System.out.println(name.replace(" ", "\n")); // replacing all spaces with new line characters
}
This program will split the string with space and print all of them as result. You can edit the for loop condition as per your need. Hope it helps :)
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
String str = in.nextLine();
String[] names = str.split(" ");
for(int i = 0; i < names.length; i++)
{
System.out.println(names[i]);
}
}
This question already has answers here:
Substring Method
(2 answers)
Closed 8 years ago.
I have to print out an account username which will be their first initial, first 3 letters of their last name, and the last 4 digits of their student number (with no spaces). I don't know how to concatenate the first 3 letters of a string or last 4 digits of a number. Can somebody help please?
import java.util.Scanner;
public class Project {
public static void main (String args[]){
Scanner user_info = new Scanner(System.in);
String first_name;
System.out.print("Enter your first name: ");
first_name = user_info.next();
String last_name;
System.out.print("Enter your last name: ");
last_name = user_info.next();
String student_number;
System.out.print("Enter your student number: ");
student_number = user_info.next();
}
}
first initial:
first_name.substring(0, 1)
first 3 letters of their last name:
last_name.substring(0, 3)
last 4 digits of their student number:
student_number.substring(student_number.length()-4, student_number.length())
This question already has answers here:
Using scanner.nextLine() [duplicate]
(5 answers)
Closed 8 years ago.
My program is supposed to count the number of occurrences a user inputted character appears in a string. For some reason, my program does not execute the for loop. Right after it prints out "Enter the string to search: ", it doesn't let me input a string and prints out: "There are 0 occurrences of '(inputted character)' in '(inputted string)'." on a new line. I need it to be able to find the occurrences of the character over any given amount of words inputted as the string. What can I do to make it function properly? Thanks.
import java.util.Scanner;
public class CountCharacters {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter a character for which to search: ");
char ch = input.next().charAt(0);
System.out.println("Enter the string to search: ");
String str = input.nextLine();
int counter = 0;
for (int i = 0; i < str.length(); i++) {
if (ch == str.charAt(i)) {
counter++;
}
}
System.out.printf("There are %d occurrences of '%s' in '%s'.", counter, ch, str);
System.out.println();
}
}
What happens is that the next() method doesn't consume the new-line character that is entered when you press Enter. Since that character is still there waiting to be read, the nextLine() consumes it. To fix this you can add a nextLine() after the next() call:
char ch = input.next().charAt(0);
input.nextLine(); // consumes new-line character
// ...
For futher information, you could read this post.
After entering your number I guess you are pressing <enter> so this needs to be chewed up before entering your string
try
char ch = input.next().charAt(0);
input.nextLine();
System.out.println("Enter the string to search: ");
String str = input.nextLine();