Results 1 to 12 of 12
  1. #1
    Green_Ghoul is offline Member
    Join Date
    Apr 2011
    Posts
    8
    Rep Power
    0

    Question 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(",","");
    stringList.add(value);
    }
    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. #2
    Green_Ghoul is offline Member
    Join Date
    Apr 2011
    Posts
    8
    Rep Power
    0

    Default

    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. #3
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

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

  4. #4
    Green_Ghoul is offline Member
    Join Date
    Apr 2011
    Posts
    8
    Rep Power
    0

    Default

    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(",","");
                stringList.add(value);
            }
            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. #5
    Green_Ghoul is offline Member
    Join Date
    Apr 2011
    Posts
    8
    Rep Power
    0

    Default

    :'(

    anyone?

  6. #6
    dlorde is offline Senior Member
    Join Date
    Jun 2008
    Posts
    339
    Rep Power
    6

    Default

    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. #7
    Green_Ghoul is offline Member
    Join Date
    Apr 2011
    Posts
    8
    Rep Power
    0

    Default

    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. #8
    dlorde is offline Senior Member
    Join Date
    Jun 2008
    Posts
    339
    Rep Power
    6

    Default

    Quote Originally Posted by Green_Ghoul View Post
    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. #9
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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. #10
    ra4king's Avatar
    ra4king is offline Senior Member
    Join Date
    Apr 2011
    Location
    Atlanta, Georgia, US
    Posts
    396
    Rep Power
    4

    Default

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

  11. #11
    Green_Ghoul is offline Member
    Join Date
    Apr 2011
    Posts
    8
    Rep Power
    0

    Default

    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. #12
    dlorde is offline Senior Member
    Join Date
    Jun 2008
    Posts
    339
    Rep Power
    6

    Default

    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.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •