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 9 of 9

Thread: Beginner Array Problem. Swapper

  1. #1
    Junior Member
    Join Date
    May 2012
    Posts
    17
    My Mood
    Stressed
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default Beginner Array Problem. Swapper

    SOLVED!





    This is what i have and I am not sure what i am doing wrong. It keeps giving me errors!

    public class Swapper
    {
    public static void main(String[] args){
       public void swapFirstAndSecondHalf(int[] values)
       {
          int[] a = {1,2,3,4,5};
    	  int k = 0;
    	  int temp = 0;
    	  while(k < a.length/ 2)
    	  {
    	  temp = a[k];
    	  a[k] = a[a.length -1-k];
    	  a[a.length-1-k] = temp;
    	  k++;
    	  }
     
     
     
     
       }
       // This method is used to check your work
       public int[] check(int[] values)
       {
          swapFirstAndSecondHalf(values);
          return values;
       }
    }
    }

    Any help would be greatly appreciated!
    Last edited by bankoscarpa; May 4th, 2012 at 11:45 PM. Reason: SOLVED!!!!!!!


  2. #2
    Member
    Join Date
    Apr 2012
    Location
    Superior, CO, USA
    Posts
    80
    Thanks
    0
    Thanked 14 Times in 14 Posts

    Default Re: Beginner Array Problem. Swapper

    Hint - don't put methods inside of methods. main() is a method. swapFirstAndSecondHalf() is a method.
    Need Java help? Check out the HotJoe Java Help forums!

  3. The Following User Says Thank You to stdunbar For This Useful Post:

    bankoscarpa (May 4th, 2012)

  4. #3
    Junior Member
    Join Date
    May 2012
    Posts
    17
    My Mood
    Stressed
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default Re: Beginner Array Problem. Swapper

    Would i fix that by making a new class?

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Beginner Array Problem. Swapper

    Try moving the method outside of the {}s that surround the code for the main() method.
    You may not need a new class.
    If you don't understand my answer, don't ignore it, ask a question.

  6. The Following User Says Thank You to Norm For This Useful Post:

    bankoscarpa (May 4th, 2012)

  7. #5
    Junior Member
    Join Date
    May 2012
    Posts
    17
    My Mood
    Stressed
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default Re: Beginner Array Problem. Swapper

    I am not sure what you mean. Should i write the code where the main method has nothing in it and then separate it?
    thank you Norm

  8. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Beginner Array Problem. Swapper

    To correct the compiler error, make sure that no methods are defined inside of other methods.
    Make that change, get a clean compile and THEN write code for the main() method.
    One step at a time.
    If you don't understand my answer, don't ignore it, ask a question.

  9. The Following User Says Thank You to Norm For This Useful Post:

    bankoscarpa (May 4th, 2012)

  10. #7
    Junior Member
    Join Date
    May 2012
    Posts
    17
    My Mood
    Stressed
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default Re: Beginner Array Problem. Swapper

    I have been trying to do that for the last few hours. It keeps giving me back errors.

  11. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Beginner Array Problem. Swapper

    Show the code and Post the full text of the error messages if you want help with them.
    If you don't understand my answer, don't ignore it, ask a question.

  12. The Following User Says Thank You to Norm For This Useful Post:

    bankoscarpa (May 4th, 2012)

  13. #9
    Junior Member
    Join Date
    May 2012
    Posts
    17
    My Mood
    Stressed
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default Re: Beginner Array Problem. Swapper

    the issue is now resolved. Thank you very much for your help. I ended up not using a second class. Thank you norm.

Similar Threads

  1. Beginner problem with a do-while.
    By Eliijahh in forum Loops & Control Statements
    Replies: 3
    Last Post: November 21st, 2011, 01:28 PM
  2. Beginner Problem
    By jokibaer in forum What's Wrong With My Code?
    Replies: 7
    Last Post: August 15th, 2011, 06:34 PM
  3. Beginner Problem
    By Melvrick in forum What's Wrong With My Code?
    Replies: 2
    Last Post: August 15th, 2011, 12:29 PM
  4. Beginner question on where to put an array
    By Diplo in forum What's Wrong With My Code?
    Replies: 5
    Last Post: June 3rd, 2011, 03:56 PM
  5. Beginner Problem
    By nve5009 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 19th, 2010, 11:29 AM