Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

book : alice in action with java author: Joel Adams published in : 2008 chapter

ID: 3628219 • Letter: B

Question

book : alice in action with java
author: Joel Adams
published in : 2008
chapter :9 (java methods)
exercise:4
I need to make a program that asks for the full name of the user, and the input should be his first name initial and his last name initial. they give me this example:
public class Initials {
// get the first and last initials of a name
public static String twoInitials(String name) {
String result = ""; // start with empty string
result += firstInitial(name); // append first initial
result += lastInitial(name); // append last initial
return result; // we’re done!
}

// get the first initial of a name
public static char firstInitial(String name) {
char result = name.charAt(0);
return result;
}

// get the last initial of a name
public static char lastInitial(String name) {
int indexOfSpace = name.lastIndexOf(' ');
return name.charAt(indexOfSpace + 1);
}

}
then, i did this :

import java.util.Scanner;


public class Initials2 {
public static void main(String args[]){
Scanner keyboard = new Scanner(System.in);
// get the first and last initials of a name
System.out.println("Please enter your name and last name: ");
String name = keyboard.next(); //
firstInitial(name);
lastInitial(name);
twoInitials(name);

System.out.println(twoInitials(name));


}
public static char firstInitial(String name) {
char result = name.charAt(0);
return result;

}

public static char lastInitial(String name) {
int indexOfSpace = name.lastIndexOf(" ");
return name.charAt(indexOfSpace + 1);
}
public static String twoInitials(String name) {

String result = " ";
result += firstInitial(name);
result += lastInitial(name);
return result;
}



}

when i enter a name like... Jhon Travolta, the result is JJ instead of JT i know the problem is that when u return the value of the name variable in the first method wont be anymore Jhon Travolta so in the second method it gets the letter J as a result it gives JJ i want to know if is possible to get the JT without asking the question "Please enter your name and last name:" twice. sorry im begginer if you dont understand what im trying to say just solve the problem with methods... thx i appreciate your help.


Explanation / Answer

please rate - thanks

import java.util.Scanner;


public class Initials2 {
public static void main(String args[]){
Scanner keyboard = new Scanner(System.in);
// get the first and last initials of a name
System.out.println("Please enter your name and last name: ");
String name = keyboard.nextLine(); //
String initials;
char first,last;
first=firstInitial(name);
last=lastInitial(name);
initials=twoInitials(first,last);

System.out.println(initials);


}
public static char firstInitial(String name) {
char result = name.charAt(0);
return result;

}

public static char lastInitial(String name) {
int indexOfSpace = name.lastIndexOf(' ');
return name.charAt(indexOfSpace + 1);
}
public static String twoInitials(char first,char last) {

String result = " ";
result += first;
result += last;
return result;
}