PDA

View Full Version : Java with..do?



Sir R. M8gic1an
10-07-2009, 06:51 PM
if you are familiar with pascal's With..do then you know how useful it can be.

I suspect there is a similar feature in java, "use" ? i tried searching google but as you can imagine "java use" turned up a lot of results that were absolutely useless for my search :p

Does anyone know it?

Cheers,
~RM

Method
10-07-2009, 09:23 PM
There isn't anything like that as far as I know. What are you trying to do that can't be done by referencing an object and setting a value or calling one of its methods (perhaps even a constructor)?

mixster
10-07-2009, 09:51 PM
I thought it had somewhat natural "with".."do" in the form that you can access object specific variables etc in methods without mentioning the object itself as long as the identifier wouldn't be ambiguous in the context?

Sir R. M8gic1an
10-07-2009, 10:11 PM
my mistake, i was trying to use something like with..do on an array, cus the name was just so long it pissed me off to write it out, only now did i realize it was an array and therefore wouldn't be able to do this :p

~RM

Nava2
10-07-2009, 11:04 PM
You could import the class?

ape
10-07-2009, 11:31 PM
To the best of my knowledge, Java does not have With.. do. You would need to use something like this...

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

}
You can also do this if the array is an object array...



for (theObject g : objectArray) {

}

Method
10-08-2009, 05:33 AM
To the best of my knowledge, Java does not have With.. do. You would need to use something like this...

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

}
You can also do this if the array is an object array...



for (theObject g : objectArray) {

}

You can use a for each loop with any type of array, Collection, or other class that implements the Iterable interface.

traitors
10-08-2009, 08:11 AM
just used this in my lab


do{
//code here
}
while(//whatever state you are trying to change maintain){
//do stuff
}

you can even throw in an if statement in the while section


do{
}
while(){
if()
}

might be of some use

Method
10-08-2009, 09:46 AM
just used this in my lab


do{
//code here
}
while(//whatever state you are trying to change maintain){
//do stuff
}

you can even throw in an if statement in the while section


do{
}
while(){
if()
}

might be of some use

That's not how you use a do while loop.

do {
stuffHere();
} while (true);

traitors
10-08-2009, 04:15 PM
That's not how you use a do while loop.

do {
stuffHere();
} while (true);


lmao made the exact same mistake in my lab ah the joys of java

Nadeem
10-09-2009, 10:05 PM
I don't think there is a similar feature in java like with..do. (I think pascal/delphi are the only languages to have this...? not sure...)



~NS

senrath
10-09-2009, 10:54 PM
I don't think there is a similar feature in java like with..do. (I think pascal/delphi are the only languages to have this...? not sure...)



~NS

The closest would be a for..each loop, which is used like this (ape already mentioned it):
public class ForEachExample {

public static void main(String[] args)
{
String[] sArray = {"Hi", "This", "Is", "An", "Example"};
for(String s : sArray)
{
System.out.println(s);
}
}
}

Which outputs this:

Hi
This
Is
An
Example

Nadeem
10-10-2009, 12:56 AM
Do you know what with..do is? :p

Loops can be done in tons of different ways in any language, with..do can't be.

this is with..do to refresh memory:

type
TExampleType = record
intEx: Integer;
end;
var ExampleType: TExampleType;
begin
// Regular way to use the type
ExampleType.intEx = 0;

// Using with..do
with ExampleType do
begin
intEx = 0;
end;
end;




~NS

senrath
10-10-2009, 02:25 AM
Do you know what with..do is? :p

Loops can be done in tons of different ways in any language, with..do can't be.

this is with..do to refresh memory:

type
TExampleType = record
intEx: Integer;
end;
var ExampleType: TExampleType;
begin
// Regular way to use the type
ExampleType.intEx = 0;

// Using with..do
with ExampleType do
begin
intEx = 0;
end;
end;




~NS
I know what with..do does. I said for..each was the closest that Java does, I never said it was the same thing.

