Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Convert int to a bool

  1. #1
    Member
    Join Date
    Sep 2013
    Posts
    64
    Thanks
    24
    Thanked 0 Times in 0 Posts

    Default Convert int to a bool

    How do I make this work?

    int i = 0;
     
    do
    {
              System.out.println("blah blah blah");
              Scanner input = new Scanner(system.in);
              i = input.nextInt();
     
              if (i != 0)
                   i = 1
     
    } while (i);

    I want the program to say "okay, this 'i' is a 1, so that means TRUE, so we'll keep going" or "nope, this 'i' is a 0, that's FALSE, so let's exit the loop".

    It seems really hard?


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,438
    My Mood
    Hungover
    Thanks
    144
    Thanked 634 Times in 542 Posts

    Default Re: Convert int to a bool

    A boolean expression can be expressed like this:

    i==0
    i<76
    i!=17
    etc.

    These evaluate to a boolean value (true or false). You can use boolean values in things like if statements and while loops.

    You've already used a boolean expression in an if statement. You say you want to use a boolean expression in a while loop. What part of that is confusing you?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Member
    Join Date
    Sep 2013
    Posts
    64
    Thanks
    24
    Thanked 0 Times in 0 Posts

    Default Re: Convert int to a bool

    I guess I'm just used to C++ where you could say 'while(integer)'

    I feel like that's cleaner than always saying 'while (integer == 1)'.....

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,438
    My Mood
    Hungover
    Thanks
    144
    Thanked 634 Times in 542 Posts

    Default Re: Convert int to a bool

    I guess it's a personal preference, but while(i == 1) seems fine to me. In fact I would say it's better than just while(i) because it makes it clear that 1 is the only value it should continue for.

    For your code, I don't know why you're checking if(i != 0) and making it 1. That step becomes unnecessary if you just use while(i != 0) instead.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Can't convert string to int
    By Soorma in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 15th, 2013, 04:49 PM
  2. [SOLVED] convert int to String
    By itispj in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 14th, 2011, 10:34 PM
  3. [SOLVED] convert String to int
    By b109 in forum Java Theory & Questions
    Replies: 3
    Last Post: June 3rd, 2010, 03:05 AM
  4. Conversion of string into integer in Java
    By JavaPF in forum Java Programming Tutorials
    Replies: 17
    Last Post: January 23rd, 2010, 08:33 AM
  5. Conversion of string into integer in Java
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 17
    Last Post: January 23rd, 2010, 08:33 AM