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.
Code Java:
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 :)
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.
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.
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:
Code java:
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 ;)
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 :D
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 :D?
Thanks so much again.
Code Java:
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]);
}
}
}
Re: Help needed with reversing numbers entered (in Java).
Quote:
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.
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.
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.
Re: Help needed with reversing numbers entered (in Java).
Quote:
Originally Posted by
dicdic
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
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:
Code Java:
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 :D
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
Re: Help needed with reversing numbers entered (in Java).
Thank you so much dicdic!:) I did not realize such a simplistic mistake. :eek:
I truly appreciate your assistance!
Thank you for helping me understand how to understand the foreign language of Java. :D:D
-Vex:cool: