This method should reverse the order of elements in the input array.Code:public static void reverseOrder (Character[] symbols)
I need to write a method that will do the required things stated above, i am kind of lost to where to start.
Printable View
This method should reverse the order of elements in the input array.Code:public static void reverseOrder (Character[] symbols)
I need to write a method that will do the required things stated above, i am kind of lost to where to start.
So what exactly is your question? Are you trying to take input then print it backwards?
Yes whatever is in the array should be printed out backwards.
So what have you tried already that hasn't worked out?
So far i created a loop, and the way i think i should do it is to create variables that will store the last element in it and replace it with the first?Code:public static void reverseOrder (Character[] symbols)
{
for(int reverse = 0; reverse < symbols.length; reverse++)
{
}
}
Ok so I wrote some code that should do what I think you are trying to do, albeit with an error message before it outputs the backwards writing, here goes:
Tell me if that's what you're looking for :).Code:import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Backwards {
public static void main (String[] args){
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String input = null;
try {
input = br.readLine();
} catch (IOException e) {
System.out.println("Error!");
System.exit(1);
}
for (int runtime = input.length() - 1; runtime <= input.length(); runtime--)
{
System.out.print(input.charAt(runtime));
}
}
}
I am still learning this Java things this is kind of confusing i see that you are creating an object for BufferedReader but where are you swapping the places in the array?
So are you trying to take an array entry and print it backwards? If so, just declare runtime before the for loop, and have it equal the array entry.
Obviously you have to create an array first, but I'm not sure you really need to do the thing with the array. What exactly is the context of this problem?
Would it be like this?Code:public static void reverseOrder (Character[] symbols)
{
for(int reverse = 0; reverse < symbols.length; reverse++)
{
char c = symbols[reverse];
System.out.print(c);
}
}
Did you run the code? If so what happen? Can you show your results here?
it only prints out the numbers from the size of the array but it goes to 100 even though in the test i have only created an Character array of 3 elements
Code:Character [] newlist = new Character[]{'c','b','d'};
Ok there's your problem, the correct way to define an array is:
Code:<variableType> <arrayName>[] = new <variableType>[<numberOfElementsInArray>];
<defineArrayElementsHere>
oops
that should be
Code:<variableType>[]<arrayName>
No, you ca do it in many ways.
Code:// Method 1
Character [] newlist = new Character[]{'c','b','d'};
// Method 2
Character newList1[] = new Character[]{'c','b','d'};
// Method 3
Character newList2[] = new Character[3];
newList2[0] = 'c';
newList2[1] = 'b';
newList2[2] = 'd';
Ok, good to know, thanks!