Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 18 of 18

Thread: Loop Error

  1. #1
    Junior Member
    Join Date
    Sep 2014
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Loop Error

    Hi, I am beginner to Java. I wanted to create a method that will reverse elements of my array, but I keep getting error and I am not sure why.

    I use Jeliot to see what are the problems, but everything there is fine except the return statement in my code. Could anyone help me to fix it?
    public class ReverseMe {

    public static String revio(String[] a){
            int N = a.length;
            String[] reverse = new String[N];
            for (int i = 0; i < N ; i++) {
                reverse[i] = a[N-i-1];
     
            }
            return reverse[N];
     
     
     
    }
        public static void main (String[] args)
        {
            String[] myArray = {"1", "2", "3"};
            String arr = revio(myArray);
            System.out.println(arr);
       }
     }


  2. #2
    Junior Member
    Join Date
    Sep 2014
    Posts
    21
    My Mood
    Cheeky
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: Loop Error

    Have you been here? I found it by googling "reverse array java"
    Collections (Java Platform SE 7 )

  3. #3
    Junior Member
    Join Date
    Sep 2014
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Loop Error

    Well I've been there, but I can't find anything there that might help me.
    I know that I get this error because my array starts from 0 and the last index is 2. My program tried to access to index 3 which is not in range of 0 to 2. I can't eliminate it :/

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Loop Error

    I keep getting error
    Please copy the full text of the error message and paste it here. It has important info about the error.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Sep 2014
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Loop Error

    java.lang.ArrayIndexOutOfBoundsException: 3
    	at ReverseMes.reverseConcat(ReverseMes.java:10)
    	at ReverseMes.main(ReverseMes.java:20)
    	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    	at java.lang.reflect.Method.invoke(Unknown Source)
    	at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:272)
    >
    I already tried changing from return[N] to return[N-1] it's not the case, because I get only one value in my outcome : "1". I know that I can change from String revio(String[] a) to String[]revio(String[] a), but I would rather solve it my way.

  6. #6
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: Loop Error

    Edit: Sorry, that was not correct.

    I know that I can change from String revio(String[] a) to String[]revio(String[] a), but I would rather solve it my way.
    I dont understand what you mean with that? What is "your way" of solving it, and what is the problem with returning a string array?

  7. #7
    Junior Member
    Join Date
    Sep 2014
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Loop Error

    I went through it with Jeliot and I know that my problem is that it tries to get access to index 3 which is not in range of 0 to 2.. I know that my problem is the "return" statement which I can't fix.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Loop Error

    java.lang.ArrayIndexOutOfBoundsException: 3
    at ReverseMes.reverseConcat(ReverseMes.java:10)
    That error message is NOT for the posted code. It refers to reverseConcat() which is not shown.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Sep 2014
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Loop Error

    Sorry, my bad. This is correct error:
    java.lang.ArrayIndexOutOfBoundsException: 3
    	at ReverseMe.revio(ReverseMe.java:10)
    	at ReverseMe.main(ReverseMe.java:15)
    	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    	at java.lang.reflect.Method.invoke(Unknown Source)
    	at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:272)

    I fixed my problem using StringBuilder, but is there any possible solution to it without using StringBuilder?
    Last edited by yellowglow; September 19th, 2014 at 11:18 AM.

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Loop Error

    Please post the current version of the code showing what you are asking about.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    Sep 2014
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Loop Error

    public static String revio(String[] a){
      int N = a.length;
      StringBuilder reverse = new StringBuilder();
      for (int i = 0; i < N ; i++) {
      reverse.append(a[N-i-1]);  
      }
      return reverse.toString(); 
      }
     
     
    public static void main (String[] args)
    {
      String[] myArray = {"1", "2", "3"};  
      String arr = revio(myArray);
      System.out.println(arr);
    }
    }
    This is my current and fully working code.

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Loop Error

    That converts an array of Strings to a String, not an array with the Strings reversed.
    What if the array was of int not String?
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Junior Member
    Join Date
    Sep 2014
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Loop Error

    You're right. That's why I am curious how to solve it without StringBuilder. Any advice?

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Loop Error

    The first code looks close. Work on it.
    If you don't understand my answer, don't ignore it, ask a question.

  15. #15
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Loop Error

    Start with your original code and the error in Post #9, fix that error which will lead to other syntax errors or coding mistakes, fix those, and you'll end up with code that works. That single error begins a chain of errors that collectively are preventing the code from doing what you'd like.

  16. #16
    Junior Member
    Join Date
    Jul 2013
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Loop Error

    Quote Originally Posted by yellowglow View Post
    You're right. That's why I am curious how to solve it without StringBuilder. Any advice?
    try to swap your elements to reverse array

  17. #17
    Junior Member
    Join Date
    Sep 2014
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Loop Error

    if u r returning a array then its return type should be an array...use public static string[] reverse(parameters)

  18. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Loop Error

    @mrbadola The OP needs to define what the method is supposed to do. When that has been decided, then he'll know how to code it.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. For loop error
    By Aosora in forum What's Wrong With My Code?
    Replies: 3
    Last Post: June 4th, 2014, 03:53 AM
  2. Loop error
    By skypi205 in forum Java Theory & Questions
    Replies: 1
    Last Post: November 3rd, 2013, 05:32 AM
  3. [SOLVED] Simple Increment in a Loop error
    By chrisob in forum Loops & Control Statements
    Replies: 7
    Last Post: May 22nd, 2012, 07:33 AM
  4. Possible Loop Error...
    By The_Mexican in forum What's Wrong With My Code?
    Replies: 10
    Last Post: December 5th, 2010, 03:17 PM
  5. Error when loop used
    By anandkokatnur in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 24th, 2010, 11:17 AM