How to concatenate parts of strings in Java? [duplicate] - java

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())

Related

Skipping first String Input when I am giving data in Console [duplicate]

This question already has answers here:
Scanner is skipping nextLine() after using next() or nextFoo()?
(24 answers)
Closed 2 years ago.
I want to make a program that will read user inputs and will printout them gradually. But when I am running the code, in the Console area, the first line is automatically skipping. But when I am taking input as Integer, all is running well. Where is my fault?
import java.util.*;
public class MainClass {
static Scanner input = new Scanner(System.in);
public static void main(String[] args) {
int limit, i, j;
System.out.print("How many names you want to take: ");
limit = input.nextInt();
String[] name = new String[limit];
for (i = 0; i < name.length; i++) {
System.out.print("Enter your name: ");
name[i] = input.nextLine();
}
for (String output : name) {
System.out.println("Names are: " + output);
}
}
}
Console area:
How many names you want to take: 3
Enter your name: Enter your name: Saon
Enter your name: Srabon
Names are:
Names are: Saon
Names are: Srabon
Invoke input.nextLine() after the input.nextInt() in order to clear the new line character produced by pressing Enter key (when you enter int number);
Alternatively, you can read your int as Integer.valueOf(input.nextLine()).

Why does this skips the first line? [duplicate]

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
}
}

Need little help on understanding how starting index and substring works [duplicate]

This question already has answers here:
How does Java store Strings and how does substring work internally? [closed]
(2 answers)
Closed 5 years ago.
Here is my code
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
System.out.print("Type a word: ");
String userWord = reader.nextLine();
System.out.print("Length of the first part: ");
int userLength = Integer.parseInt(reader.nextLine());
System.out.println("Result: " + userWord.substring(0, userLength));
}
Result:
Type a word: hellothere
Length of the first part: 3
Result: hel
Starting index starts counting from 0 right? So shouldn't the result print "hell"?
0 = h
1 = e
2 = l
3 = l
The second parameter of substring() is an exlusive index. Means: When you enter 3 it will give you only the letters until index 2. This isn't one of the most obvious methods in the Java API, but should be well documented.

need this to return what is entered [closed]

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().

How to capitalize the first letter of your name in java? [duplicate]

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

Categories

Resources