Results 1 to 3 of 3

Thread: weird problem

  1. #1
    GPB
    GPB is offline Member
    Join Date
    Feb 2010
    Posts
    20
    Rep Power
    0

    Default weird problem

    I'm having kind of a weird problem. The exercise is to have a string like 'wrssllssrs..' be used to form a track, where 'w' = walk/empty field; 's' = straight; 'r' = right turn; 'l' = left turn.
    I use this to call it:
    Java Code:
    new Track(int_height,int_width,String_path);
    For example: 'wrssllssrs..' would become:


    To do this I basically 'follow' the track.
    What I try to do is to read the string character by character and based on the current direction put the right kind of track into a 2-d array. Also I update the x and y coordinates after each read character (or piece of track), so that it will go to the correct next position.

    Below is my error message and code. The red lines are the lines from the error message, and the orange ones are the ones I put in to check where exactly in the program it's failing. It shows the x and y coordinates on which it is currently on and the letter that has been read.

    Now I believe the error is caused by something else, because as you can see from the output, the 3rd coordinate is (0,0), when it should be (1,1). That means it does x--, in stead of y++, which in turn would mean that at the 'r' it picks the South direction (marked blue), instead of the correct East direction (marked green).

    Any help on this would be greatly appreciated.

    Java Code:
    run:
    0 0
    w
    1 0
    r
    0 0
    s
    0 -1
    s
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
            at javaapplication3.Track.<init>(Track.java:43)
            at javaapplication3.Main.main(Main.java:18)
    Java Result: 1
    BUILD SUCCESSFUL (total time: 0 seconds)


    Java Code:
    public class Track {
        
        enum TrackDirection { Em , NS, EW, NE, NW, SE, SW, Cr }; // Em = Empty Field
                                                                // Cr = Crossroads
        enum direction { North , East , South , West };
        
        private direction direction_current;
        
        public Track (int height, int width, String path) {
            direction_current = direction.East;
            TrackDirection[][] track = new TrackDirection[width][height];
            for ( int a = 0 ; a < height ; a++ ) {
                for ( int b = 0 ; b < width ; b++ ) {
                    track[b][a] = TrackDirection.Em;
                }
            }
            int x = 0;
            int y = 0;
            for ( int i = 0 ; i < path.length() ; i++ ) {
                char ch = path.charAt(i);
                [COLOR="Orange"]System.out.println(x+" "+y);[/COLOR]
                switch (ch) {
                    case 'w':  [COLOR="Orange"]System.out.println(ch);[/COLOR]
                               track[x][y] = TrackDirection.Em;
                               if ( direction_current == direction.North ) {y--;}
                               if ( direction_current == direction.East )  {x++;}
                               if ( direction_current == direction.South )  {y++;}
                               if ( direction_current == direction.West )  {x--;}
                               break;
                    case 's':  [COLOR="Orange"]System.out.println(ch);[/COLOR]
                               if ( direction_current == direction.North ) {
                                   [COLOR="Red"]if ( track [x][y] == TrackDirection.EW ) {[/COLOR]
                                       track[x][y] = TrackDirection.Cr;
                                   }
                                   else {
                                       track[x][y] = TrackDirection.NS;
                                   }
                                   y--;
                               }
                               if ( direction_current == direction.East ) {
                                   if ( track[x][y] == TrackDirection.NS ) {
                                       track[x][y] = TrackDirection.Cr;
                                   }
                                   else {
                                       track[x][y] = TrackDirection.EW;
                                   }
                                   x++;
                               }
                               if ( direction_current == direction.South ) {
                                   if ( track[x][y] == TrackDirection.EW ) {
                                       track[x][y] = TrackDirection.Cr;
                                   }
                                   else {
                                       track[x][y] = TrackDirection.NS;
                                   }
                                   y++;
                               }
                               if ( direction_current == direction.West ) {
                                   if ( track[x][y] == TrackDirection.NS ) {
                                       track[x][y] = TrackDirection.Cr;
                                   }
                                   else {
                                       track[x][y] = TrackDirection.EW;
                                   }
                                   x--;
                               }
                               break;
                    case 'l':  [COLOR="Orange"]System.out.println(ch);[/COLOR]
                               if ( direction_current == direction.North ) {
                                   track[x][y] = TrackDirection.SW;
                                   direction_current = direction.West;
                                   x--;
                               }
                               if ( direction_current == direction.East )  {
                                   track[x][y] = TrackDirection.NW;
                                   direction_current = direction.North;
                                   y--;
                               }
                               if ( direction_current == direction.South )  {
                                   track[x][y] = TrackDirection.NE;
                                   direction_current = direction.East;
                                   x++;
                               }
                               if ( direction_current == direction.West )  {
                                   track[x][y] = TrackDirection.SE;
                                   direction_current = direction.South;
                                   y++;
                               }
                               break;
                    case 'r':  [COLOR="Orange"]System.out.println(ch);[/COLOR]
                               if ( direction_current == direction.North ) {
                                   track[x][y] = TrackDirection.SE;
                                   direction_current = direction.East;
                                   x++;
                               }
                               [COLOR="YellowGreen"]if ( direction_current == direction.East )  {
                                   track[x][y] = TrackDirection.SW;
                                   direction_current = direction.South;
                                   y++;
                               }[/COLOR]
                               [COLOR="DeepSkyBlue"]if ( direction_current == direction.South )  {
                                   track[x][y] = TrackDirection.NW;
                                   direction_current = direction.West;
                                   x--;
                               }[/COLOR]
                               if ( direction_current == direction.West )  {
                                   track[x][y] = TrackDirection.NE;
                                   direction_current = direction.North;
                                   y--;
                               }
                               break;
                    default :  track[x][y] = TrackDirection.Em;break;
                }
    
            }
            for ( int y_2 = 0 ; y_2 < height ; y_2++ ) {
                for ( int x_2 = 0 ; x_2 < width ; x_2++ ) {
                    System.out.println(track[x_2][y_2]);
                }
            }
        }
    
    }
    Last edited by GPB; 02-26-2010 at 03:33 PM.

  2. #2
    GPB
    GPB is offline Member
    Join Date
    Feb 2010
    Posts
    20
    Rep Power
    0

    Default

    small bump before i go to bed :)

  3. #3
    GPB
    GPB is offline Member
    Join Date
    Feb 2010
    Posts
    20
    Rep Power
    0

    Default

    solved, it had to do with needing to put else{} statements after the if's in the 'l' and 'r' cases in the switch. because they would change the direction, after which the next if{}statement could also be true.

Similar Threads

  1. Replies: 6
    Last Post: 02-20-2010, 04:27 AM
  2. Weird msdos problem
    By dudejonne in forum New To Java
    Replies: 6
    Last Post: 11-02-2009, 09:39 PM
  3. Weird path problem when reading properties file
    By jerry_popperq in forum New To Java
    Replies: 0
    Last Post: 03-18-2009, 04:32 PM
  4. Weird problem upon calling same function twice
    By alin_ms in forum New To Java
    Replies: 2
    Last Post: 12-20-2008, 07:14 PM
  5. Replies: 9
    Last Post: 09-30-2008, 08:30 AM

Posting Permissions

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