Compare double information

Hi all
I have an array initalized as double.. see attached code..
Given double v1 stored in a gene.. = See section A

And given three different vectors/double stored in a map; which is a container of (3 different vectors).
These includes: Node; weight; Genes:

See section B.

Already I have a method that compare values:: myComparator (double[] a, double[] b)

My only problem or question is this!
How can I extract a vector  (double[] v1 = {184, 188, 49};)
from a..
And How can I extract a vector  weight vector: from Node: 01( 179.00 159.00 63.00 )
from b

And present both to myComparator???

If you need more info please let me know..
***************************************************************************************
Section A
***************************************************************************************

                  double[] v1 = {184, 188, 49};
		double[] v2 = {183, 170, 7};
		double[] v3 = {199, 77, 158};

								
		Cell[] cells = new Cell[5];
		cells[0] = new Cell("g1", v1);
		cells[1] = new Cell("g2", v2);
		cells[2] = new Cell("g3", v3);

		Cell g1 = cells[0];
		Cell g2 = cells[1];		
		Cell g3 = cells[2];
***************************************************************************************
Section B
***************************************************************************************

		Node[] map = contained the following info when initialized
		(
		Node 0 0
		weight vector: 119.00 159.00 63.00 
		Genes:
		)
		
		(
		Node 0 1
		weight vector: 179.00 159.00 63.00 
		Genes:
		)
		
		(
		Node 10
		weight vector: 166.00 159.00 63.00 
		Genes:
		)

Open in new window

ZURINETAsked:
Who is Participating?
 
for_yanConnect With a Mentor Commented:
Ok this is simple and works for everything:

           //  String v = "{ 179.00 159.00 63.00 }";

        String v = "( 188, 144, 14)";
        StringTokenizer t = new StringTokenizer(v,"{}(), ");

	double[] result = new double[3];
        result[0] = Double.parseDouble(t.nextToken());
            result[1] = Double.parseDouble(t.nextToken());
            result[2] = Double.parseDouble(t.nextToken());
	for(int i = 0;i < result.length;i++) {

         System.out.println(result[i] + " ");
	}

Open in new window

0
 
for_yanCommented:
You already had this solution from CEHJ for one of the previous questions,
you can add {} and I hope it should work for both cases with parentheses and braces
(let me check now):
public static double[] vectorize(String v) {
	String[] atoms = v.replaceAll("[() {}]", "").split(",");
	double[] result = new double[atoms.length];
	for(int i = 0;i < atoms.length;i++) {	
	    result[i] = Double.valueOf(atoms[i]);
	}
	return result;
    }

Open in new window

0
 
for_yanCommented:

Yes this same code  works both for parenthesis and braces
                   String v = "(184, 188, 49)";
	String[] atoms = v.replaceAll("[() {}]", "").split(",");
	double[] result = new double[atoms.length];
	for(int i = 0;i < atoms.length;i++) {
	    result[i] = Double.valueOf(atoms[i]);
         System.out.println(result[i] + " ");
	}

Open in new window


output:

184.0 
188.0 
49.0 

Open in new window

0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
ZURINETAuthor Commented:
May be I just need a break...
up for more than 14 hours.
0
 
for_yanCommented:
No sorry, that my code only works with commas.
Don't have time now.
I guess CEHJ will send you the right one for both.
0
 
for_yanCommented:
input:

( 188, 144, 14)

Open in new window



Output:
188.0 
144.0 
14.0 

Open in new window



input:

{ 179.00 159.00 63.00 }

Open in new window


output:


179.0 
159.0 
63.0 

Open in new window


0
 
objectsConnect With a Mentor Commented:
you should just need the following
nice and simple, and it'll parse both formats
public static double[] parseVector(String s) {
		double[] array = new double[3];
		int i = 0;
		Scanner scanner = new Scanner(s.substring(1));
		while (scanner.hasNextDouble()) {
			array[i++] = scanner.nextDouble();
		}
		return array;
	}

Open in new window

0
 
objectsCommented:
> And present both to myComparator???

VectorComparator comparator = new VectorComparator();
int compare = comparator.compare(parseVector("( 188, 144, 14)"), parseVector("{ 179.00 159.00 63.00 }
"));
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.