1

I am relatively fresh (couple weeks) into Java and I am messing around with an Employee input system with ArrayLists. Anyway I want to ensure no matter the user input that that name in the output is the same format.

Example:

Input --> Enter Employee Name: SAMANTHA

Output --> Employee Name: Samantha

Here is the code I am running, I am just not sure where within this I could set that formatting.

import java.util.ArrayList;
import java.util.Scanner;
public class EmployeeTester_v5
{
   public static void main (String[] args)
   {

//ASSIGN VARIABLES

      String c = "";
      String newEmployee = "";
      double yearToDate = 0.0;
      double increase = 0.025;
      double newSalary = 0.0;


//ARRAY LISTS 

      ArrayList<String>first = new ArrayList<String>();
      ArrayList<String>last = new ArrayList<String>();
      ArrayList<Double>salary = new ArrayList<Double>();
      ArrayList<Integer>months = new ArrayList<Integer>();

//SCANNER INPUT

      //create a new scanner
      Scanner input = new Scanner(System.in);


//WHILE LOOP - to keep asking for user input until "No" is entered

      do{

          //USER INPUT
            System.out.println ("Enter employee first name: ");
            first.add(input.next());

            System.out.println ("Enter employee last name: ");
            last.add(input.next());

            System.out.println ("Enter employee salary: ");
            salary.add(input.nextDouble());

            System.out.println ("Number of months worked this year: ");
            months.add(input.nextInt());

            System.out.println("Enter another employee in the system?");
            c = input.next();


        }while(c.equalsIgnoreCase("Yes"));
            System.out.println();


//ARRAY OUTPUT

             for(int i=0; i < first.size(); i++)
                 {
                 yearToDate = months.get(i) * salary.get(i)/12;
                 newSalary = (increase * salary.get(i)) + salary.get(i);

                 System.out.print("Employee Name: " + first.get(i) + " ");
                 System.out.print(last.get(i)+"\n");
                 System.out.printf("Current Salary: $%.2f\n", salary.get(i));
                 System.out.printf("Year to Date: $%.2f\n", yearToDate);
                 System.out.printf("New Salary: $%.2f\n", newSalary);



                 System.out.println("----------------------");
                 }

    }

}
Sam Russo
  • 145
  • 1
  • 3
  • 18
  • Possible duplicate of [How to capitalize the first letter of a String in Java?](https://stackoverflow.com/questions/3904579/how-to-capitalize-the-first-letter-of-a-string-in-java) – Alan Blyth Feb 13 '18 at 13:47
  • @AlanBlyth I looked at this and didn't get the clarity I needed since I am using ArrayLists – Sam Russo Feb 13 '18 at 13:48

3 Answers3

4

First thing you should do is to check out String API https://docs.oracle.com/javase/8/docs/api/java/lang/String.html
It's a must-know when it comes to Java, you'll need it in possibly every project you'll work on :)

There are plenty of ways to achieve your goal here.
What you could do for example is to capitalize the first letter and then append the rest of the String that you'll force to lowercase - check out the snippet below.

String inputString = input.next();
String resultString = inputString.substring(0, 1).toUpperCase() + inputString.substring(1).toLowerCase();
Amaneusz
  • 158
  • 1
  • 1
  • 8
  • Maybe what the OP is not understanding, is that this can be wrapped in a private method, like this: – Alan Blyth Feb 13 '18 at 13:54
  • 1
    Please notice that there are no validations in the snippet that I've suggested. Normally, you should always check stuff like nulls, length of the String etc. – Amaneusz Feb 13 '18 at 13:54
1

Try like this :

System.out.print("Employee Name: " + first.get(i).substring(0,1).toUpperCase()+first.get(i).substring(1).toLowerCase() + " ");

this one first.get(i).substring(0,1).toUpperCase() gest your first letter in string upper, and first.get(i).substring(1).toLowerCase() gets letters from index 1 - so from the 2nd letter of the string to lower.

leopcode
  • 62
  • 1
  • 8
0

Maybe what the OP is not understanding, is that this can be wrapped in a private method, like this:

private String fixCapitalisation(String input) {

return input.substring(0, 1).toUpperCase() + input.substring(1).toLowerCase();
}

Then your line uses this by adjusting the line that prints the name:

System.out.print("Employee Name: " + fixCapitalisation(first.get(i)) + " ");

You can then reuse this function on the last name too...

Here is the entire class with this change:

package Dunno;
import java.util.ArrayList;
import java.util.Scanner;
public class EmployeeTester_v5
{
   public static void main (String[] args)
   {

//ASSIGN VARIABLES

      String c = "";
      String newEmployee = "";
      double yearToDate = 0.0;
      double increase = 0.025;
      double newSalary = 0.0;


//ARRAY LISTS 

      ArrayList<String>first = new ArrayList<String>();
      ArrayList<String>last = new ArrayList<String>();
      ArrayList<Double>salary = new ArrayList<Double>();
      ArrayList<Integer>months = new ArrayList<Integer>();

//SCANNER INPUT

      //create a new scanner
      Scanner input = new Scanner(System.in);


//WHILE LOOP - to keep asking for user input until "No" is entered

      do{

          //USER INPUT
            System.out.println ("Enter employee first name: ");
            first.add(input.next());

            System.out.println ("Enter employee last name: ");
            last.add(input.next());

            System.out.println ("Enter employee salary: ");
            salary.add(input.nextDouble());

            System.out.println ("Number of months worked this year: ");
            months.add(input.nextInt());

            System.out.println("Enter another employee in the system?");
            c = input.next();


        }while(c.equalsIgnoreCase("Yes"));
            System.out.println();


//ARRAY OUTPUT

             for(int i=0; i < first.size(); i++)
                 {
                 yearToDate = months.get(i) * salary.get(i)/12;
                 newSalary = (increase * salary.get(i)) + salary.get(i);

                 System.out.print("Employee Name: " + fixCapitalisation(first.get(i)) + " ");
                 System.out.print(last.get(i)+"\n");
                 System.out.printf("Current Salary: $%.2f\n", salary.get(i));
                 System.out.printf("Year to Date: $%.2f\n", yearToDate);
                 System.out.printf("New Salary: $%.2f\n", newSalary);



                 System.out.println("----------------------");
                 }

    }

   //New Method to fix capitalisation
   private static String fixCapitalisation(String input) {

     return input.substring(0, 1).toUpperCase() + input.substring(1).toLowerCase();
     }

I hope my answer might help you to understand the answers already given better, and how the duplicate answer i provided is relevant - the use of an ArrayList here makes no difference to the String manipulation.

You should consider defining your own class of "Employee", that can persist the name, salary etc. then you only need one ArrayList, you currently have lists with values that are Logically linked by their position in the Array, but there is no technical dependency on that.

Alan Blyth
  • 186
  • 1
  • 9