Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / November 2004

Tip: Looking for answers? Try searching our database.

specific values in loops

Thread view: 
Danny Gopie - 29 Nov 2004 19:17 GMT
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);}

}
}}
VisionSet - 29 Nov 2004 21:01 GMT
> 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 : "");

Signature

Mike W

VisionSet - 29 Nov 2004 21:59 GMT
> System.out.println(a < aantal ? aantal : "");

Doh:

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

--
Mike W
Danny Gopie - 29 Nov 2004 22:45 GMT
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.

>> System.out.println(a < aantal ? aantal : "");
>
[quoted text clipped - 4 lines]
> --
> Mike W
VisionSet - 29 Nov 2004 23:41 GMT
> Thanks, but can you explain what ? does? Coz i get an error:
>
[quoted text clipped - 5 lines]
> 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.

Signature

Mike W

Oscar kind - 29 Nov 2004 21:09 GMT
> I want to print only the values in this loop that are smaller then the give
> value "aantal"
[quoted text clipped - 15 lines]
> }
> }}

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)?

Signature

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

Danny Gopie - 29 Nov 2004 22:41 GMT
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

> 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 - 30 Nov 2004 05:35 GMT
Oscar Kind <oscar@danwa.net> wrote:

>> Which values do you want to print? I see three values: i, a and aantal.
>>
[quoted text clipped - 4 lines]
> 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.

Signature

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



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.