2012-10-25 43 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的是不是一個數字。

相關問題