public void readArray(Scanner in) - the method should read lines from the Scanne
ID: 3585373 • Letter: P
Question
public void readArray(Scanner in) - the method should read lines from the Scanner until there are no more lines to read. Each line will represent the row of the array. The line is allowed to have white spaces or tabs, and should only have four characters r,w,b,t. The total no. characters found on the line should be the same on each line and this no. indicates the column of the array. There should only be one 'r'. If any of these specifications are violated or in is null an IllegalArgumentException should be thrown.
- so what would happen basically is read off a file. Store each line in a temporary string variable, do an error checking and make a 2D char array using it.
We are given this code that would guide us in making this method, but I'm still confused how to do it.
An example of file to be read would look like this. w w w w
w w r t
This is what I've got so far where it reads a line, and also examines each token of the line. It's really just a small portion of the code. But now, I just don't know what to do next.
Any help or hint please. Thank you.
public void readArray(Scanner in) {
while (in.hasNextLine()) {
String text = in.nextLine();
Scanner readLine = new Scanner(text);
while (readLine.hasNext()){
String token = readLine.next();
}
}
}
Explanation / Answer
Let's modify the original implementation
String[] chars = s.split("\s+");
//set no of cols
if (noOfCols == -1)
noOfCols = chars.length;
//check if no of colums are not the same
if (noOfCols != chars.length)
throw new IllegalArgumentException("No of columns are not same");
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.