List<Cluster<DoublePoint>> to Set<DoublePoint> - java

I am trying to use DBSCANClusterer(apache.math3) to sort a Set of points which I generate and write it to a file. At this point, I am stuck here:
public Set<DoublePoint> DBSCAN(Set<DoublePoint> set2) {
Set<DoublePoint> points = new Set<DoublePoint>();
DBSCANClusterer<DoublePoint> dbscan = new DBSCANClusterer<DoublePoint>(1, 15);
//run dbscan on set of points
List<Cluster<DoublePoint>> clusters = dbscan.cluster(set2);
**sorted = clusters???**
How can I assign: List<Cluster<DoublePoint>> clusters to Set<DoublePoint> sorted?? I guess it should be something like 2D->1D!
And here is the rest of my code:
import java.awt.Point;
import java.io.*;
import java.util.*;
import org.apache.commons.math3.ml.clustering.Cluster;
import org.apache.commons.math3.ml.clustering.DBSCANClusterer;
import org.apache.commons.math3.ml.clustering.DoublePoint;
import java.util.HashSet;
import java.util.Random;
import java.util.Set;
public class Main {
public static void main(final String[] args) throws Exception {
new Main().run();
}
public void run() {
Set<DoublePoint> set = generateSetPoints();
try {
writeToFile(set, "points");
} catch(IOException ex) {
System.out.println("IO Exception while writing to file");
}
Set<DoublePoint> set_by_dbscan = dbScan(set);//
try {
writeToFile(set_by_dbscan, "by_dbscan");
} catch(IOException ex) {
System.out.println("IO Exception while writing to file");
}
}
public Set<DoublePoint> generateSetPoints() {
int xx=100;
int yy=100;
Set<DoublePoint> set = new HashSet<>();
Random rnd = new Random();
int number=100;
do{
int tmp[] = new int[2];
tmp[0] = rnd.nextInt(xx);
tmp[1] = rnd.nextInt(yy);
DoublePoint rndpoint = new DoublePoint(tmp);
set.add(rndpoint);
}
while (set.size()<number);
return set;
}
public void writeToFile(Set<DoublePoint> set, String filename) throws IOException {
File fout = new File( filename + ".txt");
FileOutputStream fos = new FileOutputStream(fout);
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos));
for (DoublePoint p: set) {
bw.write(p.getPoint()[0] + "," + p.getPoint()[1]);
bw.newLine();
}
bw.close();
}
public Set<DoublePoint> dbScan(Set<DoublePoint> set2) {
Set<DoublePoint> points = new Set<DoublePoint>();
DBSCANClusterer dbscan = new DBSCANClusterer(1, 15);
List<Cluster<DoublePoint>> clusters = dbscan.cluster(set2);
return clusters;
}
}

A HashSet is an unsorted data structure.
If you want sorted to be sorted, use something that retains order.
sorted = nes HashSet<>() is a real WTF...
Also, DBSCAN is not meant for sorting. Use OPTICS clustering instead.

Related

Unable to copy txt file over to arraylist and into file

