We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hello ladies and gents,
I am attempting to make a basic stop watch program that.
Here's the code:
String[] animals;
void setup() {
StopWatch.start();
animals = new String[3];
animals[0] = "Cat";
animals[1] = "Dog";
animals[2] = "Turtle";
StopWatch.stop();
print(StopWatch.time());
}
//void bubbleSort (String[] ar) {
// for (int i = 0; i < ar.length-1; i++) {
// for (int j = 0; j < ar.length-1; j++) {
// if (ar[j].compareTo(ar[j+1]) > 0) {
// swap (ar, j, j+1);
// // you need a swap method
// }
// }
// }
//}
class StopWatch {
long startTime, endTime;
void start () {
startTime = millis();
}
void stop() {
endTime = millis();
}
double time() {
return endTime - startTime;
}
void reset() {
startTime = 0;
endTime = 0;
}
}
I get the error: Cannot make a static reference to the non-static method start() from the type SSE.StopWatch on line 4 of the main window.
Answers
since it's class
you need
StopWatch watch = new StopWatch();
?
or so
Tweaked version: :D
Also take a look at this thread:
http://forum.processing.org/two/discussion/1725/millis-and-timer
Same tweaked version. But w/ currentTimeMillis() in place of nanoTime():
Thanks for the suggestions everybody! I can't believe I missed actually creating the Stop Watch. The things you miss when looking at your own code.
Yup, you've forgotten to instantiate the class 1st!.
However, in my tweaked versions, that's not needed at all! \m/
How does the may you filled the array with Strings work vs the way I did it? Also, what's the difference between a string and an array of Strings? A normal String would just hold the three terms while the Array would hold each String as an element?
startTime = endTime = 0;
Does this just say startTime is = to the value of endTime - which is = 0, and therefore they both = 0?
http://processing.org/reference/Array.html
http://processing.org/reference/arrayaccess.html
http://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html
I don't get what you mean, sorry!
A String object has a
char[]
field internally, which all of itschar
values are actually hold.While an array object provides a fixed # of slots to store values of a single data-type.
http://processing.org/reference/assign.html
The assignment operator
=
--- http://processing.org/reference/assign.htmldiff. from other operators, evaluates from right to left!
So in
startTime = endTime = 0;
, 1st0
is assigned to endTime, and returns that very same value.That returning evaluated value, which is
0
, is finally assigned to startTime too via the 2nd=
operator.Now all of them is
0
in the end! (*)Does the char function of a String access the character at that location or the whole word?
How does assigning values to the Array of Strings using {"Thing1", "Thing2"} work vs Stringname[#] = "Thing1" Stringname[#] = "Thing2" etc
Can the values still be accessed like normal for an Array when using option 1?
There's no method named char in the String class!
I said that a String object has a field of type
char[]
!See its available methods for yourself:
http://docs.oracle.com/javase/8/docs/api/java/lang/String.html
Perhaps you're actually interested in the charAt() method:
http://docs.oracle.com/javase/8/docs/api/java/lang/String.html#charAt-int-
Ok.
My main concern now is the difference between
and
String[] animals[#] = ("Things");
Also, why does the former not require new ?
By reading this:
would you say that I should be actually creating a String array at random?
It's 1 of 2 Java's special instantiations w/o
new
. The other is called string literal.Read the links I've posted to know further!
In order to create the structure, you're gonna need actual data elements.
To find a bunch of it, perhaps you're gonna need some text file and pick a specified # of random words outta it?
Or even from a URL?
So do you even need the new when making an array of String?
I believe you're mixing up array & String data-types. :-?
The
{}
w/onew
is a special alternative array object instantiation.And
""
w/onew
is the same thing, but for String objects!The
{}
is instantiating the array, whose reference is about to be stored in variable animals.Now, each of those
""
is a String object instantiation. And the wrappedchar
values are stored in them.And finally the just instantiated String objects' references are stored in the array's indexed "slots"!
I may be mixing them up, but aren't I making an Array of Strings in my code?
Yes you are:
String[]
is an array type which stores references for String objects! :-B