Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Javascript problem - segment of code, that is nested within other code which pro

ID: 3607114 • Letter: J

Question

Javascript problem - segment of code, that is nested within other code which properly inputs a user prompted file. This part of the code is attempting to set the first three int's in a file to three variable, then set the remaining 6 into a 2D array as: [2][1] //next array row [8][7] //next array row [4][3]

So that I have an array that has two columns and as many rows as specified by the third integer taken in (variable NumBlocks) but I keep having a problem where I end up with parts of the array having index out of bounds exceptions: "StringIndexOutOfBoundsException: String index out of range: 0" This is the code with a print statement where I tried to troubleshoot, but from a file that looks like this:

9 6 3
2 1
8 7
4 3

My print statement looks like this: 2 18 7.

Below the print statment code is the code in which I get the odd printing:

static void readData(Scanner input) {
int TargetRect_w = input.nextInt();
int TargetRect_h = input.nextInt();
int NumBlocks = input.nextInt();
for (int x = 0; x < NumBlocks; x++) {
String Rectangle_line = input.nextLine();
Rectangle_line = Rectangle_line.replace("/ /g","");
System.out.print(Rectangle_line);
}
}

String exception error code:

static void readData(Scanner input) {
int TargetRect_w = input.nextInt();
int TargetRect_h = input.nextInt();
int NumBlocks = input.nextInt();
for (int x = 0; x < NumBlocks; x++) {
String Rectangle_line = input.nextLine();
Rectangle_line = Rectangle_line.replace("/ /g","");

int width = 2;

       int height = (NumBlocks*2);

       char rect[][] = new char[height][width];

       int charCount = 0;

       for (int c = 0; c < width; c++) {

          for (int r = 0; r < height; r++) {

                rect[r][c] = Rectangle_line.charAt(charCount);

                charCount += 1;

             }

          }

     System.out.println( rect );

     }

     }

}

}

Shouldn't the attempted fix code be at least printing as: 21 87 43, as I'm replacing all spaces (but not all whitespace - i.e. new lines)?

And then is this odd offset what's causing the arrays to index as out of bounds? ALSO - this is not a compiling error b/c of braces, trying to type this over from my java file I may have missed some of the end braces so that is not noteworthy for commenting on.

Explanation / Answer

Below mentioned code will at least get you to the array, I don't know what you want to store in this array.

static void readData(Scanner input) {

String firstLine = input.nextLine();

String[] parts = firstLine.split(" ");

int TargetRect_w = Integer.valueOf(parts[0]);

int TargetRect_h = Integer.valueOf(parts[1]);

int NumBlocks = Integer.valueOf(parts[2]);

for (int x = 0; x < NumBlocks; x++) {

String rectLine = input.nextLine();

parts = rectLine.split(" ");

int height = Integer.valueOf(parts[0]);

int width = Integer.valueOf(parts[1]);

System.out.println("Height: "+height+", Width: "+width);

char rect[][] = new char[height][width];

}

}

Explanation: system.out.print removes nextline character thats why you are getting 2 18 7 as result and when you read first line its blank as the nextline character from first line is still remaining. Because nextLine() reads a line till nextline character. Thats why first line is blank, for example data looks like this :- 9 6 3 <nextline> 2 1 <nextline>.

I hope this clears your doubt.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote