LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-28-2011, 01:25 AM   #1
phoenixfire
Member
 
Registered: Nov 2006
Location: Australia
Distribution: Gentoo
Posts: 86

Rep: Reputation: 15
Java newbie needs help


Hello! I've just started programming at my university and I'm finding it a bit hard to get started. I've been given this for homework.
Quote:
Given 2 integers, a and b, print their sum. However, sums in the range 10..19 inclusive, are forbidden, so in that case just print 20
"

The problem I'm having is that i don't know how to tell java to print 20 when the value is in that range. I'm guessing it's an if else statement? Any help would be greatly appreciated. Thanks!
 
Old 07-28-2011, 01:27 AM   #2
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
Yes, just use an if and an else . There really isn't much more to it. If a condition is true, you do one thing and if it isn't, you do something else. You're even given the condition to test.

Edit: You do know how to form the test, right? I mean, do you know the operators you need to use in the test?

Last edited by Nylex; 07-28-2011 at 01:41 AM.
 
Old 07-28-2011, 01:39 AM   #3
phoenixfire
Member
 
Registered: Nov 2006
Location: Australia
Distribution: Gentoo
Posts: 86

Original Poster
Rep: Reputation: 15
So how do i specify the range? I've got
Quote:
if (n1 + n2) = (10..19)
then System.out.println ("The sum of this number is 20")
else System.out.println (n1 + n2);
But I'm getting a syntax error on token "=".
I've also tried == but that doesn't work either.
 
Old 07-28-2011, 01:44 AM   #4
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
You need to use == to test equality. Think about the range in two parts: n1 + n2 is greater than 10 and less than 19. Does that help now? Also, in Java, you don't use "then". The syntax is


Code:
if(condition)
 // Statement
else
 // Statement
or

Code:
if(condition)
{
 // Multiple statements
}
else
{
 // Multiple statements
}
 
Old 07-28-2011, 01:48 AM   #5
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
I'm not sure which book you're using (if any), but you might want to look at this. For this problem, you'll want to look at operators and control flow.
 
Old 07-28-2011, 01:50 AM   #6
devnull10
Member
 
Registered: Jan 2010
Location: Lancashire
Distribution: Slackware Stable
Posts: 572

Rep: Reputation: 120Reputation: 120
Code:
System.out.println(n1+n2<20?20:n1+n2);


But on a serious note, look up the different comparisons you can use, specifically less than (<), greater than (>), not equals (!=) etc etc... As a hint, this is how a java comparison of variables a and b would look:

Code:
if (a>b)
  System.out.println("a is greater than b");
else 
  System.out.println("a is not greater than b");
If you have more than one statement in the if block, youwill need to surround with parentheses:

Code:
if (a>b) {
  a=1;
  b=2;
  /*etc*/
}

Last edited by devnull10; 07-28-2011 at 01:53 AM.
 
Old 07-28-2011, 01:58 AM   #7
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
Quote:
Originally Posted by devnull10 View Post
Code:
System.out.println(n1+n2<20?20:n1+n2);
I wouldn't just post code like this without explaining how it works, as it will probably confuse a beginner.

