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.


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

Results 1 to 3 of 3

Thread: Reversed Input

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

    Default Reversed Input

       public static void main(String args[])
           int[] array = StdIn.readAllInts();
           for (int i = 0; i<array.length;i++){
           int temp = array[i];
           array[i] = array[(array.length) - 1 - i];
           array[(array.length) - 1 - i] = temp;
           System.out.print(array[i] + " ");

    What am I doing wrong? My input is {1, 2, 3, 1, 2, 3} and outcome of it is {3, 2, 1, 1, 2, 3}.. How to make to return {3, 2, 1, 3, 2, 1} ?

  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Washington, DC
    My Mood
    Thanked 636 Times in 540 Posts

    Default Re: Reversed Input

    What happened when you stepped through this with a debugger, or at least added some print statements, to figure out where the execution of the program differs from what you'd expect?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Grand Poobah
    Join Date
    Mar 2011
    My Mood
    Thanked 167 Times in 158 Posts

    Default Re: Reversed Input

    Is the objective to reverse the array or just display the data in reverse order? If the latter then just use a reverse loop to print out all values.
    Improving the world one idiot at a time!

Similar Threads

  1. Just need a little input and help please!
    By rosemccaskey in forum Object Oriented Programming
    Replies: 1
    Last Post: April 11th, 2013, 05:38 PM
  2. Best Way for Yes or No Input
    By Levica in forum Java Theory & Questions
    Replies: 1
    Last Post: January 11th, 2013, 02:48 PM
  3. Returning reversed string? Help please.
    By mindlessn00b in forum What's Wrong With My Code?
    Replies: 6
    Last Post: October 18th, 2010, 04:23 PM
  4. Getting input
    By BuhRock in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 22nd, 2010, 10:30 AM
  5. [SOLVED] allow a new input, dicarding the last mismatch input without terminating the program
    By voltaire in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 9th, 2010, 04:44 AM