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
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.
/** Only accepts scanners with up to 1000 tokens. @throws IndexOutofBounds Exception when Scanner has more than 1000 tokens. */
- we set the size to 1000 for the reason that we don't know how many lines is there on the file String [] toTokenArray(Scanner in) String[] arr = new String[1000] // make size big enough int i = 0; while (in.hasNext()) { String s = in.Next(); arr[i] = s; i = i + 1; } return Arrays.copyOf(arr, i);
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();
}
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started