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

Javascript Problem static void readData(Scanner input) { int TargetRect_w = inpu

ID: 3606850 • Letter: J

Question

Javascript Problem

static void readData(Scanner input) {
int TargetRect_w = input.nextInt();
int TargetRect_h = input.nextInt();
int NumBlocks = input.nextInt();
while (input.hasnext()) {
String Rectangle_line = input.nextLine();
Rectangle_line = Rectangle_line.replace(/s/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.get(charCount);
charCount += 1;
}
}

This is a segment of javascript code that I'm having trouble finding the problem with, im getting 6 errors in the line:

Rectangle_line = Rectangle_line.replace(/s/g,"");

at various points, exact errors:

Block_V21.java:57: error: illegal start of expression

   Rectangle_line = Rectangle_line.replace(/s/g,"");

   ^

Block_V21.java:57: error: illegal character: ''

   Rectangle_line = Rectangle_line.replace(/s/g,"");

^

Block_V21.java:57: error: ';' expected

   Rectangle_line = Rectangle_line.replace(/s/g,"");

   ^

Block_V21.java:57: error: illegal start of expression

   Rectangle_line = Rectangle_line.replace(/s/g,"");

^

Block_V21.java:57: error: <identifier> expected

   Rectangle_line = Rectangle_line.replace(/s/g,"");

   ^

Block_V21.java:57: error: illegal start of expression

   Rectangle_line = Rectangle_line.replace(/s/g,"");

   ^

6 errors

This isn't the complete code because I'm more looking for an expalination of what's wrong with this particular segment of code, I'm basically trying to take an input file, read it and create a 2D array of the numbers inside the file. The file has lines of 2 numbers i.e. "2 3" but with spacing which I need to eliminate (I think) because I keep getting a "string index out of bounds" error if I don't attempt to remove the spacing. The code before this part works correctly (as I'm able to print content from the file I'm reading in and if there's a file error an exception is thrown) however, I can't seem to create the array I want/remove spaces in order to do so and was hoping to get an explaination of which this particualr bit of code doesn't work/won't compile. I'm relatively new to javascript so this very well may be a syntax error that is easily understood to be invalid, but I just don't see it. Thanks for any help/explaination.

Explanation / Answer

Hi,

I have fixed all issues and highlighted the code chnages below

static void readData(Scanner input) {

int TargetRect_w = input.nextInt();

int TargetRect_h = input.nextInt();

int NumBlocks = input.nextInt();

while (input.hasNext()) {

String Rectangle_line = input.nextLine();

Rectangle_line = Rectangle_line.replace("\s/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;

}

}

}

}

In hasNext method letter 'N" must be upper case.

In String class, we have replace method with two parameters not one.

in String class, we dont have get() method for getting each character. we should use charAt()

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