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

Any help or hint please. THANK YOU! I would like to make a method that read line

ID: 3587898 • Letter: A

Question

Any help or hint please. THANK YOU!

I would like to make a method that read lines off a file from the Scanner until there are no more lines to read. Store each line into a temporary String variable and process that string. Main goal for this method is to make a "2D char array" from it. Each line read represents the row of the array while the total number of characters found on the line indicates the column. I also need to throw an IllegalArgumentException of any of these are violated: 1) Line should only contain characters (r,b,w,t) and should only contain one 'r'. 2) The total number of characters should be the same on each line.

This is my code so far:

public void readWorld(Scanner in) {

while (in.hasNextLine()) { // check for next line

String text = in.nextLine(); // read next line

Scanner readLine = new Scanner(text);

while (readLine.hasNext()){ // read each token of the line

String token = readLine.next(); } }

Explanation / Answer

public void readWorld(Scanner in) {
List<Character[]> charArrList = new ArrayList<>();
int lineLength = 0;
boolean firstLine = true;
while (in.hasNextLine()) { // check for next line
String text = in.nextLine(); // read next line
if (!firstLine && text.length() != lineLength) {
throw new IllegalArgumentException("Different length line found");
}
else {
firstLine = false;
lineLength = text.length();
}
char[] arr = text.toCharArray();
int countR = 0;
for (int i = 0; i < arr.length; i++) {
if (arr[i] == 'r' || arr[i] == 'b' || arr[i] == 'w' || arr[i] == 't') {
if (arr[i] == 'r' && countR == 1) {
throw new IllegalArgumentException("Found more than 1 r");
}
} else {
throw new IllegalArgumentException("Char other than r, b, w, t found");
}
}
}
int size = charArrList.size();
char[][] array = new char[size][lineLength];
for (int i = 0; i < size; i++) {
Character[] temp = charArrList.get(i);
for (int j = 0; j < lineLength; j++) {
array[i][j] = temp[j];
}
}
}

Here is the method which does exactlly you wnat but as your function signature is void so I don't kow what you wnat to do with array variable here which is a 2D char array.

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