AppletTalk.com Forum Index AppletTalk.com
Java discussions newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

specific values in loops

 
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java Help
View previous topic :: View next topic  
Author Message
Danny Gopie
Guest





PostPosted: Mon Nov 29, 2004 7:17 pm    Post subject: specific values in loops Reply with quote



Hello

I want to print only the values in this loop that are smaller then the give
value "aantal"

Now it just print true, becoz System.out.println(a < aantal); is a boolean.
But how do i change this?

Danny

public class e211 {
public static void main(String[] args) {

String saantal = JOptionPane.showInputDialog(null,"Voer een getal
in","invoer getal",JOptionPane.QUESTION_MESSAGE);
int aantal = Integer.parseInt(saantal);

for ( int i = 0; i < 10; i++) {
int a = i * aantal;
if (a < aantal){
System.out.println(a < aantal);}




}
}}


Back to top
VisionSet
Guest





PostPosted: Mon Nov 29, 2004 9:01 pm    Post subject: Re: specific values in loops Reply with quote




"Danny Gopie" <ouygiytf (AT) jhgvkuy (DOT) nl> wrote

Quote:
Hello

I want to print only the values in this loop that are smaller then the
give
value "aantal"

System.out.println(a < aantal ? aantal : "");

--
Mike W




Back to top
Oscar kind
Guest





PostPosted: Mon Nov 29, 2004 9:09 pm    Post subject: Re: specific values in loops Reply with quote



Danny Gopie <ouygiytf (AT) jhgvkuy (DOT) nl> wrote:
Quote:
I want to print only the values in this loop that are smaller then the give
value "aantal"

Now it just print true, becoz System.out.println(a < aantal); is a boolean.
But how do i change this?


public class e211 {
public static void main(String[] args) {

String saantal = JOptionPane.showInputDialog(null,"Voer een getal
in","invoer getal",JOptionPane.QUESTION_MESSAGE);
int aantal = Integer.parseInt(saantal);

for ( int i = 0; i < 10; i++) {
int a = i * aantal;
if (a < aantal){
System.out.println(a < aantal);}
}
}}

Which values do you want to print? I see three values: i, a and aantal.

You can change the output by using a different expression than
(a < aantal). Or did you mean (a + " < " + aantal)?


--
Oscar Kind http://home.hccnet.nl/okind/
Software Developer for contact information, see website

PGP Key fingerprint: 91F3 6C72 F465 5E98 C246 61D9 2C32 8E24 097B B4E2

Back to top
VisionSet
Guest





PostPosted: Mon Nov 29, 2004 9:59 pm    Post subject: Re: specific values in loops Reply with quote


"VisionSet" <spam (AT) ntlworld (DOT) com> wrote


Quote:

System.out.println(a < aantal ? aantal : "");


Doh:

System.out.println(a < aantal ? a : "");

--
Mike W




Back to top
Danny Gopie
Guest





PostPosted: Mon Nov 29, 2004 10:41 pm    Post subject: Re: specific values in loops Reply with quote

Hi Oscar
Well, i want to print the value of a. But only the values of "a" that are
smaller then the input var "aantal" .What kind of changes do you mean with
the output?

Danny
Quote:

Which values do you want to print? I see three values: i, a and aantal.

You can change the output by using a different expression than
(a < aantal). Or did you mean (a + " < " + aantal)?


--
Oscar Kind http://home.hccnet.nl/okind/
Software Developer for contact information, see website

PGP Key fingerprint: 91F3 6C72 F465 5E98 C246 61D9 2C32 8E24 097B B4E2



Back to top
Danny Gopie
Guest





PostPosted: Mon Nov 29, 2004 10:45 pm    Post subject: Re: specific values in loops Reply with quote

Thanks, but can you explain what ? does? Coz i get an error:

e211.java [22:1] incompatible types
found : java.lang.String
required: int
System.out.println(a < aantal ? a : "");

1 error
Errors compiling main.
Quote:

System.out.println(a < aantal ? aantal : "");


Doh:

System.out.println(a < aantal ? a : "");

--
Mike W






Back to top
VisionSet
Guest





PostPosted: Mon Nov 29, 2004 11:41 pm    Post subject: Re: specific values in loops Reply with quote


"Danny Gopie" <ouygiytf (AT) jhgvkuy (DOT) nl> wrote

Quote:
Thanks, but can you explain what ? does? Coz i get an error:

e211.java [22:1] incompatible types
found : java.lang.String
required: int
System.out.println(a < aantal ? a : "");

1 error
Errors compiling main.

funny no problem on 1.5.

try this:

System.out.println(a < aantal ? Integer.toString(a) : "");

or this:

if(a < aantal ) System.out.println(a);

The 1st is the 'ternary operator'

booleanExp ? exprA : exprB

it basically says if booleanExp evaluates to true make the whole statement
evaluate to exprA otherwise exprB.

It is a condensed form of an if/else statement.

Your System.out.println() is overloaded to accept just about anything and as
such I thought my odd, confusing and unhelpful Wink though untested reply
would be okay. Just tried it on mine though and it is fine.

Normally you would do something like:

String s = a < aantal ? Integer.toString(a) : ""

my original

String s = a < aantal ? a : ""

is nonsense because if it evaluates true then it tries to assign an int to a
String, but because the System.out.println is overloaded it should be okay.

--
Mike W



Back to top
Oscar kind
Guest





PostPosted: Tue Nov 30, 2004 5:35 am    Post subject: Re: specific values in loops Reply with quote

Danny Gopie <ouygiytf (AT) jhgvkuy (DOT) nl> wrote:
Oscar Kind <oscar (AT) danwa (DOT) net> wrote:
Quote:

Which values do you want to print? I see three values: i, a and aantal.

You can change the output by using a different expression than
(a < aantal). Or did you mean (a + " < " + aantal)?

Well, i want to print the value of a. But only the values of "a" that are
smaller then the input var "aantal" .What kind of changes do you mean with
the output?


You already only print something when a < aantal. So that's covered.
At the moment, you print the value of (a < aantal), which is a boolean.
To print the value of a, you would change the println call to an
expression that evaluates to a...


PS: Please try NOT to top-post in the future (it is the norm on this
newsgroup, and mixing top-posting and bottom-posting is horrible for
readability).
PPS: Please keep the attribution; If "foo" said something, keep the
generated line that states that "foo" said it.


--
Oscar Kind http://home.hccnet.nl/okind/
Software Developer for contact information, see website

PGP Key fingerprint: 91F3 6C72 F465 5E98 C246 61D9 2C32 8E24 097B B4E2

Back to top
Display posts from previous:   
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java Help All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.