Go Back   Velocity Reviews > Newsgroups > Java
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply

Java - Stupid (yet simple) error

 
Thread Tools Search this Thread
Old 11-10-2003, 05:37 PM   #1
Default Stupid (yet simple) error


I have this code:

int i = -5;
while ((i<0) & (i>=100))
i = Integer.parseInt(stdin.readLine());
// stdin is a BufferedReader

It's supposed to repeatedly read the users input until the user types a
number between 0 and 100. However, whenever I start the program it accepts
that i=-5 and an error occurs because of this.
Why?

Gajo




gajo
  Reply With Quote
Old 11-10-2003, 05:59 PM   #2
Bjorn Abelli
 
Posts: n/a
Default Re: Stupid (yet simple) error

"gajo" wrote...

> int i = -5;
> while ((i<0) & (i>=100))
> i = Integer.parseInt(stdin.readLine());
> // stdin is a BufferedReader
>
> It's supposed to repeatedly read the users input until
> the user types a number between 0 and 100.


The condition you have written can *never* evaluate to true. (i can never be
*both* less than zero *and* greater or equal to 100).

I guess you're looking for:

while ((i < 0) || (i >= 100))

Bjorn A




Bjorn Abelli
  Reply With Quote
Old 11-10-2003, 08:58 PM   #3
gajo
 
Posts: n/a
Default Re: Stupid (yet simple) error
I should probably say something like "arrrrgh!" now, right?




gajo
  Reply With Quote
Old 11-10-2003, 10:11 PM   #4
nos
 
Posts: n/a
Default Re: Stupid (yet simple) error
sok, many of us have been in your shoes before


"gajo" <> wrote in message news:boou74$i4$...
> I should probably say something like "arrrrgh!" now, right?
>
>





nos
  Reply With Quote
Old 11-24-2003, 02:34 PM   #5
Chris Smith
 
Posts: n/a
Default Re: Stupid (yet simple) error
gajo wrote:
> I have this code:
>
> int i = -5;
> while ((i<0) & (i>=100))
> i = Integer.parseInt(stdin.readLine());
> // stdin is a BufferedReader
>
> It's supposed to repeatedly read the users input until the user types a
> number between 0 and 100. However, whenever I start the program it accepts
> that i=-5 and an error occurs because of this.
> Why?


I don't know what you mean by "an error occurs", but it's clearly not
possible to satisfy the condition "((i < 0) & (i >= 100))", so the input
will never actually happen. Perhaps you meant "|" (and it's better to
use the short-cutting operators unless you have a specific reason not
to, so I'd use "||" instead.)

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation


Chris Smith
  Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
please help: simple java coding error 'cannot be referenced from a static context' clm90 General Help Related Topics 0 10-17-2009 06:49 AM
Stupid Runner Error GMB General Help Related Topics 3 01-23-2008 09:51 PM
Need help on Modelsim VHDL syntax? ASAP:) kaji General Help Related Topics 0 03-14-2007 10:43 PM
Need help on a Modelsim VHDL Syntax? ASAP:) kaji Software 0 03-14-2007 10:43 PM
Need Help on a Modelsim VHDL Syntax....ASAP:) kaji Hardware 0 03-14-2007 10:41 PM




SEO by vBSEO 3.3.2 ©2009, Crawlability, Inc.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46