SRL Archive - Java Help and Tutorials

Java with..do?


Sir R. M8gic1an

2009-10-07 14:51:07

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

2009-10-07 17:23:56

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

2009-10-07 17:51:07

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

2009-10-07 18:11:44

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

2009-10-07 19:04:26

You could import the class?

ape

2009-10-07 19:31:33

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

2009-10-08 01:33:34

From: ape
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

2009-10-08 04:11:08

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

2009-10-08 05:46:10

From: traitors
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

2009-10-08 12:15:19

From: Method
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

2009-10-09 18:05:24

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

2009-10-09 18:54:44

From: Nadeem
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

2009-10-09 20:56:07

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

2009-10-09 22:25:08

From: Nadeem
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

2009-10-09 22:31:03

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

2009-10-09 22:42:41

From: Nadeem
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

2009-10-09 23:06:39

^^ your post helped RM more than mine styll (:



~NS

arash

2009-10-14 20:48:09

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

2009-10-14 20:51:42

From: arash
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

2009-10-14 23:49:05

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?

2009-10-15 05:27:09

From: arash
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

2009-10-15 15:20:34

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

2009-10-16 16:03:47

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

2009-10-16 16:08:36

From: arash
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

2009-10-16 19:57:35

From: senrath
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

2009-10-16 20:16:12

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


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

Wizzup?

2009-10-17 07:17:50

From: senrath
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

2009-10-17 10:40:17

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

2009-10-17 10:52:15

From: arash
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?

2009-10-17 11:08:20

Variable might not have been initialized should only be a warning, not an error.

senrath

2009-10-17 15:01:52

From: Wizzup?
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

2009-10-17 15:16:54

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

2009-10-17 15:37:31

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?

2009-10-17 15:46:25

From: Nadeem
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

2009-10-17 15:50:14

From: Wizzup?
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

2009-10-17 16:11:30

From: senrath
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

2009-11-02 13:01:37

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

2009-11-02 13:28:09

From: arash
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?

2009-11-02 14:51:36

From: Nava2
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

2009-11-02 16:57:09

From: Nava2
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.