Java 将某些字符串元素转换为int
我在网上搜索过如何转换数组中的两个特定元素,但这项研究非常不走运Java 将某些字符串元素转换为int,java,arrays,Java,Arrays,我在网上搜索过如何转换数组中的两个特定元素,但这项研究非常不走运 package scanner; import java.util.Scanner; public class EmployeeInformation { static Scanner sc = new Scanner(System.in); static String[][] info = {{"09-001", "Ja Gb", "100", "10", },
package scanner;
import java.util.Scanner;
public class EmployeeInformation {
static Scanner sc = new Scanner(System.in);
static String[][] info = {{"09-001", "Ja Gb", "100", "10", },
{"09-002", "Justine", "200", "20", },
{"09-003", "Ja Ja", "150", "15", }};
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.print(" - MENU -\n");
System.out.print("A. Search Record\nB. Payroll Summary\n------------------\nEnter choice: ");
String choice = null;
choice = sc.nextLine();
if (choice.equalsIgnoreCase("a")) {
System.out.print("Enter Employee #: ");
String EmpNum = sc.nextLine();
SearchRecord(EmpNum);
}
else if (choice.equalsIgnoreCase("b")){
PayrollSummary();
}
else {
System.out.print("Invalid input.");
}
}
private static void SearchRecord(String employeeNumber) {
// TODO Auto-generated method stub
String[] matchedRow = null;
for (int i = 0; i < info.length; i++) {
String[] oneRow = info[i];
if (oneRow[0].equals(employeeNumber)) {
matchedRow = oneRow;
break;
}
}
System.out.print("\nEmployee #:\tEmployee Name\tRate per Hour\tTotal Hours Worked\n");
for (int i = 0; i < matchedRow.length; i++) {
System.out.print(matchedRow[i] + "\t\t");
}
}
private static void PayrollSummary() {
System.out.println("\nEmployee #:\tEmployee Name\tRate per Hour\tTotal Hours Worked\tGross Pay");
int intArr[] = new int[info.length];
int r = 0;
while ( r < info.length) {
int c = 0;
while ( c <= r ) {
if ( c == 2 ) {
intArr[c] = Integer.parseInt(info[r][c]);
if ( c == 3 ) {
intArr[c] = Integer.parseInt(info[r][c]);
}
}
c++;
// How do I multiply index 2 and 3 of Array info and store it in info[r][4]?
}
r++;
}
}
}
...
包装扫描仪;
导入java.util.Scanner;
公共类雇员信息{
静态扫描仪sc=新扫描仪(System.in);
静态字符串[][]信息={{“09-001”、“Ja Gb”、“100”、“10”、},
{“09-002”、“贾斯汀”、“200”、“20”},
{“09-003”、“Ja-Ja”、“150”、“15”、};
/**
*@param args
*/
公共静态void main(字符串[]args){
//TODO自动生成的方法存根
系统输出打印(“-MENU-\n”);
System.out.print(“A.搜索记录\nB.工资汇总\n------------------------------输入选项:”);
字符串选择=null;
choice=sc.nextLine();
if(选择同等信号案例(“a”)){
系统输出打印(“输入员工:”;
字符串EmpNum=sc.nextLine();
搜索记录(EmpNum);
}
else if(选择。等效信号情况(“b”)){
工资汇总表();
}
否则{
系统输出打印(“无效输入”);
}
}
私有静态无效搜索记录(字符串employeeNumber){
//TODO自动生成的方法存根
字符串[]matchedRow=null;
对于(int i=0;i 而(c为了将表示为字符串的两个值相乘,必须首先解析它们
如果您想将任意字符串解析为整数,您应该记住不可能解析某些字符串,例如“Justine”。您必须处理在这种情况下将抛出的NumberFormatException
try{
Integer myInt = Integer.parseInt(info[x][y]);
}catch(NumberFormatException e){
// handle your exception
}
为了将表示为字符串的两个值相乘,必须首先解析它们
如果您想将任意字符串解析为整数,您应该记住不可能解析某些字符串,例如“Justine”。您必须处理在这种情况下将抛出的NumberFormatException
try{
Integer myInt = Integer.parseInt(info[x][y]);
}catch(NumberFormatException e){
// handle your exception
}
你可以这样做
class Testing
{
public static void main(String[] args)
{
System.out.println(isNumeric("123"));
System.out.println(isNumeric("123.45"));
System.out.println(isNumeric("$123"));
System.out.println(isNumeric("123x"));
}
public static boolean isNumeric(String str)
{
try
{
double d = Double.parseDouble(str);
}
catch(NumberFormatException nfe)
{
return false;
}
return true;
}
}
通过这种方式,您可以通过数组进行解析,如果它是数字格式异常,则它不是数字。您可以这样做
class Testing
{
public static void main(String[] args)
{
System.out.println(isNumeric("123"));
System.out.println(isNumeric("123.45"));
System.out.println(isNumeric("$123"));
System.out.println(isNumeric("123x"));
}
public static boolean isNumeric(String str)
{
try
{
double d = Double.parseDouble(str);
}
catch(NumberFormatException nfe)
{
return false;
}
return true;
}
}
通过这种方式,您可以通过数组进行解析,如果它是一个数字格式异常,它就不是一个数字。这段代码中发生了什么?这段代码中有例外吗?这就是您要寻找的:info[r][4]=info[r][3]*info[r][2]
?您想解析info数组中的值吗?如果是,请记住解析,例如“Justine”into int将产生NumberFormatException。[…]将其存储在info[r][4]?中。您的info
的每一行的长度都是4,因此最大索引是info[r][3]
@ronk,是的。我尝试了该代码,但没有成功。我得到了以下错误:“运算符*未为参数java.lang.string定义…”这段代码中发生了什么?这段代码中有任何异常吗?这就是您要查找的:info[r][4]=info[r][3]*info[r][2]
?是否要解析info数组中的值?如果是,请记住,将例如“Justine”解析为int将产生NumberFormatException。[…]将其存储在info[r][4]?.您的info
的每一行的长度都是4,因此最大索引是info[r][3]
@ronk,是的。我尝试了该代码,但没有成功..我遇到了此错误“运算符*未为参数java.lang.string定义…”@mill.bartek非常感谢它解决了我的问题。我只是想知道为什么我必须处理numberformatexception?我的代码是这样编写的,并且得到了程序的正确输出int arrR=0;int arrC=0;int r=0;而(r
如果您100%确定数组中提供的值始终是能够解析为int的字符串(例如“10”、“45”),则可以跳过错误处理。处理错误可以使应用程序以正确的方式工作,而不依赖于提供给parseInt()的输入方法。@mill.bartek非常感谢它解决了我的问题。我只是想知道为什么我必须处理numberformatexception?我的代码是这样编写的,并且得到了程序的正确输出int arrR=0;int arrC=0;int r=0;而(r
如果您100%确定数组中提供的值始终是能够解析为int的字符串(例如“10”、“45”),则可以跳过错误处理。处理错误可以使应用程序以正确的方式工作,而不依赖于提供给parseInt()方法的输入。