2012-10-20 31 views
2

我有一個二維數組。該任務是接受來自用戶的行號和列號,並且如果用戶想要逐行輸入數字數據(或逐列輸入),他必須用逗號來輸入數字數據。如,3×3將是:用於多維數組的Java中的Split()方法

1,2,4

2,5,3

5,3,2

所以第一行元素將是[0,0 ] = 1 [0,1] = 2 [0,2] = 3,第二行[1,0] = 2 [1,1] = 5 [1,2] = 3等 我知道我應該用字符串做,然後分割爲「,」,然後將其轉換爲整數,但我不知道如何在這種情況下進行分割。

這就是我對行輸入:

for (int row=0; row<board.length; row++){ 
     for (int column=0; column<board[row].length; column++){ 
      //System.out.print("["+board[row]+"]["+board[column]+"]: "); 
      board[row][column] = myInput.nextInt(); 
      //input validation 
      while ((1 > (board[row][column])) || ((board[row][column]) > board.length)) 
      { 
       System.out.println("Please enter number between 1 to "+ board.length +":"); 
       board[row][column] = myInput.nextInt(); 
      } 
     } 
    } 

回答

0

我想這是你想要的,假設inputStr存儲用戶輸入:

String[] values = inputStr.split(','); 
if(values.length != 2) { 
    //Tell them they should enter two numbers 
    System.out.println("Please enter two numbers separated by a comma"); 
} 
else { 
    try { 
     int rowVal = Integer.parseInt(values[0]); 
     int columnVal = Integer.parseInt(values[0]); 

     System.out.println("The value is: "+board[rowVal][columnVal]); 
    } 
    catch (NumberFormatException e) { 
     //It was comma separated but not two numbers 
     System.out.println("Please enter two numbers separated by a comma"); 
    } 
} 

對於由行插入行:

//Just to clean up the code 
int rows = board.length; 
int columns = board[0].lemgth; 

String[][] dataValues = new String[rows][columns] 
String[] data = new String[rows.length]; 
//Recieve data 
for(int i = 0; i < rows; i++) { 
    data[i] = input.nextString(); //modify with however you get a String in. 
} 
//Parse numbers 
for(int i = 0; i < rows; i++) { 
    dataValues[i] = data[i].split(','); 
    if(dataValues[i].length != columns) { 
     throw Exception("Wrong number of columns for row: "+i); 
    } 
    for(int j = 0; j < columns; j++) { 
    try { 
     board[row][column] = Integer.parseInt(dataValues[i][j]); 
    } catch (NumberFormatException e) { 
     throw Exception("Invalid value for ["+i+","+j+"]"); 
    } 
    } 
} 
+0

喜弘,首先非常感謝您的回答。我的意思是用戶必須提供許多行和列(2x2,4x4,9x9 ...)。相比之下,他必須逐行或逐列輸入以逗號分隔的數據。如果他有三行,那麼他必須輸入三行,每行將包含三個以逗號分隔的元素。我必須將每個元素放置在二維數組中(board [] [])。之後,我將不得不這樣做一些計算,如檢查列中是否有重複的數字,但另一個問題:) – Oksana

+0

聽起來像一個縮小的數獨:-)更新我的回答 –

+0

Yeap,它是一個輕量級的數獨: ) – Oksana

0

您已經聲明您首先接受行數和列數,因此在開始閱讀之前電話號碼,你知道表格的尺寸。

所以你真的只需要邏輯來更新行或列,這取決於你所運行的模式。下面是一個不完整的例子。

希望它給你一個關於如何完成它的想法。

public class SplitInputExample { 

    int maxRows = 3; 
    int maxCols = 3; 
    int board[][] = new int[3][3]; 
    boolean interpretRowise = true; 

    private void initialize() { 
     for (int i = 0; i < maxRows; i++) { 
      board[i] = new int[maxCols]; 
     } 

    } 

    /** 
    * 
    * @param string input 
    * @param rowByRow if true, parse row by row, if false parse col by col 
    * @param index index of row/col to update i.e if updating 2nd col, pass 1 
    */ 
    public void split(String string, boolean rowByRow, int index) { 

     int limit = 0; 
     if (rowByRow){ 
      limit = maxRows; 
     }else{ 
      limit = maxCols; 
     } 
     String splitArray[] = string.split(","); 
     int inputArray[] = new int[splitArray.length]; 

     //save the parsed input in an int array 
     int counter = 0; 
     for (String s: splitArray){ 
      inputArray[counter++] = Integer.parseInt(s); 
     } 

     //checks for ensuring input array is of equals rows/cols 
     for (int i=0; i<limit; i++){ 
      //either check rows or cols 
      if (rowByRow){ 
       //fix the row 
       board[index][i] = inputArray[i]; 
      }else{ 
       //fix the col 
       board[i][index] = inputArray[i]; 
      } 
     } 

    } 

    public void print(){ 
     for (int row[]: board){ 
      for(int col: row){ 
       System.out.print(col); 
      } 
     } 
    } 

    public static void main(String args[]) { 
     String input[] = { 
      "1,2,3", 
      "4,5,6", 
      "7,8,9" 
     }; 
     SplitInputExample example = new SplitInputExample(); 

     int index = 0; 
// parse row-wise i.e 1,2,3 are values of row 0, for each column 
     for (String s: input){ 
      example.split(s,true,index++); 
     } 
     example.print(); 
     System.out.println("\n"); 

     index = 0; 
//reset index to 0 and parse column-wise i.e 1,2,3 are values of column 0 for each row 
     for (String s: input){ 
      example.split(s,false,index++); 
     } 

     example.print(); 
    } 
} 
+0

謝謝!我認爲這個可能會起作用:) – Oksana

0

這裏是解決方案:

public static int getInputByRow(int[][] board, int v) 
{ 

      for (int i=0; i<board.length; i++){ 
       int j=0;   
       String line = myInput.next(); 
       String[] splitarr = line.split(","); 

         for (String num : splitarr){       
           v = Integer.parseInt(num); 
           board[i][j++] = v; 
         } 
} 
+0

也添加一些解釋總是好的,而不僅僅是一堆代碼行。請注意,第一個'for'沒有關閉(或者''方法'取決於視圖),它沒有關閉'}'。 – Zabuza