Reputation: 43
this is the gist of the assignment: http://prntscr.com/lwbb1x
so earlier I figured out how the EmployeeNames part of the assignment, or at least I think I did. This is the EmployeeNames code:
public static String[] convertName(String[] names) {
for (int i=0; i<10; i++) {
names[i] = names[i].substring(names[i].length() - 2, names[i].length());
}
return names;
But I'm basically stuck on the Tester code. I know what I want but it's not working. Can anyone help me out? I've been scratching my head at this for hours now.
public static void main(String args[]) {
/*Scanner scan = new Scanner(System.in);
System.out.println("Enter 10 last names.");
String input = scan.nextLine();
*/ (Ignore this, I wanted to try doing inputs, but couldn't even figure out how to work with them properly so I typed up sample last names for this.)
String[] lastName = {"Jones", "Roberts", "Lee", "Chang", "Patel", "Park", "Anderson", "Liu", "Smith", "Lopez"};
System.out.println(convertName(lastName));
}
I like to see modifications to my code or a pseudocode structure because it helps me realize my mistakes best, but any help is crucial! Thank you in advance.
Upvotes: 1
Views: 60
Reputation: 541
You are doing some logical mistakes in problem.
public static String[] convertName(String[] names) {
String newNames[]=new String[names.length];
for (int i=0; i<names.length; i++) {
newNames[i] = names[i].substring(names[i].length() - 2, names[i].length());
}
return newNames;
}
In above method i just create new array and return new array with modified values.
And in Main method following code is used-
public static void main(String[] args) {
String[] lastName = {"Jones", "Roberts", "Lee", "Chang", "Patel", "Park", "Anderson", "Liu", "Smith", "Lopez"};
String [] result= convertName(lastName);
for(int i=0;i<result.length;i++){
String lastNames=result[i];
if(lastNames !=null){
System.out.println(lastNames.toUpperCase().charAt(1)+"."+lastNames.toUpperCase().charAt(0)+". "+lastName[i]);
}
}
}
Hope this will help you.!!
Upvotes: 1
Reputation: 12675
Your were doing some minor mistake, here is working code as per your requirement -
class EmployeeNames {
public static String[] convertName(String[] lastNames) {
String[] formattedNames = new String[lastNames.length];
for (int i = 0; i < lastNames.length; i++) {
formattedNames[i] = lastNames[i].substring(lastNames[i].length() - 1) + "."
+ lastNames[i].substring(lastNames[i].length() - 2, lastNames[i].length() - 1) + "." + lastNames[i];
}
return formattedNames;
}
}
public class EmployeeNamesTester {
public static void main(String[] args) {
String[] lastNames = { "Jones", "Roberts", "Lee", "Chang", "Patel", "Park", "Anderson", "Liu", "Smith",
"Lopez" };
String[] formattedNames = EmployeeNames.convertName(lastNames);
for (String formattedName : formattedNames) {
System.out.println(formattedName);
}
}
}
Output:
s.e.Jones
s.t.Roberts
e.e.Lee
g.n.Chang
l.e.Patel
k.r.Park
n.o.Anderson
u.i.Liu
h.t.Smith
z.e.Lopez
Hope it will help!
Upvotes: 0