Results 1 to 3 of 3
  1. #1
    Kris3579 is offline Member
    Join Date
    Dec 2013
    Posts
    6
    Rep Power
    0

    Question Switch statement altering integer variable...help

    Java Code:
        protected void SetData() {
            for (int _i = 127; _i > -1; _i--) {
                dat[_i] = tData[tIndex+_i];
            }
            
            for (int _i = 0; _i < 128; _i++) {
                SetBlock(dat, _i);
            }
        }
    
        protected void SetBlock(byte[] _blocks, int _index) {
            switch (_blocks[_index]) {
                case (byte) 0x00: 
                    for (int _y = 0; _y < 64; _y++) {
                        for (int _x = 0; _x < 64; _x++) {
                            segBitmap.setPixel(_x, (tHeight*_index)+_y, Color.parseColor("#00E0FF"));
                        }
                    }
                break;
                
                default:
                    if (_index > -1) {
                AlertDialog.Builder alert = new AlertDialog.Builder(mainActivity);
                alert.setCancelable(true);
                alert.setPositiveButton("OK", null);
                alert.setMessage(Integer.toString(_index));
                alert.create().show();
            }
                break;
            }
        }
    Hello, the _index variable = 0 outside switch statement but = 127 in statement.I would like to know the problem. Please help.
    Last edited by Kris3579; 12-23-2013 at 09:59 AM.

  2. #2
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: Switch statement altering integer variable...help

    What do you mean? Add some print statements to check your assumption about what the value actually is. It looks like you're calling that method and running that switch statement 128 times with _index values between 0 and 127, inclusive. And you've populated your array with values from some other array, so we can't tell from this code what the value of _blocks[_index] will be for any given value of _index.

    A couple other tips: you're calling Color.parseColor("#00E0FF") inside your loop. Better to call it once and reuse the result. And take a look at the link in my sig.
    Get in the habit of using standard Java naming conventions!

  3. #3
    Kris3579 is offline Member
    Join Date
    Dec 2013
    Posts
    6
    Rep Power
    0

    Wink Re: Switch statement altering integer variable...help

    Quote Originally Posted by kjkrum View Post
    What do you mean? Add some print statements to check your assumption about what the value actually is. It looks like you're calling that method and running that switch statement 128 times with _index values between 0 and 127, inclusive. And you've populated your array with values from some other array, so we can't tell from this code what the value of _blocks[_index] will be for any given value of _index.

    A couple other tips: you're calling Color.parseColor("#00E0FF") inside your loop. Better to call it once and reuse the result. And take a look at the link in my sig.
    Thank you for your reply, but I already solved the problem by reversing the loops.

Similar Threads

  1. Replies: 3
    Last Post: 11-19-2012, 09:12 PM
  2. The Switch Statement
    By Everyman in forum New To Java
    Replies: 11
    Last Post: 04-20-2012, 04:29 PM
  3. Replies: 2
    Last Post: 03-26-2012, 05:27 PM
  4. Replies: 7
    Last Post: 02-14-2012, 11:12 AM
  5. Switch Statement Help
    By bluegreen7hi in forum New To Java
    Replies: 6
    Last Post: 02-06-2008, 06:16 AM

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
  •