Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Javascript problem - segment of code, that is nested within other code which properly inputs a user prompted file. This part of the code is

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.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Databases Organizing Information Digital And Information Literacy

Authors: Greg Roza

1st Edition

1448805929, 978-1448805921

More Books

Students also viewed these Databases questions