2012-10-25 45 views
1

我寻遍了如何转换两个特定的数组中的元素网,正要研究很倒霉把一些字符串元素为int

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++; 
     } 



    } 
} 

... 
+1

这段代码会发生什么?这段代码是否有例外? – gks

+0

这是你在找什么:'info [r] [4] = info [r] [3] * info [r] [2]'? – RonK

+0

你想解析信息数组中的值吗?如果是这样,请记住,例如将“Justine”解析为int将产生NumberFormatException。 – MrKiller21

回答

4

为了繁衍字符串表示的两个值你必须先解析它们。

如果要将任意字符串解析为Integer,则应记住,解析某些字符串(例如“Justine”)是不可能的。您必须处理在这种情况下将引发的NumberFormatException。

try{ 
    Integer myInt = Integer.parseInt(info[x][y]); 
}catch(NumberFormatException e){ 
    // handle your exception 
} 
+0

@ mill.bartek非常感谢它解决了我的问题..我只是想知道为什么我必须处理numberformatexception? 我做了我这样的代码,并得到了程序的正确输出 'int arrR = 0; int arrC = 0; int r = 0; 而(R

+0

对格式化表示歉意...... –

+0

如果您100%确定数组内提供的值始终是能够被解析为int的字符串(例如“10”,“45”),那么您可以跳过错误处理。 – MrKiller21

0

你可以做到这一点

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; 
    } 
} 

这种方式,你可以通过你的阵列分析,如果它是一个NumberFormatException的是不是一个数字。

相关问题