By Sam Russo Palmer


2018-02-13 13:41:19 8 Comments

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("----------------------");
                 }

    }

}

3 comments

@Alan Blyth 2018-02-13 13:58:40

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.

@Amaneusz 2018-02-13 13:52:25

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

@Alan Blyth 2018-02-13 13:54:28

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

@Amaneusz 2018-02-13 13:54:45

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.

@Leonard Pera 2018-02-13 13:51:26

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.

Related Questions

Sponsored Content

78 Answered Questions

[SOLVED] Is Java "pass-by-reference" or "pass-by-value"?

32 Answered Questions

[SOLVED] Create ArrayList from array

60 Answered Questions

[SOLVED] How do I generate random integers within a specific range in Java?

  • 2008-12-12 18:20:57
  • user42155
  • 3488980 View
  • 2911 Score
  • 60 Answer
  • Tags:   java random integer

32 Answered Questions

[SOLVED] How do I convert a String to an int in Java?

30 Answered Questions

[SOLVED] When to use LinkedList over ArrayList?

6 Answered Questions

[SOLVED] Convert ArrayList<String> to String[] array

30 Answered Questions

[SOLVED] What's the simplest way to print a Java array?

  • 2009-01-03 20:39:39
  • Alex Spurling
  • 1711478 View
  • 1536 Score
  • 30 Answer
  • Tags:   java arrays printing

53 Answered Questions

[SOLVED] Creating a memory leak with Java

30 Answered Questions

[SOLVED] Initialization of an ArrayList in one line

19 Answered Questions

[SOLVED] How do I declare and initialize an array in Java?

  • 2009-07-29 14:22:27
  • bestattendance
  • 3728269 View
  • 1626 Score
  • 19 Answer
  • Tags:   java arrays declare

Sponsored Content