I\'m really having trouble doing this program without an array list. If I create
ID: 3589458 • Letter: I
Question
I'm really having trouble doing this program without an array list. If I create an array with 1000 spaces I'll have left over spaces but the file size is unknown.
DO NOT USE AN ARRAY LIST
Write a class for both in java:
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.
public void readProgram(Scanner in)
readProgram should read lines from the Scanner until there are no more lines to read. For each line, you should first remove any comment and then any whitespace from the beginning and end of the de-commented string. (A comment begins with the first '#' and goes to the end of the line.) If the resulting string is empty the line can be ignored. Otherwise, the modified line should be used to construct a new SimpleBotStatement object. If the modified line is an illegal program statement, the construction of the SimpleBotStatement will fail with an IllegalArgumentException. Your readProgram method should not catch this exception and instead should allow it to continue to the caller of readProgram. You should document that an IllegalArgumentException is thrown if a program is illegally formatted.
Note that readWorld and readProgram can each be called more than once, but it is always the most recent call to either of these methods that defines what the robot's world and program.
public void start()
When start is called, a SimpleBotModel object should be created and the most recently read world and program should be passed to its constructor. If not both a world and program have been read, an IllegalStateException should be thrown
Explanation / Answer
Hi,
Here I've just solve the problem you are facing with array. Please let me know if you need more help.
Read.java
import java.util.Scanner;
public class Read {
public void readWorld(Scanner in)
{
//first we initializes array & tempArray with a single space
String array[]=new String[1];
String tempArray[]=new String[1];
String temp;
int index=1;
while(true)//this is a infinite loop to read all the lines
{
if (index == 1) //the if condition will execute only for the first time
{
array[index-1]=in.nextLine();//we are putting the string into array
tempArray=array;//copying the same in tempArray
index++;//increasing the index for next line to be inserted into the array
}
else
{
temp=in.nextLine();
if(temp.equals(null) || temp.equals("/"))//I'm using '/' as EOF
break;
else{
array=new String[index];//it will again initializes array with newly incremented index value
for(int i=0;i<tempArray.length;i++) //this will copy everything from tempArray to array
{
array[i]=tempArray[i];
}
array[index-1]=temp;//this will put the newly inserted line to the last of array
tempArray=array;//copying evrything in tempArray
index++;
}
}
}
/*here you can put the rest of your logic for robot*/
}
public void readProgram(Scanner in)//the initial logic to put string into array is same as the previous logic
{
String array[]=new String[1];
String tempArray[]=new String[1];
String temp;
int index=1;
while(true)
{
if (index == 1)
{
temp=in.nextLine();
array[index-1]=temp;
tempArray=array;
index++;
}
else
{
temp=in.nextLine();
if(temp.equals(null) || temp.equals("/"))
break;
else{
array=new String[index];
for(int i=0;i<tempArray.length;i++)
{
array[i]=tempArray[i];
}
array[index-1]=temp;
tempArray=array;
index++;
}
}
}
/*here you can put the rest of your logic for robot*/
}
}