Page 1 of 2 12 LastLast
Results 1 to 20 of 34
  1. #1
    bdario1 is offline Member
    Join Date
    Mar 2010
    Posts
    46
    Rep Power
    0

    Default Methods Please help

    Java Code:
    public static void reverseOrder (Character[] symbols)
    This method should reverse the order of elements in the input array.


    I need to write a method that will do the required things stated above, i am kind of lost to where to start.

  2. #2
    HackerOfDoom is offline Member
    Join Date
    Mar 2010
    Posts
    21
    Rep Power
    0

    Default

    So what exactly is your question? Are you trying to take input then print it backwards?

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

    Default Yes

    Yes whatever is in the array should be printed out backwards.

  4. #4
    iluxa is offline Senior Member
    Join Date
    Mar 2010
    Posts
    266
    Rep Power
    5

    Default

    So what have you tried already that hasn't worked out?

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

    Default

    Java Code:
     public static void reverseOrder (Character[] symbols)
        {
        	for(int reverse = 0; reverse < symbols.length; reverse++)
            {
            	
            }
        	
        }
    So far i created a loop, and the way i think i should do it is to create variables that will store the last element in it and replace it with the first?

  6. #6
    HackerOfDoom is offline Member
    Join Date
    Mar 2010
    Posts
    21
    Rep Power
    0

    Default

    Ok so I wrote some code that should do what I think you are trying to do, albeit with an error message before it outputs the backwards writing, here goes:
    Java Code:
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    
    
    public class Backwards {
    public static void main (String[] args){
    	BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    	String input = null;
    	try {
    		input = br.readLine();
    	} catch (IOException e) {
    		System.out.println("Error!");
    		System.exit(1);
    	}
    	for (int runtime = input.length() - 1; runtime <= input.length(); runtime--)
    	{
    		System.out.print(input.charAt(runtime));
    	}
    }
    }
    Tell me if that's what you're looking for :).

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

    Default

    I am still learning this Java things this is kind of confusing i see that you are creating an object for BufferedReader but where are you swapping the places in the array?

  8. #8
    HackerOfDoom is offline Member
    Join Date
    Mar 2010
    Posts
    21
    Rep Power
    0

    Default

    So are you trying to take an array entry and print it backwards? If so, just declare runtime before the for loop, and have it equal the array entry.

  9. #9
    HackerOfDoom is offline Member
    Join Date
    Mar 2010
    Posts
    21
    Rep Power
    0

    Default

    Obviously you have to create an array first, but I'm not sure you really need to do the thing with the array. What exactly is the context of this problem?

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

    Default

    Quote Originally Posted by HackerOfDoom View Post
    So are you trying to take an array entry and print it backwards? If so, just declare runtime before the for loop, and have it equal the array entry.
    I don't think these kind of stuff are easy to learn for our OP. He/she is still new to Java. And also it's better not provide the code every first time, give some guideline to tryout something first. Otherwise people no learn stuff.

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

    Default

    Quote Originally Posted by bdario1 View Post
    Java Code:
     public static void reverseOrder (Character[] symbols)
        {
        	for(int reverse = 0; reverse < symbols.length; reverse++)
            {
            	
            }
        	
        }
    So far i created a loop, and the way i think i should do it is to create variables that will store the last element in it and replace it with the first?
    Do you know how to print each element of your array within that for loop? Can you show it here.

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

    Default

    Java Code:
    public static void reverseOrder (Character[] symbols)
        {
        	for(int reverse = 0; reverse < symbols.length; reverse++)
            {
            	char c = symbols[reverse];
                    System.out.print(c);
            }
        	
        }
    Would it be like this?

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

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

    Default

    it only prints out the numbers from the size of the array but it goes to 100 even though in the test i have only created an Character array of 3 elements
    Java Code:
    Character [] newlist = new Character[]{'c','b','d'};

  15. #15
    HackerOfDoom is offline Member
    Join Date
    Mar 2010
    Posts
    21
    Rep Power
    0

    Default

    Ok there's your problem, the correct way to define an array is:
    Java Code:
    <variableType> <arrayName>[] = new <variableType>[<numberOfElementsInArray>];
    <defineArrayElementsHere>

  16. #16
    HackerOfDoom is offline Member
    Join Date
    Mar 2010
    Posts
    21
    Rep Power
    0

    Default

    oops
    that should be
    Java Code:
    <variableType>[]<arrayName>

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

    Default

    Quote Originally Posted by bdario1 View Post
    it only prints out the numbers from the size of the array but it goes to 100 even though in the test i have only created an Character array of 3 elements
    Java Code:
    Character [] newlist = new Character[]{'c','b','d'};
    You said that in the method it print numbers no characters, it prints more than what you define? It cannot happen, can you show your complete code here?

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

    Default

    Quote Originally Posted by HackerOfDoom View Post
    oops
    that should be
    Java Code:
    <variableType>[]<arrayName>

    No, you ca do it in many ways.

    Java Code:
            // Method 1
            Character [] newlist = new Character[]{'c','b','d'};
            
            // Method 2
            Character newList1[] = new Character[]{'c','b','d'};
            
            // Method 3
            Character newList2[] = new Character[3];
            newList2[0] = 'c';
            newList2[1] = 'b';
            newList2[2] = 'd';

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

    Default

    Quote Originally Posted by HackerOfDoom View Post
    Ok there's your problem, the correct way to define an array is:
    Java Code:
    <variableType> <arrayName>[] = new <variableType>[<numberOfElementsInArray>];
    <defineArrayElementsHere>
    There is nothing wrong with the array declaration and initialization in OPs code. Check on my previous post.

  20. #20
    HackerOfDoom is offline Member
    Join Date
    Mar 2010
    Posts
    21
    Rep Power
    0

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 8
    Last Post: 04-17-2012, 12:00 AM
  2. methods
    By lilac87 in forum New To Java
    Replies: 7
    Last Post: 07-22-2009, 07:37 PM
  3. Replies: 4
    Last Post: 04-03-2009, 07:36 AM
  4. JSP methods example
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-30-2008, 11:00 AM
  5. Methods
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-01-2007, 09: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
  •