Page 2 of 2 FirstFirst 12
Results 21 to 34 of 34
  1. #21
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

  2. #22
    bdario1 is offline Member
    Join Date
    Mar 2010
    Posts
    46
    Rep Power
    0

    Default

    Java Code:
    Character [] newlist = new Character[]{'c','b','d'};
            System.out.println("");
            System.out.print ("The array [");
            separator = "";
            for (int s : newlist)
            {
                System.out.print(separator + s);
                separator = ", ";
            }
            System.out.print ("] entered ");
            
            ArrayExercises.reverseOrder(newlist);
            System.out.print ("looks like this in reverse order[");
            separator = "";
            for (int s : newlist)
            {
                System.out.print(separator + s);
                separator = ", ";
            }
            System.out.print ("] ");
    this is the tester that should test the method and this is the method
    Java Code:
      public static void reverseOrder (Character[] symbols)
        {
        	for(int reverse = 0; reverse < symbols.length; reverse++)
            {
        		char c = symbols[reverse];
                System.out.print(c);
    
            }
        	
        }
    there is not too much in there since i am trying to figure it out how to do it but it prints this out

    The array [99, 98, 100] entered cbdlooks like this in reverse order[99, 98, 100]

  3. #23
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    You want to pint set of characters in reverse, like c, b, d as d, b, c is it? Or the integer values holds those characters?

    Check the following, and think.

    Java Code:
    for (int s : newlist)
            {
                System.out.print(separator + s);
                separator = ", ";
            }
    What you print, s. What's the type of it, int. So what is should print?

  4. #24
    bdario1 is offline Member
    Join Date
    Mar 2010
    Posts
    46
    Rep Power
    0

    Default

    Ok thanx good that works now it prints the newarray as the characters of the array i declared, now for the method is where i need the help on replacing the array elements in reverse order. so array {'a', 'b', 'c'} should be changed to be {'c', 'b', 'a'}

  5. #25
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Cool, now lets look at the loop in reverseOrder method. It's like follows,

    Java Code:
    for(int reverse = 0; reverse < symbols.length; reverse++) {
        		char c = symbols[reverse];
                    System.out.print(c);
            }
    Here you get the element,

    Java Code:
    char c = symbols[reverse];
    So reverse start from 0 to size of the array. Is it clear? What happen if we start from the last element. According to yours, not from the position at letter 'a' from the position at letter 'c'

  6. #26
    bdario1 is offline Member
    Join Date
    Mar 2010
    Posts
    46
    Rep Power
    0

    Default

    Java Code:
    for(int reverse = symbols.length; reverse > 0; reverse++) {
        		char c = symbols[reverse];
                    System.out.print(c);
            }
    Is this what you mean?

  7. #27
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Wow, you got it. Run the code and see. You comes with an error, don't worry. Give a try first.

  8. #28
    bdario1 is offline Member
    Join Date
    Mar 2010
    Posts
    46
    Rep Power
    0

    Default

    Yea i ran it and it gives out of bound exception.
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
    at assignment09.ArrayExercises.reverseOrder(ArrayExer cises.java:214)
    at assignment09.ArrayExercisesClient.main(ArrayExerci sesClient.java:118)

  9. #29
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    I guess you know the meaning of that error message. ArrayIndexOutOfBoundException, for example say you have an array with 4 elements, you cannot access 5th element there. Because it's not available, obvious actually.

    And also arrays are zero index based, not only in java in most of the other languages as well. Zero based index means the counting start from 0 not from one.

    For and example, if you have an array like {'a', 'b', 'c'} then
    first element is 'a' and it's in index 0
    second element is 'b' and it's in index 1
    third element is 'c' and it's in index 2

    So what the value of symbols.length, it's 3 right? So don't have a position such. What you have to do? Actually there is two ways to do that.

    First you can reduce the position at the time of accessing.

    Java Code:
    char c = symbols[reverse - 1];

    Or else you can modify the for loop with the relevant range.

    Java Code:
    for(int reverse = (symbols.length - 1); reverse >= 0; reverse--)
    Think about the loop, why I use >= for comparison.

  10. #30
    bdario1 is offline Member
    Join Date
    Mar 2010
    Posts
    46
    Rep Power
    0

    Default

    Ok that makes sense actually i will try it when i get home since i am going to work now, and i will post if i have more problems, but i guess when i access every element in the array then how do i go about swaping thats where i am getting confused the most. Thanks for all of your help

  11. #31
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

  12. #32
    bdario1 is offline Member
    Join Date
    Mar 2010
    Posts
    46
    Rep Power
    0

    Default

    I am having problems on this part i got the tester to work fine, except that when i call the method this is what it prints.
    The array [a, b, c, f, n, q, s] entered sqnfcbalooks like this in reverse order[a, b, c, f, n, q, s]
    so it does print out the array but not the way it should i know why it prints it out because the print command in the method but if i get rid of the print then it does not call the method correctly in the tester.

    code for tester
    Java Code:
     Character [] newlist = new Character[]{'a','b','c','f','n','q','s'};
            System.out.println("");
            System.out.print ("The array [");
            separator = "";
            for (Character s : newlist)
            {
                System.out.print(separator + s);
                separator = ", ";
            }
            System.out.print ("] entered ");
            
            ArrayExercises.reverseOrder(newlist);
            System.out.print ("looks like this in reverse order[");
            separator = "";
            for (Character s : newlist)
            {
                System.out.print(separator + s);
                separator = ", ";
            }
            System.out.print ("] ");
    code of the method
    Java Code:
      public static void reverseOrder (Character[] symbols)
        {
        	for(int reverse = (symbols.length - 1); reverse >= 0; reverse--)
            {
        		
        		Character c = symbols[reverse];
                    System.out.print(c);
            }
        	
        }

  13. #33
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

  14. #34
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Replies: 8
    Last Post: 04-16-2012, 11:00 PM
  2. methods
    By lilac87 in forum New To Java
    Replies: 7
    Last Post: 07-22-2009, 06:37 PM
  3. Replies: 4
    Last Post: 04-03-2009, 06:36 AM
  4. JSP methods example
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-30-2008, 10:00 AM
  5. Methods
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-01-2007, 08:49 PM

Posting Permissions

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