2017-01-18 1 views
-2

im은 숫자를 입력 할 때 내 Java 프로그램에서 문제가 발생하여 색인이 범위를 벗어난 오류를 반환합니다. 선은 66의 wheres 그 위로 붙잡히고있다.사용자 입력이있는 Java 2D 배열

arrayName[row][col] = holder; 

문제를 파악하는 데 도움이 될 것입니다.

package workfiles; 

    import java.util.*; 
    import java.util.Scanner; 

public class prob2 { 

// Do not modify this method 
public static void main(String[] args) { 

    try 
    { 
     int [][] iArray = enter2DPosArray(); 
     System.out.println("The original array values:"); 
     print2DIArray(iArray); 
     int [][] tArray = transposition(iArray); 
     System.out.println("The transposed array values:"); 
     print2DIArray(tArray); 
    } 

    catch (InputMismatchException exception) 
    { 
     System.out.println("The array entry failed. The program will now halt."); 
    } 

} 

    // A function that prints a 2D integer array to standard output 
    // It prints each row on one line with newlines between rows 
    public static void print2DIArray(int[][] output) { 

    } 

// A function that enters a 2D integer array from the user 
// It raises an InputMismatchException if the user enters anything other 
// than positive (> 0) values for the number of rows, the number of 
// columns, or any array entry 
public static int[][] enter2DPosArray() throws InputMismatchException { 

    int row=0; 
    int col=0; 
    int arow=0; 
    int acol=0; 
    int holder=0; 
    Scanner numScan = new Scanner(System.in); 

    while (row<=0){ 
     System.out.print("How many rows (>0) should the array have? "); 
     row = numScan.nextInt(); 
    } 

    while (col<=0){ 
     System.out.print("How many columns (>0) should the array have? "); 
     col = numScan.nextInt(); 
    } 
    int[][] arrayName = new int[row+1][col+1]; 

    while (arow < row) { 

     if (acol<=col) 
      System.out.println("Enter a positive (> 0) integer value: "); 
      holder = numScan.nextInt(); 
    // !!!line 66 begins right here!!! 
      arrayName[arow][acol] = holder; 
      acol ++; 

     if (acol>col) 
      acol=0; 
      arow ++; 
      System.out.println("Enter a positive (> 0) integer value: "); 
      holder = numScan.nextInt(); 
      arrayName[arow][acol] = holder; 
      acol ++; 



    } 
    //arrayName[i][j] 
    numScan.close(); 
    return arrayName; 
} 

public static int[][] transposition(int [][] arrayName) { 

    int r=0, c=0; 

    int[][] transpose = new int[r][c]; 
    for (int i = 0; i < r; i++) { 
     for (int j = 0; j < c; j++) { 
      transpose[i][j] = arrayName[j][i]; 
     } 
    } 
    return transpose; 
} 

} 
+4

'경우 (ACOL <= COL)'아마 당신은 임무있어 일부'{''}'중괄호 – Frakcool

+0

이 오른쪽에'// 라인 66'에 댓글을 추가하십시오 (당신의 들여 쓰기 하더군요) 장소;) 우리는 세지 않으려 고합니다. –

+0

투표가 가능한 오타로 닫습니다. –

답변

-1

아래 전체 프로그램이 시도 보내기

if (acol <= col) { 
     System.out.println("Enter a positive (> 0) integer value: "); 
     holder = numScan.nextInt(); 
     arrayName[arow][acol] = holder; 
     acol++; 
    } 
    if (acol > col) { 
     acol=0; 
     arow ++; 
     System.out.println("Enter a positive (> 0) integer value: "); 
     holder = numScan.nextInt(); 
     arrayName[arow][acol] = holder; 
     acol++; 
    } 
+0

여전히 같은 문제가 발생하고 있지 않습니다. –

+0

확인할 수 없습니다. 귀하의 코드와 제안을 통해 AIOOB 예외 (행, cols = 3)를 관찰하지 않습니다. 단지 너무 많은 값 (9 개 이상) –

0

내 컴퓨터에 이클립스를 다시 설치 한 제거를하는 동안 제로로 설정 값을 유지하고, 그렇게 한 Statment 경우에 변경 루프, 한 번만 일어난다, 어떻게 또는 왜 그것이 범위를 벗어난 오류를 실행했다 설명 할 수 없지만, 프로그램이 올바르게 지금 작동합니다.

package workfiles; 



    import java.util.*; 
    import java.util.Scanner; 

    public class hw2 { 

    // Do not modify this method 
    public static void main(String[] args) { 

    try 
    { 
     int [][] iArray = enter2DPosArray(); 
     System.out.println("The original array values:"); 
     print2DIArray(iArray); 
     int [][] tArray = transposition(iArray); 
     System.out.println("The transposed array values:"); 
     print2DIArray(tArray); 
    } 

    catch (InputMismatchException exception) 
    { 
     System.out.println("The array entry failed. The program will now halt."); 
    } 

} 

    // A function that prints a 2D integer array to standard output 
    // It prints each row on one line with newlines between rows 
    public static void print2DIArray(int[][] output) { 
    int iArray[][]; 

     for (int row = 0; row < iArray.length; row++) { 
      for (int column = 0; column < iArray[row].length; column++) { 
       System.out.print(iArray[row][column] + " "); 
      } 
      System.out.println(); 
     } 
    } 



// A function that enters a 2D integer array from the user 
// It raises an InputMismatchException if the user enters anything other 
// than positive (> 0) values for the number of rows, the number of 
// columns, or any array entry 
public static int[][] enter2DPosArray() throws InputMismatchException { 

    int row=0; 
    int col=0; 
    int arow=0; 
    int acol=0; 
    int holder; 
    Scanner numScan = new Scanner(System.in); 

    while (row<=0){ 
     System.out.print("How many rows (>0) should the array have? "); 
     row = numScan.nextInt(); 
    } 

    while (col<=0){ 
     System.out.print("How many columns (>0) should the array have? "); 
     col = numScan.nextInt(); 
    } 
    int[][] iArray = new int[row][col]; 

    while (arow < row) { 

     while (acol < col) { 
      System.out.println("Enter a positive (> 0) integer value: "); 
      holder = numScan.nextInt(); 
      iArray[arow][acol] = holder; 
      acol++; 
     } 

    //this is where i replaced the while loop for the if statment, in the while loop it kept on resetting the acol value. there was no need to loop that part of the program.  

    if (acol >= col) { 
      acol = 0; 
      arow ++; 

     } 



    } 
    //arrayName[i][j] 
    numScan.close(); 
    return iArray; 
} 

public static int[][] transposition(int [][] iArray) { 

    int r=0, c=0; 

    int[][] transpose = new int[r][c]; 
    for (int i = 0; i < r; i++) { 
     for (int j = 0; j < c; j++) { 
      transpose[i][j] = iArray[j][i]; 
     } 
    } 
    return transpose; 
    } 

}