Question: JAVA! I would like to make a method that read lines from the Scanner until there are no more lines to read. Store each line
JAVA!
I would like to make a method that read lines 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.
We are given this code to guide us in making this metho but I'm not sure how would I apply this in my code so far.
String[] arr = new String[1000] // make size big enough, since we don't know how many lines there would be in the file int i = 0; while (in.hasNext()) { String s = in.Next(); arr[i] = s; i = i + 1; } return Arrays.copyOf(arr, i); This is my code:
public void readFile(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();
}
}
Source: public void readWorld(Scanner in) - readWorld should read lines from the Scanner until there are no more lines to read. Each line will represent a row in the robot's world. The line is allowed to have whitespace characters (spaces and tabs) and the four characters (r, w, b, t). The total number of (r, w, b, t) characters found should be the same on each line, and this number indicates the number of columns in the robot's world. The input lines should have at least one row and at least one column. There should be exactly one r. If any of these specifications are violated or in is null an IllegalArgumentException should be thrown.
Any hints please. Thank you.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