Nadeem
10-10-2009, 02:31 AM
I don't see how its close...? with..do doesn't deal with an array, it deals with an object type? the closest thing would be to reference it using a shorter name (basically what Method, and mixster were saying). But it dun matter RM needed it for arrays anyway as he stated in his 2nd post O.o...



~NS

senrath
10-10-2009, 02:42 AM
I don't see how its close...? with..do doesn't deal with an array, it deals with an object type? the closest thing would be to reference it using a shorter name (basically what Method, and mixster were saying). But it dun matter RM needed it for arrays anyway as he stated in his 2nd post O.o...



~NS

It's not really close. It's just one of the closest.

Nadeem
10-10-2009, 03:06 AM
^^ your post helped RM more than mine styll (:



~NS

arash
10-15-2009, 12:48 AM
The programming in this thread was just depressing, people want to take shortcuts so they don't learn the language.

Yes I'm being condescending because I can, and from what I have seen each person in this thread is an amateur at best.

Nava2
10-15-2009, 12:51 AM
The programming in this thread was just depressing, people want to take shortcuts so they don't learn the language.

Yes I'm being condescending because I can, and from what I have seen each person in this thread is an amateur at best.

Several of us are, yes. You didn't really need to be like that.

Was your point here really that necessary to the topic on hand?

Method
10-15-2009, 03:49 AM
His recent posts regarding scripting in SCAR are quite funny when you take into account his post made here about the rest of our abilities.

EDIT: Wow, his older posts are good too.

Wizzup?
10-15-2009, 09:27 AM
The programming in this thread was just depressing, people want to take shortcuts so they don't learn the language.

Well, that's just complete nonsense.


Yes I'm being condescending because I can, and from what I have seen each person in this thread is an amateur at best.

What's wrong with being an amateur?

n3ss3s
10-15-2009, 07:20 PM
I'd say there isn't one, haven't seen anything like that being used (In otherwords, I've seen stacks of Class.method)...

arash
10-16-2009, 08:03 PM
Yes I suck dick in SCAR, I fully admit that. However I don't want to turn this thread into a flame thread so I will actually contribute instead of flame.

Here we go....:fiery:

This is considered lazy and stylistically bad, this is not a preferred method.

public class ForEachExample {

public static void main(String[] args)
{
String[] sArray = {"Hi", "This", "Is", "An", "Example"};
for(String s : sArray)
{
System.out.println(s);
}
}
}

This is much more professional and shows programming poise.


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

}

senrath
10-16-2009, 08:08 PM
Yes I suck dick in SCAR, I fully admit that. However I don't want to turn this thread into a flame thread so I will actually contribute instead of flame.

Here we go....:fiery:

This is considered lazy and stylistically bad, this is not a preferred method.

public class ForEachExample {

public static void main(String[] args)
{
String[] sArray = {"Hi", "This", "Is", "An", "Example"};
for(String s : sArray)
{
System.out.println(s);
}
}
}

This is much more professional and shows programming poise.


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

}

I fail to see why using a straight for loop is "more professional" than using a for..each loop. Especially when it was an example of for..each loops.

arash
10-16-2009, 11:57 PM
I fail to see why using a straight for loop is "more professional" than using a for..each loop. Especially when it was an example of for..each loops.

Index manipulation cannot be done in a "for..each loop".

senrath
10-17-2009, 12:16 AM
Index manipulation cannot be done in a "for..each loop".

So? There was no index manipulation to be done in that example.

Wizzup?
10-17-2009, 11:17 AM
So? There was no index manipulation to be done in that example.

One should watch out with for...each loop in other cases - I generally try to avoid it. (But again, not relevant to the for loop in this case)


