We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › VariableDeclaratorId error Spliting array object
Pages: 1 2 
VariableDeclaratorId error Spliting array object (Read 1918 times)
VariableDeclaratorId error Spliting array object
Mar 19th, 2007, 6:51pm
 
hi, am getting a VariableDeclaratorId expected error fromt he following code. The error is given on the line with code to split my arrayobject into ints and add to second array.

Code:

String lines[] = loadStrings("mosa-dtlz1-run1.txt");
Arraylist array2 = new Arraylist();

for (int i=0; i < lines.length; i++) {
Arraylist array1 = new Arraylist();

for (int i=0; i < lines.length; i++) {
array1.add(int(split(lines[i])));
}

array2.add(array1);
}

Any help would be appreciated.
Andrew
Re: VariableDeclaratorId error Spliting array obje
Reply #1 - Mar 19th, 2007, 7:42pm
 
maybe second lines.length should be lines[i].length and second i should be named differently than first one to avoid conflicts ...
Re: VariableDeclaratorId error Spliting array obje
Reply #2 - Mar 19th, 2007, 7:58pm
 
Hi, probably a good point, but still getting same error with this;

Code:

String lines[] = loadStrings("mosa-dtlz1-run1.txt");

Arraylist array2 = new Arraylist();

for (int i=0; i < lines.length; i++) {

Arraylist array1 = new Arraylist();

for (int x=0; x < lines[i].length; x++) {
array1.add(int(split(lines[i])));
}
array2.add(array1);
}


Cheers, Andrew.
Re: VariableDeclaratorId error Spliting array obje
Reply #3 - Mar 19th, 2007, 8:21pm
 
String lines[] = new String[]{"1 2 3 4 5 6 7","3 2 1 3 6 8  5  2 1 1"," 123  123  123 12 3 123 12 3"};

ArrayList array2 = new ArrayList();

for (int i=0; i < lines.length; i++) {

ArrayList array1 = new ArrayList();

for (int x=0; x < lines[i].length(); x++) {
 array1.add(int(split(lines[i])));
}
array2.add(array1);
}

something like that?

note that lines[i].length() is not split(lines[i]).length for each char in the line you are adding the whole array to array1 ... should probably be:



String lines[] = new String[]{"1 2 3 4 5 6 7","3 2 1 3 6 8  5  2 1 1"," 123  123  123 12 3 123 12 3"};

ArrayList array2 = new ArrayList();

for (int i=0; i < lines.length; i++) {

ArrayList array1 = new ArrayList();

int[] arr = int(split(lines[i]));
for (int x=0; x < arr.length; x++) {
   println( arr[x] );
 array1.add(new Integer(arr[x])); // add only takes an object
}
array2.add(array1);
}

best,
F
Re: VariableDeclaratorId error Spliting array obje
Reply #4 - Mar 19th, 2007, 8:38pm
 
What are you trying to accomplish, exactly?

This will put your file into a single ArrayList, though be aware that each line will be an element of the ArrayList. So, array1.get(0), will return the first line of the file.

Quote:


String[] lines = loadStrings("mosa-dtlz1-run1.txt");

ArrayList array1 = new ArrayList();

for(int i = 0; i < lines.length; i++)
{
 array1.add(int(split(lines[i])));
}

for(int i = 0; i < array1.size(); i++)
{
 print(array1.get(i));
}
   


Re: VariableDeclaratorId error Spliting array obje
Reply #5 - Mar 19th, 2007, 9:53pm
 
Thanks for the replies.

ddf: I have a file with input in this format;
Code:
0.000119060274316 0.004533755899569 1.329437814393679 
0.000181709578361 0.053673636137741 0.554154118814899
0.000306569115435 0.010740510485753 0.574698355340854
0.000586202187103 0.173153243640183 0.434270018703715
0.000657828856933 0.010389250744255 0.574698355340854
0.000703266494223 0.006730556338451 0.536801961479428
0.001004139105338 0.039113795752666 0.504117849454098
0.001515971252124 0.003136844921760 1.329437814393679


I want to take each line and create a new object, with each integer as a variable of that object. I think the easiest way is to provide my constructor with an array object that has the ints of that line. Therefore im trying to get an Array(list) of Arrays that contain the ints for a single line of the input file.

Fjen: I have tried your code, and only get an output of continuous '0' zeros to the command line, a result of i presume the println (arr[x]); line.

Hopefully ive mad it a bit clearer what im trying to do.
Cheers, Andrew
Re: VariableDeclaratorId error Spliting array obje
Reply #6 - Mar 19th, 2007, 10:19pm
 
first, your values clearly are floats not ints ! so if you run my code against your values it sure will give 0s and 1s ...

to solve your problem:

Code:


class myObjectXYZ
{
myObjectXYZ (float x, float y, float z) {
// do something here ...
}
}

String[] lines = new String[]{
"0.000119060274316 0.004533755899569 1.329437814393679",
"0.000181709578361 0.053673636137741 0.554154118814899",
"0.000306569115435 0.010740510485753 0.574698355340854",
"0.000586202187103 0.173153243640183 0.434270018703715",
"0.000657828856933 0.010389250744255 0.574698355340854",
"0.000703266494223 0.006730556338451 0.536801961479428",
"0.001004139105338 0.039113795752666 0.504117849454098",
"0.001515971252124 0.003136844921760 1.329437814393679"
};

myObjectXYZ[] objects = new myObjectXYZ[lines.length];

for ( int i=0; i < lines.length; i++ )
{
float[] vals = float(split(lines[i]));
objects[i] = new myObjectXYZ( vals[0], vals[1], vals[2] );
}


alternativly you sure can store the values in an array:

Code:

float[][] fvalues = new float[lines.length][0];

for ( int i=0; i < lines.length; i++ )
{
float[] vals = float(split(lines[i]));
fvalues[i] = new float[]{ vals[0], vals[1], vals[2] };
}


F
Re: VariableDeclaratorId error Spliting array obje
Reply #7 - Mar 19th, 2007, 11:06pm
 
Silly me, trying to hard to concentrate on a new language i forget little things like that, Cheers.

Tried your new code, but when i put a

println(fvalues[i]); loop at the end to see what is getting stored i get a very odd output...

Code:
0.06573511
0.3212193
0.15728137
0.0662376
0.2401225
0.27938533
0.06710464
0.0782634
0.44037738
0.06788073
0.10928742
0.4085773
0.06985963
0.4329182
0.08296762
0.07001102
0.5030778
0.010099506
0.070448615
0.12959832
0.3856985
0.07121431...


definitely not the input it was given. Any ideas?

Also your code would only allow for an input that had 3 values per line, would i need to loop the
fvalues[i] = new float[]
code x number of times depending on x number of variables per line?

Thanks for the help. Andrew
Re: VariableDeclaratorId error Spliting array obje
Reply #8 - Mar 20th, 2007, 12:14am
 
dunkster wrote on Mar 19th, 2007, 11:06pm:
Also your code would only allow for an input that had 3 values per line, would i need to loop the
fvalues[i] = new float[]
code x number of times depending on x number of variables per line


No, you could actually just assign the result of float(split(lines[i])) to fvalues[i].
Re: VariableDeclaratorId error Spliting array obje
Reply #9 - Mar 20th, 2007, 12:22am
 
Indeed i could, thank you. This output is still puzzling me though.
Re: VariableDeclaratorId error Spliting array obje
Reply #10 - Mar 20th, 2007, 7:18am
 
try double instead of float ...

F
Re: VariableDeclaratorId error Spliting array obje
Reply #11 - Mar 20th, 2007, 11:18am
 
hi, unfortunately split() will not cast into a double, so the following line will not work with doubles...

            double[] vals = double(split(lines[i]));

Cheers.
Re: VariableDeclaratorId error Spliting array obje
Reply #12 - Mar 20th, 2007, 11:25am
 
right, here's how:

String[] svals = split(lines[i]);
double[] dvals = new double[svals.length];
for ( int s=0; s < svals.length; s++ )
{
   dvals[s] = Double.parseDouble(svals[s]);
}

F
Re: VariableDeclaratorId error Spliting array obje
Reply #13 - Mar 20th, 2007, 6:52pm
 
ok, have tried your code and am still getting a similar output as before. Its returning 15000+ values from an input file which has only 400 or so. This is the current code;
Code:
String lines[] = loadStrings("mosa-dtlz1-run1.txt");
for ( int i=0; i < lines.length; i++ ) {

String[] svals = split(lines[i]);
double[] dvals = new double[svals.length];
for ( int s=0; s < svals.length; s++ ) {
dvals[s] = Double.parseDouble(svals[s]);
}

for ( int x=0; x < svals.length; x++ ) {
println(dvals[x]);
}
}
Re: VariableDeclaratorId error Spliting array obje
Reply #14 - Mar 20th, 2007, 7:28pm
 
i don't get what you are saying, the problem that it's returning more values than there are in the file is new ...

can you please post the data-file somewhere?

F
Pages: 1 2