I am attempting to add this large txt file into an array list then sort the data. Then put 15000 lines in various temp files. I am unable to put the data into each file. Here is my code:
package bigfilesorter2;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class bigfilesorter2 {
public static final int NUM_LINES = 15000;
public static void main(String args[]) throws IOException {
FileReader fileReader = new FileReader("Aesop_Shakespeare_Shelley_Twain.txt");
BufferedReader br = new BufferedReader(fileReader);
ArrayList<String> arraylist = readingfile(br);
//System.out.println(arraylist);
makingfiles(br, arraylist);
}
public static void makingfiles(BufferedReader br, ArrayList<String> arraylist) throws IOException {
int start = 0;
int end = 15000;
for(int i = 0; i < 20; i++) {
File file = new File("/Users/domlanza/desktop/testing/Filee"+i+".txt");
FileWriter fw = new FileWriter(file);
BufferedWriter bw = new BufferedWriter(fw);
for(;start <= end; start++){
bw.write(arraylist.get(start));
bw.newLine();
}
bw.flush();
bw.close();
fw.close();
start = end + 1;
end += 15000;
}
}
public static ArrayList<String> readingfile(BufferedReader br) throws FileNotFoundException, IOException {
//Read in file
Scanner s = new Scanner(new File("Aesop_Shakespeare_Shelley_Twain.txt"));
int count = 0;
ArrayList<String> arraylist = new ArrayList<String>();
while (s.hasNext()) {
count++;
arraylist.add(s.nextLine());
}
//} catch (IOException e) {e.printStackTrace();}
Collections.sort(arraylist);
//System.out.println(arraylist);
return arraylist;
}
}
Any help would be appreciated. the commas were just the file being sorted..................
"it looks like your post is mostly code"
You need to create a list of sublists where each sublist holds 15000 lines. Given below is the complete code:
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
public class BigFileSorter {
public static final int NUM_LINES = 15000;
public static final int NUM_FILES = 20;
public static void main(String args[]) throws IOException {
FileReader fileReader = new FileReader("file.txt");
BufferedReader br = new BufferedReader(fileReader);
ArrayList<ArrayList<String>> list = readingfile(br);
makingfiles(br, list);
}
public static void makingfiles(BufferedReader br, ArrayList<ArrayList<String>> list) throws IOException {
if (list != null) {
for (int i = 0; i < NUM_FILES; i++) {
File file = new File("Filee" + i + ".txt");
FileWriter fw = new FileWriter(file);
ArrayList<String> subList = list.get(i);
for (String str : subList) {
fw.write(str + System.lineSeparator());
}
fw.close();
}
}
}
public static ArrayList<ArrayList<String>> readingfile(BufferedReader br)
throws FileNotFoundException, IOException {
ArrayList<ArrayList<String>> list = new ArrayList<ArrayList<String>>();
ArrayList<String> subList;
String line;
try {
for (int i = 0; i < NUM_FILES; i++) {
subList = new ArrayList<String>();
for (int j = 0; j < NUM_LINES; j++) {
line = br.readLine();
if (line == null) {
break;
}
subList.add(line);
}
Collections.sort(subList);
list.add(subList);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
br.close();
}
return list;
}
}
Feel free to comment in case of any doubt.
Maybe something like this as an inner for loop in your makefiles method.
// outside of the for loops
int start = 0;
int end = 15000;
// inner for loop
for(;start <= end; start++){
bw.write(arraylist.get(start));
bw.newline();
}
// end of outer for loop
start = end + 1;
end += 15000;
So complete method:
public static void makingfiles(BufferedReader br, ArrayList<String> arraylist) throws IOException {
int start = 0;
int end = 15000;
for(int i = 0; i < 20; i++) {
File file = new File("/Users/domlanza/desktop/testing/Filee"+i+".txt");
FileWriter fw = new FileWriter(file);
BufferedWriter bw = new BufferedWriter(fw);
for(;start <= end; start++){
bw.write(arraylist.get(start));
bw.newline();
}
bw.flush();
bw.close();
fw.close()
start = end + 1;
end += 15000;
}
}
Should work for what you asked in the comment, but you still have to change your read method so that it reads all the lines in one arraylist

The infinite monkey theorem in Java [duplicate]

This question already has answers here:
What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?
(26 answers)
Closed 5 years ago.
I have an exercise where are given a list of 850 basic words in English in the file basicWords.txt. I need to compose a text of 10000 words by randomly selecting words from the basic words list and write it to another file. I generated successfully the words, but I have a problem: I get an exception when the words are generated: ArrayIndexOutOfBoundsException at line 35. Also, how can I print the result into another text file?
I have a final solution for this:
package randomstring;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
/**
*
* #author robi1
*/
public class RandomString {
public static void main(String[] args){
List<String> dictionary = readDictionaryFrom("basicWordsInEnglish.txt");
List<String> monkeyText = generateTextFrom(dictionary);
writeTextToFile(monkeyText, "final.txt");
String letters = "abcdefghijklmnopqrstuvwxyz ";
Object[] wrds = readFile("basicWordsInEnglish.txt");
int x = wrds.length;
String[] words = new String[x];
for(int i =0;i<x;i++){
words[i] = wrds[i].toString();
}
char[] let = letters.toCharArray();
String n ="";
Random r = new Random();
char t;
}
public static Object[] readFile(String name){
ArrayList<String> al = new ArrayList<String>();
FileInputStream fstream;
try {
fstream = new FileInputStream(name);
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
while((strLine=br.readLine())!=null){
if(strLine.length()>4)
al.add(strLine);
}
fstream.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Object[] array = al.toArray();
return array;
}
public static List<String> readDictionaryFrom(String path) {
try {
return Files.readAllLines(new File(path).toPath());
} catch(IOException e) {
throw new RuntimeException(e);
}
}
public RandomString(List<String> text, String path) {
try(BufferedWriter file = new BufferedWriter(new FileWriter(new File(path)))){
for(String word : text) {
file.write(word+" ");
}
} catch(IOException e) {
throw new RuntimeException(e);
}
}
public static List<String> generateTextFrom(List<String> words) {
Random generator = new Random();
List<String> result = new ArrayList<>();
for(int i = 0; i < 10000; i++) {
int random = generator.nextInt(words.size());
result.add(words.get(random));
}
return result;
}
public static void writeTextToFile(List<String> text, String path) {
try(BufferedWriter file = new BufferedWriter(new FileWriter(new File(path)))){
for(String word : text) {
file.write(word+" ");
}
} catch(IOException e) {
throw new RuntimeException(e);
}
}
}
Why do you not use collections? According to description this task is very easy especially when don't use bunch for, while loops and meaningless variables like n,t,j. etc.
public void main(String... args) {
List<String> dictionary = readDictionaryFrom("path to dictionary");
List<String> monkeyText = generateTextFrom(dictionary);
writeTextToFile(monkeyText, "path to destination file");
}
public List<String> readDictionaryFrom(String path) {
try {
return Files.readAllLines(new File(path).toPath());
} catch(IOException e) {
throw new RuntimeException(e);
}
}
public static void writeTextToFile(List<String> text, String path) {
try(BufferedWriter file = new BufferedWriter(new FileWriter(new File(path)))){
for(String word : text) {
file.write(word);
}
} catch(IOException e) {
throw new RuntimeException(e);
}
}
public static List<String> generateTextFrom(List<String> words) {
Random generator = new Random();
List<String> result = new ArrayList<>();
for(int i = 0; i < 10_000; i++) {
int random = generator.nextInt(words.size());
result.add(words.get(random));
}
return result;
}
Use the debugging feature of your favorite IDE (might be Eclipse), set an exception breakpoint on ArrayIndexOutOfBoundsException, run your program in debug mode.
When it hits the exception, Eclipse will halt your program. Look at your variable values, especially which array you are accessing and what value the index has, and why it got a value outside of the array size.
By the way, your code line if(n.length()>4){ cannot produce an ArrayIndexOutOfBoundsException, as there's no array indexing in that line.

Read string arrays from file

I have this code:
public static void write() throws IOException{
ObjectOutputStream out = new ObjectOutputStream(
new FileOutputStream("ips.txt")
);
for ( int i = 0; i < Main.ipList.length; i++){
out.writeObject(ipList[i]);
}
out.flush();
out.close();
}
Which writes the string array to a text file:
static String[] ipList = {"127.0.0.1", "173.57.51.111"};
I was wondering how it would be possible to read the text file and edit the ipList with the new ips.
If you want to write String objects to a file, it's better to use a FileWriter instead of an ObjectOutputStream. Similarly, use a FileReader to read from the file. See https://docs.oracle.com/javase/tutorial/essential/io/charstreams.html for how to use these Reader objects.
ObjectOutputStream is usually suitable for writing more complex objects that implement the java.io.Serializable interface.
Here's an example:
BufferedReader inputStream = null;
List<String> ipList = new ArrayList<>();
try {
inputStream = new BufferedReader(new FileReader("ips.txt"));
String l;
while ((l = inputStream.readLine()) != null) {
ipList.add(l);
}
} finally {
if (inputStream != null) {
inputStream.close();
}
}
// get an array from the ArrayList
ipArray = ipList.toArray(new String[ipList.size()]);
You can try something like this
package a;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class A {
static String[] ipList = { "127.0.0.1", "173.57.51.111" };
public static void main(String[] args) {
try {
write();
update();
} catch (IOException e) {
System.err.println(e);
}
Arrays.asList(ipList).stream().forEach(System.out::println);
}
// Your method
public static void write() throws IOException {
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("ips.txt"));
for (int i = 0; i < A.ipList.length; i++) {
out.writeObject(ipList[i]);
}
out.flush();
out.close();
}
public static void update() throws IOException {
List<String> lines = Files.readAllLines(Paths.get(".", "newIps.txt"));
List<String> newIps = new ArrayList<>();
newIps.addAll(Arrays.asList(ipList));
newIps.addAll(lines);
ipList = newIps.toArray(ipList);
}
}
The content of the newIps.txt file is
0.0.0.0
192.168.1.1
The output of the program is
127.0.0.1
173.57.51.111
0.0.0.0
192.168.1.1
Note that Arrays.asList(ipList) returns a bridge list over the array (any changes to the list will be visible for array), so we do a putAll

Take numbers from a file and sort them

I need to make my program read a file, then take the numbers in the string and sort them into an array. I can get my program to read the file and put it to a string, but that's where I'm stuck. All the numbers are on different lines in the file, but appear as one long number in the string. This is what I have so far:
public static void main(String[] args) {
String ipt1;
Scanner fileInput;
File inFile = new File("input1.dat");
try {
fileInput = new Scanner(inFile);
//Reads file contents
while (fileInput.hasNext()) {
ipt1 = fileInput.next();
System.out.print(ipt1);
}
fileInput.close();
}
catch (FileNotFoundException e) {
System.out.println(e);
}
}
I recommend reading the values in as numeric types using fileInput.nextInt() or whatever type you want them, putting them in an array and using a built in sort like Arrays.sort. Unless I'm missing a more subtle point about the question.
If your task is just to get input from some file and you're sure the file has integers, use an ArrayList.
import java.util.*;
Scanner fileInput;
ArrayList<Double>ipt1 = new ArrayList<Double>();
File inFile = new File("input1.dat");
try {
fileInput = new Scanner(inFile);
//Reads file contents
while (fileInput.hasNext()){
ipt1.add(fileInput.nextDouble()); //Adds the next Double to the ArrayList
System.out.print(ipt1.get(ipt1.size()-1)); //Prints out what you just got.
}
fileInput.close();
}
catch (FileNotFoundException e){
System.out.println(e);
}
//Sorting time
//This uses the built-in Array sorting.
Collections.sort(ipt1);
However, if you DO need to come up with a simple array in the end, but CAN use ArrayLists, you can add the following:
Double actualResult[] = new Double[ipt1.size()]; //Declare array
for(int i = 0; i < ipt1.size(); ++i){
actualResult[i] = ipt1.get(i);
}
Arrays.sort(actualResult[]);
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
public class SortNumberFromFile {
public static void main(String[] args) throws IOException {
BufferedReader br = null;
try {
System.out.println("Started at " + LocalDateTime.now());
br = new BufferedReader(new FileReader("/folder/fileName.csv"));//Read data from file named /folder/fileName.csv
List<Long> collect = br.lines().mapToLong(a -> Long.parseLong(a)).boxed().collect(Collectors.toList());//Collect all read data in list object
Collections.sort(collect);//Sort the data
writeRecordsToFile(collect, "/folder/fileName.txt");//Write sorted data to file named /folder/fileName.txt
System.out.println("Ended at " + LocalDateTime.now());
}
finally {
br.close();
}
}
public static <T> void writeRecordsToFile(Collection<? extends T> items, String filePath) {
BufferedWriter writer = null;
File file = new File(filePath);
try {
if(!file.exists()) {
file.getParentFile().mkdirs();
file.createNewFile();
}
writer = new BufferedWriter(new FileWriter(filePath, true));
if(items != null && items.size() > 0) {
for(T eachItem : items) {
if(eachItem != null) {
writer.write(eachItem.toString());
writer.newLine();
}
}
}
} catch (IOException ex) {
}finally {
try {
writer.close();
} catch (IOException e) {
}
}
}
}

FileNotFound exception while files already exist

this code couldn't find the files that the buffered reader is supposed to read from it and i have the files in the src folder in eclipse project and it still doesn't read from file so does anybody have any idea about what the problem is.
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.math.*;
import java.util.ArrayList;
public class Encrypt {
public static ArrayList<String> data = new ArrayList<String>();
public static BigInteger [] keys = new BigInteger[3];
public static BigInteger n;
public static double e;
public static BigInteger d;
public static String line;
public static String result;
public static String [] temp;
public static BigInteger tempVar;
public static BigInteger tempResult;
public static int tempVar2;
public static void encryption(ArrayList<String> data) throws IOException{
for (int i = 0; i<data.size(); i++){
if(data.get(i)!= null){
temp = new String[data.get(i).split(" ").length];
temp = data.get(i).split(" ");
for(int j = 0; j<temp.length;j++){
for (int k = 0; k< temp[j].length(); k++){
tempVar2 = (int)temp[j].charAt(k);
tempVar=BigInteger.valueOf((long)Math.pow(tempVar2,e));
tempResult = (tempVar.remainder(n));
result =""+ tempResult;
LogEncrypt(result);
}
}
}
}
}
public static void read() throws IOException{
try {
BufferedReader br = new BufferedReader(new FileReader("plainText.txt"));
System.out.println(br.ready());
while ((line = br.readLine()) != null) {
data.add(br.readLine());
}
System.out.println("done with text");
} catch (FileNotFoundException e) {
System.out.println("please add the text file");
e.printStackTrace();
}
try {
BufferedReader ba = new BufferedReader(new FileReader("Key.txt"));
System.out.println(ba.ready());
int i =0;
while ((line = ba.readLine()) != null) {
keys[i] = new BigInteger(ba.readLine());
i++;
}
n = keys[0];
e = keys[1].doubleValue();
d = keys[2];
System.out.println("done with key");
} catch (FileNotFoundException e) {
System.out.println("please add the key file");
e.printStackTrace();
}
}
public static void LogEncrypt(String result) throws IOException {
BufferedWriter out = new BufferedWriter(new FileWriter("output.txt"));
try {
out.write(result);
out.newLine();
} catch(IOException e1) {
System.out.println("Error during reading/writing");
} finally {
out.close();
}
}
public static void main(String[]args) throws IOException{
read();
encryption(data);
}
}
Put the file outside of the src, or at least add "src/" to the file location

Categories

Resources