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

Thread: Help needed with reversing numbers entered (in Java).

  1. #1
    Member Knowledge_Feeds_The_Mind's Avatar
    Join Date
    Feb 2014
    Posts
    38
    My Mood
    Worried
    Thanks
    36
    Thanked 0 Times in 0 Posts

    Question Help needed with reversing numbers entered (in Java).

    I am having issues with my output and cannot figure out what is causing it. I have been struggling for 2 days now.. Any help appreciated
    Also if I can see how to calculate the minimum and the maximum in the same program, it would be greatly appreciated.

     
        public static void main(String[] args) {
            Scanner keyboard = new Scanner(System.in); 
            int integers; 
            int[] numbers;
            numbers = new int[10];
            int max;
            int min;
     
            System.out.println("Please enter ten numbers: ");
            for (int i = 0; i < numbers.length; i++)  
            {
             numbers[i] = keyboard.nextInt ();  
            };
            int input = keyboard.nextInt();
            int[] revNumbers;
            revNumbers = new int[10];
            for ( int i = (numbers.length - 1); i >= 0; i--) //problem child
            {
             revNumbers[ revNumbers.length - 1] = numbers [ i ]; 
             System.out.println("Numbers in reverse order are: " +  i); 
            }
        }
     
     }
    My result when I input numbers are always the following:

    "Numbers in reverse order are: 9, Numbers in reverse order are: 8,
    Numbers in reverse order are: 7, Numbers in reverse order are: 6
    Numbers in reverse order are: 5, Numbers in reverse order are: 4,
    Numbers in reverse order are: 3, Numbers in reverse order are: 2,
    Numbers in reverse order are: 1, Numbers in reverse order are: 0."

    Thank you


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,845
    My Mood
    Mellow
    Thanks
    239
    Thanked 721 Times in 707 Posts

    Default Re: Help needed with reversing numbers entered (in Java).

    How do you want the output to look? If you want:

    Numbers in reverse order are: 9, 8, 7, 6, 5, 4, 3, 2, 1, 0

    then put the phrase "Numbers in reverse order are: " before (outside) the loop. These are small adjustments you should figure out after seeing the incorrect output.

    If you are looking for some other result, please specify.

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

    Knowledge_Feeds_The_Mind (March 30th, 2014)

  4. #3
    Member Knowledge_Feeds_The_Mind's Avatar
    Join Date
    Feb 2014
    Posts
    38
    My Mood
    Worried
    Thanks
    36
    Thanked 0 Times in 0 Posts

    Default Re: Help needed with reversing numbers entered (in Java).

    It was my mistake for not specifying, I get the results (above) for every number I put in. It is as if those values are set in the array. I would like to know how to change it so, that when I input numbers like:
    12,17,20,31,16,18,27,19,25,

    I get the numbers in reverse:
    25,19,27,18,16,31,20,17,12.

    Thank you so much for addressing this question on the weekend.

  5. #4
    Junior Member drgy55's Avatar
    Join Date
    Mar 2014
    Posts
    7
    My Mood
    Mellow
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: Help needed with reversing numbers entered (in Java).

    Hey Knowledge, it looks like the problem you're having is that your final System.out.println is printing out the variable "i" rather than the value of revNumbers[i]. You will also want to change your initial for loop to read for (int i = 0; i < numbers.length - 1; i++) so that the user does not enter 11 numbers initially. Try this code out:

    public static void main(String[] args)
    	{
    		        Scanner keyboard = new Scanner(System.in); 
    		        int integers; 
    		        int[] numbers;
    		        numbers = new int[10];
    		        int max;
    		        int min;
     
    		        System.out.println("Please enter ten numbers: ");
    		        for (int i = 0; i < numbers.length - 1; i++)  
    		        {
    		         numbers[i] = keyboard.nextInt ();  
    		        };
    		        int input = keyboard.nextInt();
    		        int[] revNumbers;
    		        revNumbers = new int[10];
    		        for ( int i = (numbers.length - 1); i > 0; i--) //problem child
    		        {
    		         revNumbers[ i] = numbers [ i ]; 
    		         System.out.println("Numbers in reverse order are: " +  revNumbers[i]); 
    		        }
    	}
     
    }

    Also, I'm sure it's only temporary, but maybe add some spacing/indentation to make it a bit neater
    Last edited by drgy55; March 30th, 2014 at 05:01 PM.

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

    Knowledge_Feeds_The_Mind (March 30th, 2014)

  7. #5
    Member Knowledge_Feeds_The_Mind's Avatar
    Join Date
    Feb 2014
    Posts
    38
    My Mood
    Worried
    Thanks
    36
    Thanked 0 Times in 0 Posts

    Default Re: Help needed with reversing numbers entered (in Java).

    Drgy55, thanks so much B), this is so rad. Cannot believe that I did not notice this. I go by the name Vex, because it is shorter and easier

    My answer finally reverses!! There is a 0 though, that replaces the first number in the sequence. Is there anything I can do to fix this ?
    Thanks so much again.

     
        public static void main(String[] args) {
     
            Scanner keyboard = new Scanner(System.in); 
            int integers; 
            int[] numbers;
            numbers = new int[10];
            int max;
            int min;
     
            System.out.println("Please enter ten numbers: ");
            for (int i = 0; i < numbers.length - 1; i++)  
            {
             numbers[i] = keyboard.nextInt ();  
            };
     
            int input = keyboard.nextInt();
            int[] revNumbers;
            revNumbers = new int[10];
            for ( int i = (numbers.length - 1); i > 0; i--)
     
            {
     
             revNumbers[ i ] = numbers [ i ]; //10
             System.out.println("Numbers in reverse order are: " + revNumbers[i]); 
     
           }
        }
     }

  8. #6
    Member
    Join Date
    Oct 2013
    Location
    Manila, Philippines
    Posts
    285
    My Mood
    Amused
    Thanks
    6
    Thanked 64 Times in 61 Posts

    Default Re: Help needed with reversing numbers entered (in Java).

    My answer finally reverses!!
    Actually, the array did not reversed. You just print it from its last element to first since you iterate the array from last index and put a print inside the loop.

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

    Knowledge_Feeds_The_Mind (March 30th, 2014)

  10. #7
    Member Knowledge_Feeds_The_Mind's Avatar
    Join Date
    Feb 2014
    Posts
    38
    My Mood
    Worried
    Thanks
    36
    Thanked 0 Times in 0 Posts

    Default Re: Help needed with reversing numbers entered (in Java).

    So then how would i write it correctly? Thank you so much!!!!!
    Thank you for your assistance.

  11. #8
    Member
    Join Date
    Oct 2013
    Location
    Manila, Philippines
    Posts
    285
    My Mood
    Amused
    Thanks
    6
    Thanked 64 Times in 61 Posts

    Default Re: Help needed with reversing numbers entered (in Java).

    base on your code, it would be like this,
    before the loop starts, create integer variable with initial value equal to the length of your numbers array subtracted by 1.
    then, create a loop which starts at index 0, condition of less than length of numbers, incremented by 1.
    then do the declaration of value in your revNumbers array.
    something like this:
    revNumbers[index in your loop] = numbers[the variable created before the loop]
    and make sure to subtract 1 to the variable that is declared before the loop every after declaration.

  12. #9
    Junior Member drgy55's Avatar
    Join Date
    Mar 2014
    Posts
    7
    My Mood
    Mellow
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: Help needed with reversing numbers entered (in Java).

    Quote Originally Posted by dicdic View Post
    base on your code, it would be like this,
    before the loop starts, create integer variable with initial value equal to the length of your numbers array subtracted by 1.
    then, create a loop which starts at index 0, condition of less than length of numbers, incremented by 1.
    then do the declaration of value in your revNumbers array.
    something like this:
    revNumbers[index in your loop] = numbers[the variable created before the loop]
    and make sure to subtract 1 to the variable that is declared before the loop every after declaration.
    ^This would be a true reversal of the array

  13. #10
    Member Knowledge_Feeds_The_Mind's Avatar
    Join Date
    Feb 2014
    Posts
    38
    My Mood
    Worried
    Thanks
    36
    Thanked 0 Times in 0 Posts

    Default Re: Help needed with reversing numbers entered (in Java).

    So I figured it all out, but there is a small hitch. The program prompts for 11 values, when it is supposed to prompt for only 10.......
    How do I fix this?




    This is how my code looks right now:


    public static void main(String[] args) {
     
            Scanner keyboard = new Scanner(System.in); 
            int[] numbers; // The values that are inputted and then plugged into "i."
            numbers = new int[10]; // The array that holds 10 spaces for 10 values
            int max = 0;
            int min = 100;
     
            System.out.println("Please enter ten numbers: ");
            for (int i = 0; i < numbers.length; i++)  
            {
             // The equationss that calculate the minimum and maximum values.
             numbers[i] = keyboard.nextInt(); 
             if (numbers [i] > max){
                 max =  numbers [i];
             }
     
             if (numbers [i] < min){  
                  min = numbers [i];         
            }
            };
     
            // The code used to calculate the reverse number and finalize it. 
            int input = keyboard.nextInt();
            int[] revNumbers;
            revNumbers = new int[10];
            for ( int i = (numbers.length - 1 ); i >= 0; i--)
     
            { 
             // Final step that outputt the reverse numbers and then min & max.   
             revNumbers[i] = numbers [i]; //10
             System.out.println("Numbers in reverse order are: " + revNumbers[i]);
     
        }  
             System.out.println("Max = " + max);
             System.out.println("Min = " + min);
             System.out.println("Program solved. Thank you for you cooperation.");
       }
      }
    Thank you so much

  14. #11
    Member
    Join Date
    Oct 2013
    Location
    Manila, Philippines
    Posts
    285
    My Mood
    Amused
    Thanks
    6
    Thanked 64 Times in 61 Posts

    Default Re: Help needed with reversing numbers entered (in Java).

    because of this int input = keyboard.nextInt(); after your loop.
    your loop will ask for 10 input, and after the loop another input will be made because of int input = keyboard.nextInt();

    and as I told you ind my above reply, you did not really reversed the array.
    the reversed array is stored at revNumbers right?
    try to print all the element after this statement
    System.out.println("Program solved. Thank you for you cooperation.");
    create a loop and print the values of revNumbers you'll see that it did not really reveres

  15. The Following User Says Thank You to dicdic For This Useful Post:

    Knowledge_Feeds_The_Mind (April 1st, 2014)

  16. #12
    Member Knowledge_Feeds_The_Mind's Avatar
    Join Date
    Feb 2014
    Posts
    38
    My Mood
    Worried
    Thanks
    36
    Thanked 0 Times in 0 Posts

    Default Re: Help needed with reversing numbers entered (in Java).

    Thank you so much dicdic! I did not realize such a simplistic mistake.
    I truly appreciate your assistance!
    Thank you for helping me understand how to understand the foreign language of Java.
    -Vex

Similar Threads

  1. Replies: 1
    Last Post: March 19th, 2014, 10:37 PM
  2. Java Recursion reversing my strings
    By Naked Snake in forum What's Wrong With My Code?
    Replies: 6
    Last Post: September 26th, 2013, 10:32 AM
  3. [SOLVED] Help with my Java program: Making change from an entered double.
    By iDizzle in forum What's Wrong With My Code?
    Replies: 20
    Last Post: March 18th, 2012, 03:16 PM
  4. Reversing an array
    By severus1 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: June 30th, 2011, 03:54 PM
  5. Replies: 1
    Last Post: December 4th, 2010, 04:26 PM