SRL Archive - Java Help and Tutorials

Help: Basic java stuff


Cartmann

2010-02-20 15:20:18

Hi!, I'm going througe the big javabook, and can't get one of my assignments to compile! =0

public class Age
{
public static void main(String[] arg)
{
int age;
age = 15;

if (age >= 18)
System.out.println("Your an adult.");
else
System.out.println("Your not an adult.");
if (18< age >=15) //Issue
System.out.println("But you may fuck, hell yeah!");
else
System.out.println("Dam dam dammm no womans 4 you my little amish mate");

System.out.println("You are " + age + " years old.");
}
}
Would be glad if someone could tell me how to make my 'age' go between these two bars? thanks!

Sex

2010-02-20 15:26:39

Maybe do 18 < age && 15 <= age?

senrath

2010-02-20 15:28:23

Yeah. You can't combine two boolean expressions like that. You have to do (age < 18 && age >= 15)

Diddy Kong

2010-02-20 15:38:06

Be sure to use braces on all your if and if-else statements

Cartmann

2010-02-20 15:46:05

Thanks, It's working just fine now :)

BTW, I've tried making some decent grammar in one of my other assignments, well, let's just say it didn't work out as intended!:

public class eurodollar
{
public static void main(String[] arg)
{
double commission, $, euro, result;
String dollars;
{
$ = 1234;
dollars = ("dollars");
if($=1)
dollars = ("dollar");
if($>1)
dollars= ("cents");
if($=0.01)
dollars= ("cent");

euro = $*0.74;
commission = euro*0.02;
if (commission <0.5)
commission = 0.5;
result = euro-commission;
System.out.println($+" "+dollars+" is equald to "+result+" euro, with the commission of "+commission);

}
}
}

i luffs yeww

2010-02-20 15:47:33

From: GreenText
Be sure to use braces on all your if and if-else statements


If there's only one function, it's redundant, amirite?

So technically they aren't needed here, but they'd help.

senrath

2010-02-20 15:53:43

From: Cartmann
Thanks, It's working just fine now :)

BTW, I've tried making some decent grammar in one of my other assignments, well, let's just say it didn't work out as intended!:


You need to use ==, not just =, when you're comparing things.

From: i luffs yeww
If there's only one function, it's redundant, amirite?

So technically they aren't needed here, but they'd help.


Yes, but it's better to get into the habit of always doing it. It helps improve readability, and if you always put the braces in, you don't have to worry about forgetting them when you need to use them.

i luffs yeww

2010-02-20 16:01:17

From: senrath
Yes, but it's better to get into the habit of always doing it. It helps improve readability, and if you always put the braces in, you don't have to worry about forgetting them when you need to use them.


:p I meant to add that, but somehow I ended at "it'd help." x]

Cartmann

2010-02-20 16:04:02

Ofc senrath, it's stated in the javabook just above the assignment itself >.< Think I'll might head towards bed instead :P
But thanks for your assistance guys!

- And what do you mean about "Braces"?

senrath

2010-02-20 16:07:48

From: Cartmann
Ofc senrath, it's stated in the javabook just above the assignment itself >.< Think I'll might head towards bed instead :P
But thanks for your assistance guys!

- And what do you mean about "Braces"?


Instead of
if(booleanvalue)
doStuff();

do

if(booleanvalue)
{
doStuff();
}

Diddy Kong

2010-02-20 16:10:13

From: i luffs yeww
If there's only one function, it's redundant, amirite?

So technically they aren't needed here, but they'd help.


This is Java syntax; Go learn it before you post back on this thread.

i luffs yeww

2010-02-20 16:21:45

From: GreenText
This is Java syntax; Go learn it before you post back on this thread.


I know this is Java.. :/ Try it, brah. :p It'll work.

void_hatred

2010-02-20 16:23:30

what i luffs yeww said is only partly true, you only require braces for the scope if zero to one expressions are in that scope, ie.

[PHP]
if (a) b;
if (a) { b; }
[/PHP]

are the same, however in some cases braces are needed, ie.

[PHP]
if (a)
System.out.writeln("ohai");

// and

if (a) { }
System.out.writeln("ohai");
[/PHP]

are different things, the first examples has no braces thus executes the next following expression

in the former example "ohai" would only be displayed if a had a truthy value, in contrast, "ohai" would always be displayed to the standard output stream in the latter example regardless of a's value

to make them the same you could do an "empty" expression, ie.

[PHP]
if (a) ;
[/PHP]

that would be perfectly valid as a "empty" statement as the semicolon denotes the end of an expression (or function call etc)

hope that helped and I haven't made a fool of myself haha :)

void_hatred

i luffs yeww

2010-02-20 16:32:17

From: void_hatred
what i luff yeww said is only partly true, you only require braces for the scope if zero to one expressions are in that scope, ie.

[PHP]
if (a) b;
if (a) { b; }
[/PHP]

are the same, however in some cases braces are needed, ie.

[PHP]
if (a)
System.out.writeln("ohai");

// and

if (a) { }
System.out.writeln("ohai");
[/PHP]

are different things, the first examples has no braces thus executes the next following expression

in the former example "ohai" would only be displayed if a had a truthy value, in contrast, "ohai" would always be displayed to the standard output stream in the latter example regardless of a's value

to make them the same you could do an "empty" expression, ie.

[PHP]
if (a) ;
[/PHP]

that would be perfectly valid as a "empty" statement as the semicolon denotes the end of an expression (or function call etc)

hope that helped and I haven't made a fool of myself haha :)

void_hatred


:p Why use PHP tags instead of Java? ;)


if (a) System.out.println("wat"); /*Also, void, why writeln is in SCAR, not Java, right? Sorry, I dunno. I don't really like Java.*/

Is what I said would work, and it will.


if (a) System.out.println("wat"); if (b) /*this is an example of what wouldn't work if you're wanting a && b, which is different from what I said*/ System.out.println("lolwut");

Hopefully that makes sense. I said that if it's only one function inside the if statement, then the curly brackets aren't necessary. But


if (a)
System.out.println("wat");
if (b)
System.out.println("lolwut");
/*Assuming that you only want to print "lolwut" if both a and b are true*/

is entirely different. ;) The above snippet would require nesting.