# Thread: Can someone help me out?

1. Member
Join Date
Apr 2011
Posts
8
Rep Power
0

## Can someone help me out?

So for my computer science class, we are supposed to "program a simple toy robot that walks up and down a hallway picking up toys on each tile"

We were given this as the text file for the scanner:
8, 1, 3
1, 0, 1, 1, 0, 2, 6
>

The ">" represents the direction the robot travels down the hall, and starting position. So the hallway would look like this:
8 1 3 1 0 1 1 0 2 6
>

So there are 8 toys in the first tile, 1 in the second, etc. and the robot starts facing right in the 10th tile.

Anyways, what my plan to do is this: -make a string array list
-find the bracket and it's position/direction through the array list
-remove the bracket from the list
-convert that list into an integer array list via parsing or something
-then make a loop to remove until the starting tile is zero and then make it move in the given direction.

The only two things that I need to put in the final output would be number of moves (a move either being switching tiles, or removing a toy) and final direction.

Here's my code right now:

import java.io.* ;
import java.util.* ;
import java.util.ArrayList ;
public class ToyRobot
{
public static void main (String[] args) throws IOException
{
File myFile = new File("robotdata.txt");
Scanner sc = new Scanner (myFile);
ArrayList<String> stringList = new ArrayList<String>();
int count = 0;
int start = 0;
String direction = "";
while(sc.hasNext())
{
String value = sc.next();
value = value.replaceAll(",","");
}
System.out.println(stringList);
for(int j=0; j<stringList.size();j++)
{
if(stringList.get(j) == "<")
{
start = j-1;
stringList.remove(stringList.get(j));
}
else if(stringList.get(j) == ">")
{
start = j-1;
stringList.remove(stringList.get(j));
}
System.out.println(stringList);
}
}
}

Whenever I run the program, it doesn't do anything with removing the bracket from the array list, which I need to do so I can change everything to an integer.

Also, would my plans for this project even work? To me it seems......well, rudimentary to say the least.

2. Member
Join Date
Apr 2011
Posts
8
Rep Power
0
ughghghgh, my indentation is missing, it was there when i pasted my code into the form....sorry about that, guys.

Oh, that bracket is supposed to be under the 6 in my hallway example

3. Wrap the code in code tags,
[code ]<-omit space
[/code]
this way your code will keep it's indentation.

4. Member
Join Date
Apr 2011
Posts
8
Rep Power
0
oh, okay, thanks.

Java Code:
```8 1 3 1 0 1 1 0 2 6
>```
Here's my code indented:
Java Code:
```import java.io.* ;
import java.util.* ;
import java.util.ArrayList ;
public class ToyRobot
{
public static void main (String[] args) throws IOException
{
File myFile = new File("robotdata.txt");
Scanner sc = new Scanner (myFile);
ArrayList<String> stringList = new ArrayList<String>();
int count = 0;
int start = 0;
while(sc.hasNext())
{
String value = sc.next();
value = value.replaceAll(",","");
}
System.out.println(stringList);
for(int j=0; j<stringList.size();j++)
{
if(stringList.get(j) == "<")
{
start = j-1;
stringList.remove(stringList.get(j));
}
else if(stringList.get(j) == ">")
{
start = j-1;
stringList.remove(stringList.get(j));
}
System.out.println(stringList);
}
}
}```

5. Member
Join Date
Apr 2011
Posts
8
Rep Power
0
:'(

anyone?

6. Senior Member
Join Date
Jun 2008
Posts
339
Rep Power
10
I didn't understand your explanation of the task, so I can't help with the algorithm, but if you have some particular Java difficulty, I'll try to help.

7. Member
Join Date
Apr 2011
Posts
8
Rep Power
0
well, my goal is to get the position and direction of the bracket thingy, take it out of the array list, and then convert that string array list into an integer array (which shouldn't be hard, i'll just make a new array, get .size() of the list, and parse each individual String into the new integer array.

I'm having trouble with the bracket part. In my for loop in the code, that's where it should be checked, but nothing is happening. And I kind of need that to get done to finish the rest of the program.

So if you or anone can help, that'd be wonderful.

8. Senior Member
Join Date
Jun 2008
Posts
339
Rep Power
10
Originally Posted by Green_Ghoul
I'm having trouble with the bracket part. In my for loop in the code, that's where it should be checked, but nothing is happening.
Well if you can explain exactly what you're trying to do with the 'bracket part' (and what that actually is), and what's going wrong, and perhaps what 'checking it' actually involves...

It would probably simplify things if you could extract the part that's causing difficulties and express it as a separate problem - maybe provide an SSCCE.

As I said before, I didn't understand the task you described, so I don't know what that code you posted is trying to do.

9. If your goal is to take one array list of strings which contains a < or > and convert all the remaining items into numbers and store them in a new arraylist of integers you can do this almost trivially with indexOf and remove.

10. Do not use "==" to compare two String contents. Use "string.equals(another)" to compare for content equality.

11. Member
Join Date
Apr 2011
Posts
8
Rep Power
0
the bracket, as seen in the given text file in my first post, is either ">" or "<" > means the robot is moving right, < means moving left. If the text file is 8, 1, 6, >, 5, 3 the robot starts facing right and starts on the third tile, of 5. I'm looping through the array list that I made, and I need to check the direction and position of the bracket, and then remove it to move on. What is "SSCCE"?

Anyways, the code is some project for my computer science class. I'm trying to describe it as best I can. Basically a robot picks up toys and I need to find how many moves he makes and the ending direction he's in at the end. 8, 1,6 would mean 8 toys in tile one, 1 toy in tile two, and 6 toys in tile three.

12. Senior Member
Join Date
Jun 2008
Posts
339
Rep Power
10
OK, it sounds simpler than I thought... IIUIC, you have a line of text containing numbers separated by spaces, and embedded in it is a '<' or a '>' character. You want to find out which character is embedded and where, then remove it - yes?

If so, sunde887's String method suggestions should do the trick. If not, or if you don't understand those String methods, please explain.

The SSCCE was a link that explained itself - to get the most out of these forums, you need a browser that allows you to follow links. An SSCCE is a Simple Self-Contained Correct (or Compilable) Example.