Hi, im currently studying for my final test in java beginner class, so there is a problem that i couldn't solved until now, i'm totally beginner and have search the solution for almost two days :(

The program is taking variabel n from other text file, then this n will be used as the amount of randomize data, after we get the random array we need to sort and reserve it, so far i could make the sort but not with the reverse, please somebody give a solution what should i do to make a reserve/descendant int array in output

import java.io.FileInputStream;
import java.util.*;


public class SortReserve{
	public static void main(String []args) throws Exception{
		Scanner input = new Scanner(System.in);
		Random rnd= new Random();
		Scanner fs;
		int deret;
		String filename;
		int n;


		System.out.print ("Nama File : "); filename = input.nextLine();


		try {
			fs = new Scanner (new FileInputStream(filename));
			while (fs.hasNext()){
				n=fs.nextInt();
				System.out.printf("\n  N : %d%n\n",n);
				int [] menaik = new int [n];
				
				for(int i=0;i<menaik.length;i++){
					menaik[i]=(rnd.nextInt(100)+1);
					Arrays.sort( menaik );
					System.out.println("Menaik\n");
					System.out.printf("%5d ",menaik[i]);
				}System.out.println("\n");

			}
			fs.close();


		}
		catch (java.io.FileNotFoundException e){
			System.err.println ("Can't make the file "+ filename);
			System.exit(-1);
		}
		catch (SecurityException e){
			System.err.println("Accses denied");
			System.exit(-1);
		}
	}
}

I've tried compare and just found out it only works for string array, i've search how to convert int array to string but didn't work out, any help and solution really appreciated :)

NB : you need any text file contains a number when running this program

what should i do to make a reverse

You need to come up with the algorithm/design to do this.
Take a piece of paper and work out the steps you need to do to reverse the list.
Or use a deck of cards laying out a few cards in a row. Then move the cards one at a time using only one hand to reverse them. Don't have one card cover another. Start at the other ends and move to the center. There will be a difference depending on if there are an even number of cards or an odd number.

I rewrote the program to make it easier to debug. You don't need the file while testing. Use numbers from the program to make it easier to see what is happening.

import java.util.*;
 
 
public class SortReverse {
	public static void main(String []args) {
		Random rnd= new Random();
		int deret;
		String filename;
		int n;
 
		Scanner	fs = new Scanner ("3 4 5 6 7");

			while (fs.hasNext()){
				n=fs.nextInt(); 
				System.out.printf("  N : %d  ", n);
				int [] menaik = new int [n];    // create a new array to hold random numbers
 
				for(int i=0;i<menaik.length;i++){
					menaik[i]= rnd.nextInt(100)+1;
//					Arrays.sort( menaik );    // this moves values to end after others of 0 ???
					System.out.print("Menaik:");
					System.out.printf("%4d ",menaik[i]);
				}  // end for(i) thru elements of this array
            System.out.println(""); // flush line
			} // end while()

			fs.close();
 
 
    }
}

/*
I've tried compare and just found out it only works for string array, i've search how to convert
int array to string but didn't work out, any help and solution really appreciated 

NB : you need any text file contains a number when running this program
idblack
Newbie Poster
  Online
  

  Output:
Running: java.exe -classpath D:\JavaDevelopment;.  -Xmx512M SortReverse

  N : 3  Menaik:  25 Menaik:  74 Menaik:  91 
  N : 4  Menaik:  28 Menaik:  84 Menaik:  66 Menaik:  29 
  N : 5  Menaik:  65 Menaik:  65 Menaik:  55 Menaik:  99 Menaik:  38 
  N : 6  Menaik:  64 Menaik:  87 Menaik:  63 Menaik:   6 Menaik:  29 Menaik:  69 
  N : 7  Menaik:  82 Menaik:  21 Menaik:   4 Menaik:  44 Menaik:  36 Menaik:  23 Menaik:  70 

0 error(s)

*/

Thank you for your reply Mr NormR1 but that program is not what exactly i need, what i need is this :

ie : from text file we load that n = 4, so there will be 4 random number generated and saved to menaik[]
lets make the generated number is : 2 4 12 0
Sort
0 2 4 12
Reverse
12 4 2 0

my problem when i tried to make the reverse i do understand the logic to pop stack from the biggest index but i can't make it in Java language

Can you write down the logic for doing the reverse. Something like this:
You need a loop with two indexes for the array, one to the lowest and one to the highest
Swap the elements at each index
incr the lowest and decrment the highest
test if indexes meet
if not, go back to swap step.

either that, or create a loop with a second (temporary) array that is as big as the first one.
array2[0] = array1[n];
array2[1] = array1[n-1];
...
and later on, switch the two, especially if you're having troubles with the correct increase/decrease values, this could be a sollution

If you do not want to have another array, sort the array first, and then do it as followed:

aArray <- a sorted array
head <- 0
tail <- array.size() - 1
while head < tail
  swap aArray[head] with aArray[tail]
  increment head by 1
  decrement tail by 1

That's a simple psudo code (my own version :P).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.