Ich fest auf ein Problem mit meinem Java-Programm in, wenn ich Zahlen eingeben wird Index Fehler außerhalb der Grenzen zurückgegeben. Die Linie ist die, an der sie sich verfängt.Java 2D-Array mit Benutzereingabe
arrayName[row][col] = holder;
jede Hilfe bei der Ermittlung des Problems wäre am hilfreichsten. vollständiges Programm unter
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;
}
}
'if (Acol <= col)' wahrscheinlich bist du Mission einig '' '{}' Klammern (Ihr Einrücken sagt mir, dass) – Frakcool
Bitte einen Kommentar mit '// Zeile 66' am rechten Ort;) Wir wollen nicht zählen. –
Voting zum Schließen als wahrscheinlicher Tippfehler. –