public class test {
public static void main(String[] args) {
// Declare array and init the vars.
int[] a = new int[10];
for(int i = 0; i < 10; i++)
a[i] = i * 2;

// This part will not affect the loop
// java basic types are simply copied
// in a for each loop. You want to use
// for(; ; ;) here. (objects are passed by reference)
for(int b : a) {
b += b;
}

for(int c : a) {
System.out.print(c + " ");
}
System.out.println();
}
}



$ vim test.java
$ javac test.java
$ java test
0 2 4 6 8 10 12 14 16 18

arash
10-17-2009, 02:40 PM
Good habits make good programmers,

For example in java all integer variables are initialized to 0 yet programmers still do int example=0 to build good habits.

A for each loop has many, many, issues index manipulation is just one of them.

Method
10-17-2009, 02:52 PM
Good habits make good programmers,

For example in java all integer variables are initialized to 0 yet programmers still do int example=0 to build good habits.

A for each loop has many, many, issues index manipulation is just one of them.

The compiler won't compile the class if you don't initialize a variable and try to access it, though. For example, this piece of code won't compile or work:

public class Test {

public static void main(String[] args) {
int val;
System.out.println(val);
}
}


Test.java:5: variable val might not have been initialized
System.out.println(val);
^
1 error

Wizzup?
10-17-2009, 03:08 PM
Variable might not have been initialized should only be a warning, not an error.

senrath
10-17-2009, 07:01 PM
Variable might not have been initialized should only be a warning, not an error.

Eclipse upgrades that one to an error. I dunno if others do or not.

Nava2
10-17-2009, 07:16 PM
drjava, the IDE I use for school, returns an error.

I agree though, it should be a warning rather than error. Its bad practice, kinda like in scar scripting when you just assume result is false. It CAN mess up, so its better not to.

Idk, my 0.02.

Nadeem
10-17-2009, 07:37 PM
I use drjava at school too... The javac itself returns an error for that.

I pointed this out on RM's other thread :p It is a good habit to initialize your variables before using them. But in scar not many people do it cuz its not enforced upon them to by the compiler.



~NS

Wizzup?
10-17-2009, 07:46 PM
I use drjava at school too... The javac itself returns an error for that.

I pointed this out on RM's other thread :p It is a good habit to initialize your variables before using them. But in scar not many people do it cuz its not enforced upon them to by the compiler.



~NS

Well, sometimes you don't have to initialize them, as they will simply be set by another method. But I am not sure if java correctly traces this.

senrath
10-17-2009, 07:50 PM
Well, sometimes you don't have to initialize them, as they will simply be set by another method. But I am not sure if java correctly traces this.

I'm pretty sure it does. But not 100% sure.

Nadeem
10-17-2009, 08:11 PM
I'm pretty sure it does. But not 100% sure.
It doesn't spit out the error if the variable is being initialized in the init() method or the main() method, after being declared globally.



~NS

arash
11-02-2009, 06:01 PM
You do not need to initialize variables in java but you should(horrible idea not to). However if for some strange silly reason you need to just add suppress statements to shut the compiler up.

Nava2
11-02-2009, 06:28 PM
You do not need to initialize variables in java but you should(horrible idea not to). However if for some strange silly reason you need to just add suppress statements to shut the compiler up.

Most compilers say otherwise...

Also, Wizzup did you mean that when you assign a variable to a method thats not initializing it? It is.. So that would be the same as setting an integer to 0. I'm sure you already knew that though.

Wizzup?
11-02-2009, 07:51 PM
Most compilers say otherwise...

Also, Wizzup did you mean that when you assign a variable to a method thats not initializing it? It is.. So that would be the same as setting an integer to 0. I'm sure you already knew that though.

No, I said what arash said.

arash
11-02-2009, 09:57 PM
Most compilers say otherwise...

Also, Wizzup did you mean that when you assign a variable to a method thats not initializing it? It is.. So that would be the same as setting an integer to 0. I'm sure you already knew that though.

The point of a suppress statement is to make the compiler not say anything. Wizzup was also perfectly clear in what he meant.

*P.S: Nice edit of my post wizzup xD.