FWIW, phoenixfire, this operator is called the ternary operator (at least in C; I assume it's the same here). It's a shorthand for if-else and the general form is

condition ? value if true : value if false

You test the condition and if it's true, the value before the colon is returned. If false, the value following the colon is returned.
 
Old 07-28-2011, 02:03 AM   #8
phoenixfire
Member
 
Registered: Nov 2006
Location: Australia
Distribution: Gentoo
Posts: 86

Original Poster
Rep: Reputation: 15
Thanks Nylex, i'm understanding if else a lot better now. I didn't realize you had to put the statements in {}'s. It can compile now, yay! I'll have a look through those Java tutorials. Looks really helpful.

Devnull10, could you explain that line?

Also, there must be way to enter a range instead of putting each number in individually?

I apologies for the basic questions and thank you for your patients.
 
Old 07-28-2011, 02:06 AM   #9
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
Quote:
Originally Posted by phoenixfire View Post
Thanks Nylex, i'm understanding if else a lot better now. I didn't realize you had to put the statements in {}'s. It can compile now, yay! I'll have a look through those Java tutorials. Looks really helpful.
You have to use curly braces if you have multiple statements. If it's just a single statement, you can leave them out.

Quote:
Devnull10, could you explain that line?
Have a look at my last post. I explained the construct there.

Quote:
I apologies for the basic questions and thank you for your patients.
Patience .
 
Old 07-28-2011, 02:37 AM   #10
phoenixfire
Member
 
Registered: Nov 2006
Location: Australia
Distribution: Gentoo
Posts: 86

Original Poster
Rep: Reputation: 15
Thanks you both very much for your help. After reading and understanding (well, i hope i do) all this i think you've put me ahead of my class.

My last question though is how do i enter a range of numbers?
eg.
if (n1 + n2 == *range of numbers*).
 
Old 07-28-2011, 02:51 AM   #11
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
Again, you need to break the condition into two parts: n1 + n2 is greater than 10 and n1 + n2 is less than 19. So, you need two tests in your if statement. Look at this.
 
Old 07-28-2011, 02:53 AM   #12
devnull10
Member
 
Registered: Jan 2010
Location: Lancashire
Distribution: Slackware Stable
Posts: 572

Rep: Reputation: 120Reputation: 120
Quote:
I wouldn't just post code like this without explaining how it works, as it will probably confuse a beginner.
It was a tongue in cheek reply which is why I explained fully the "other" way below.

You don't have to put statements in braces unless you have more than one statement in the if block, however I tend to do so anyway out of habit.

Quote:
Also, there must be way to enter a range instead of putting each number in individually?
Well, in this case you don't need to put a range in as all you are doing is checking whether the value is GREATER THAN 20.
So: "if (a>20)..." works fine.

If you wanted to check say a variable is in the range 10 - 20 inclusive then just use the AND operator (&&):

Code:
if (a>=10 && a<=20) {
  System.out.println("a is between 10 and 20 inclusive.");
}
 
Old 07-28-2011, 02:55 AM   #13
phoenixfire
Member
 
Registered: Nov 2006
Location: Australia
Distribution: Gentoo
Posts: 86

Original Poster
Rep: Reputation: 15
So just to confirm, there is no way at all to put in a range of numbers? I'm not talking about my question but in Java as a whole.
 
Old 07-28-2011, 04:15 AM   #14
devnull10
Member
 
Registered: Jan 2010
Location: Lancashire
Distribution: Slackware Stable
Posts: 572

Rep: Reputation: 120Reputation: 120
Well, when you say "a range of numbers" then strictly speaking, no, in the sense that you can't say:

Code:
if (a in (1 to 10)) ...
as you can in some languages. But... a range has an upper and lower bound, so you just check those. Ie, if you want to be in the range 1 to 10, you just ensure the number is not less than 1 and not greater than 10. So technically that is a range, you are just typing it differently!

Code:
if (a>=1&&a<=10)
  /*Number is in the range 1 - 10*/
The above is checking a number is in the range 1-10, you can of course do the opposite using an "OR" (||):

Code:
if (a<1||a>10)
  /*number is outside the range 1 - 10*/

Last edited by devnull10; 07-28-2011 at 04:19 AM.
 
Old 07-28-2011, 07:39 AM   #15
phoenixfire
Member
 
Registered: Nov 2006
Location: Australia
Distribution: Gentoo
Posts: 86

Original Poster
Rep: Reputation: 15
That does makes sense. I don't know why i couldn't wrap my brain around that before. I'd be lost without the help from both of you. Thanks again!

I'm sure i'll be back to ask some more questions in the near future. Hopefully i'll be able to answer some too very soon.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Newbie - How do I install Java? bcredeye Linux - Newbie 2 03-08-2011 06:14 PM
java servlet newbie joel2001k Programming 4 04-28-2009 02:07 AM
newbie java question lmellen Programming 6 06-30-2004 04:34 PM
Java Newbie Question patpawlowski Programming 6 02-27-2004 04:25 PM
java newbie stephenk Programming 2 07-02-2003 09:25 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:54 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration