 |
AppletTalk.com Java discussions newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Danny Gopie Guest
|
Posted: Mon Nov 29, 2004 7:17 pm Post subject: specific values in loops |
|
|
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
|
Posted: Mon Nov 29, 2004 9:01 pm Post subject: Re: specific values in loops |
|
|
"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
|
Posted: Mon Nov 29, 2004 9:09 pm Post subject: Re: specific values in loops |
|
|
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
|
Posted: Mon Nov 29, 2004 9:59 pm Post subject: Re: specific values in loops |
|
|
"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
|
Posted: Mon Nov 29, 2004 10:41 pm Post subject: Re: specific values in loops |
|
|
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
|
Posted: Mon Nov 29, 2004 10:45 pm Post subject: Re: specific values in loops |
|
|
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
|
Posted: Mon Nov 29, 2004 11:41 pm Post subject: Re: specific values in loops |
|
|
"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 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
|
Posted: Tue Nov 30, 2004 5:35 am Post subject: Re: specific values in loops |
|
|
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 |
|
 |
|
|
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
|
|