Java中ArrayList输出中的名称大写
我刚接触Java两周,现在正在用ArrayList处理员工输入系统。无论如何,我想确保无论用户输入什么,输出中的名称都是相同的格式 例如: 输入->输入员工姓名:SAMANTHA 输出->员工姓名:萨曼莎 这是我正在运行的代码,我只是不确定我可以在其中设置格式Java中ArrayList输出中的名称大写,java,arrays,arraylist,string-formatting,Java,Arrays,Arraylist,String Formatting,我刚接触Java两周,现在正在用ArrayList处理员工输入系统。无论如何,我想确保无论用户输入什么,输出中的名称都是相同的格式 例如: 输入->输入员工姓名:SAMANTHA 输出->员工姓名:萨曼莎 这是我正在运行的代码,我只是不确定我可以在其中设置格式 import java.util.ArrayList; import java.util.Scanner; public class EmployeeTester_v5 { public static void main (Stri
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("----------------------");
}
}
}
试着这样做:
System.out.print("Employee Name: " + first.get(i).substring(0,1).toUpperCase()+first.get(i).substring(1).toLowerCase() + " ");
这一个first.geti.substring0,1.toUpperCase表示字符串上限中的第一个字母,first.geti.substring1.toLowerCase从索引1中获取字母-因此从字符串的第二个字母到下限。尝试如下:
System.out.print("Employee Name: " + first.get(i).substring(0,1).toUpperCase()+first.get(i).substring(1).toLowerCase() + " ");
这一个first.geti.substring0,1.toUpperCase将最大化字符串上限中的第一个字母,first.geti.substring1.toLowerCase从索引1中获取字母-因此从字符串的第二个字母到下限。您应该做的第一件事是签出字符串API 谈到Java,这是一个必须知道的问题,您可能需要在每个项目中使用它: 这里有很多方法可以实现你的目标。 例如,您可以将第一个字母大写,然后将要强制使用的字符串的其余部分附加为小写-查看下面的代码段
String inputString = input.next();
String resultString = inputString.substring(0, 1).toUpperCase() + inputString.substring(1).toLowerCase();
您应该做的第一件事是签出字符串API 谈到Java,这是一个必须知道的问题,您可能需要在每个项目中使用它: 这里有很多方法可以实现你的目标。 例如,您可以将第一个字母大写,然后将要强制使用的字符串的其余部分附加为小写-查看下面的代码段
String inputString = input.next();
String resultString = inputString.substring(0, 1).toUpperCase() + inputString.substring(1).toLowerCase();
OP可能不理解的是,这可以包装在私有方法中,如下所示:
private String fixCapitalisation(String input) {
return input.substring(0, 1).toUpperCase() + input.substring(1).toLowerCase();
}
然后,您的行通过调整打印名称的行来使用它:
System.out.print("Employee Name: " + fixCapitalisation(first.get(i)) + " ");
然后,您也可以在姓氏上重用此函数
以下是进行此更改的整个类:
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();
}
我希望我的答案可以帮助您更好地理解已经给出的答案,以及我提供的重复答案的相关性——这里使用ArrayList对字符串操作没有影响
你应该考虑定义你自己的雇员类,它可以保存名字、薪水等等。然后你只需要一个数组,你现在有一个列表,它们的值是由数组中的位置逻辑链接的,但是没有技术依赖性。可以将其包装在私有方法中,如下所示:
private String fixCapitalisation(String input) {
return input.substring(0, 1).toUpperCase() + input.substring(1).toLowerCase();
}
然后,您的行通过调整打印名称的行来使用它:
System.out.print("Employee Name: " + fixCapitalisation(first.get(i)) + " ");
然后,您也可以在姓氏上重用此函数
以下是进行此更改的整个类:
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();
}
我希望我的答案可以帮助您更好地理解已经给出的答案,以及我提供的重复答案的相关性——这里使用ArrayList对字符串操作没有影响
你应该考虑定义你自己的雇员类,它可以保存名字、薪水等等。然后你只需要一个数组,你现在有列表,它们的值是通过数组中的位置逻辑链接的。但是没有技术上的依赖性。
可能重复的@AlanBlyth我看了这个,没有得到我需要的清晰度,因为我使用的是ArrayList可能重复的@AlanBlyth我看了这个,没有得到我需要的清晰度,因为我使用的是ArrayList可能是OP不理解的,这可以封装在私有方法中,如下所示:请注意,我建议的代码段中没有验证。通常,您应该始终检查null、字符串长度等内容。OP可能不理解的是,这可以包装在私有方法中,如下所示:请注意,我建议的代码段中没有验证。通常,您应该始终检查空值、字符串长度等内容。