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 5 of 5

Thread: Modification of Boolean function in Java program

  1. #1
    Junior Member
    Join Date
    Jun 2009
    Posts
    24
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Modification of Boolean function in Java program

    	boolean mod = true;
    	for( i = 0;i<count1;i++){
    			while(mod){
     
    			stockName[i] =	portFolioStockName();
    			stockSym[i]  =	portFolioStockSym();	
    			unitPrice[i] =	portFolioUnitPrice();
    			unitBought[i] = portFolioUnitBought();
    			portFolioSummary(i);
    			System.out.println("Enter 1 to modify ,0 to ok");
    			mod = myScanner.nextBoolean();
    			}
    			}
    i dont want the user to enter True to modify or False to confirm info, but want them to enter number so it can set mod into either true or false how can i do it??


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: boolean help

    Hello lotus.

    You can do that like this:

    import java.util.Scanner;
     
    public class lotus {
     
        /**
         * JavaProgrammingForums.com
         */
        public static void main(String[] args) {
     
            Scanner myScanner = new Scanner(System.in);
     
            boolean mod = true;
            int myInt = 0;
     
            while (mod) {
     
                System.out.println("Enter 1 to modify, 0 to ok");
                myInt = myScanner.nextInt();
                mod = (myInt != 0);
                System.out.println("You entered: " + mod);
            }
        }
    }

    Example output:

    Enter 1 to modify, 0 to ok
    1
    You entered: true
    Enter 1 to modify, 0 to ok
    0
    You entered: false
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  3. #3
    Junior Member
    Join Date
    Jun 2009
    Posts
    24
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: boolean help

    mod = (myInt != 0); what does this line of code actually do?

  4. #4
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: boolean help

    It checks to see if myInt is not 0 and if that is the case it will return true otherwise false.

    mod equals (myInt not equals zero)

    // Json

  5. The Following User Says Thank You to Json For This Useful Post:

    JavaPF (July 10th, 2009)

  6. #5
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Thumbs up Re: boolean help

    Quote Originally Posted by Json View Post
    It checks to see if myInt is not 0 and if that is the case it will return true otherwise false.

    mod equals (myInt not equals zero)

    // Json
    Yes that's correct. Cheers for answering that Json.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  7. The Following User Says Thank You to JavaPF For This Useful Post:

    Json (July 10th, 2009)

Similar Threads

  1. [SOLVED] Java exception "result already defined"
    By rptech in forum Object Oriented Programming
    Replies: 3
    Last Post: May 20th, 2009, 05:48 AM
  2. Java operaton on boolean varibles
    By big_c in forum Java Theory & Questions
    Replies: 5
    Last Post: May 12th, 2009, 04:40 AM