dimanche 30 avril 2017

Inputting text file to an array, randomizing data from text file in array, outputting randomized data to separate text file

I have successfully copied the code into the array (which is actually a list of all 201 countries and their level of internet usage). The list is in alphabetical order and I want to randomize the data so that there is no order to the data in the array. Here is my code:

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

public class Unsort {
public static void main(String[] args) throws IOException {

    String[] countries = new String[201];
    String[] percentages = new String[201];
    String[] line = new String[201];

    Scanner keyboard = new Scanner(System.in);

    Scanner fileIN = new Scanner(new File("F:/CountrySortedAlpha.txt"));
    PrintWriter out = new PrintWriter("F:/CountryUnsortedAlpha.txt");

    while(fileIN.hasNext()){
        for(int i = 0; i < 201; i++){
            line[i] = fileIN.nextLine();
            System.out.print(line[i] + " " + i + "\n");
        }
        for(int i = 0; i < 201; i ++){
            int randomize = (int) (Math.random() * 201);
            System.out.print("\n" + randomize);
        }
    }
}
}

The way that I have been trying is to make a random number to access the array at, but it ends up with so many collisions. The second loop was just to make sure that the random variable worked. So my question is: how do I randomize the data in the array without collisions while using the random number generator?




Aucun commentaire:

Enregistrer un commentaire