Hi i am a beginner programmer and new this forum...i'm in the middle of an assignment and having some trouble with one part.....removing duplicates from a .txt file while writing to an array..here's my code so far..
class MainProg{
public static void main (String[]args){
GenKeys keys = new GenKeys();
try{
String f = "keys.txt";
FileWriter fw = new FileWriter(f);
BufferedWriter bw = new BufferedWriter(fw);
for (int i=1; i<=500; i++){
//bw.write(i+ System.getProperty("line.separtor"));
bw.write(keys.getrandom() + "\r\n");
}
// close the file after all the writing has taken place
bw.close ();
} catch (IOException e){
System.out.println ("Error writing to file" + e.toString());
}
// declare a place to store each line as it is read in
int num = 0;
int myArray[] = new int [500];
try{
File file = new File("keys.txt");
Scanner scanner = new Scanner(file);
while (scanner.hasNext()) {
System.out.println(scanner.next());
}
scanner.close();
}catch(IOException e){
System.out.print(e.toString());
System.out.print("Non-Existant File");
}
}
}
next part ofmy question is
MainProg should then read all of the keys from “keys.txt”, and store them in an
array, removing any duplicates. (In other words if the number 55 is generated
twice, only one of these 55’s should be added to the array). Note that your final
array will contain less than 500 keys. Now, write the remaining keys into a file
called “sorted.txt”, one key per line, with the smallest number on the first line
and the largest file on the last line.
If anyone can offer some help or advice on how to do this i would greatly appreciate this.
Thanks
--- Update ---
here's my java class to return the random ints....
import java.util.Random;
public class GenKeys{
Random random;
public GenKeys(){
random = new Random();
}
public int getrandom(){
return random.nextInt(250)+ 1;
}
}