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

Thread: boolean value in a simple loop (do-while)

  1. #1
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default boolean value in a simple loop (do-while)

    public class NewMain {
     
        private static Scanner sc = new Scanner(System.in);
     
        public static void main(String[] args) {
     
            int loopCount = 0;
     
            int negativeCount = 0;
     
            int num1Count = 0,
                num2Count = 0,
                num3Count = 0;
     
            int num1,
                num2,
                num3;
     
            int totalCount = 0;
     
            String input1 = "",
                   input2 = "",
                   input3 = "";
     
            do {
     
                System.out.print("Num1: ");
                num1 = sc.nextInt();
     
                System.out.print("Num2: ");
                num2 = sc.nextInt();
     
                System.out.print("Num3: ");
                num3 = sc.nextInt();
     
                if (num1 < 0) {
     
                    negativeCount++;
                    num1Count++;
     
                    input1 = "Num1";
                }
     
                if (num2 < 0) {
     
                    negativeCount++;
                    num2Count++;
     
                    input2 = "Num2";
                }
     
                if (num3 < 0) {
     
                    negativeCount++;
                    num3Count++;
     
                    input3 = "Num3";
                }
     
                if ((Continue()) == true) {
     
                    loopCount++;
                }
            }
            while (Continue()); //this part will ask me to continue, BUT IT  SUPPOSED TO ASK ME ONCE, not TWICE
     
            System.out.print("\n");
            System.out.println("The Total Count Of Negative Numbers That Were Entered Is: " + negativeCount);
     
            System.out.print("\n");
            System.out.println(loopCount);
        }
     
        public static boolean Continue() {
     
            String cont;
     
            System.out.print("\n");
            System.out.print("Do You Want To Enter Again?: ");
            cont = sc.next();
     
            if ((cont.equalsIgnoreCase("Y")) || (cont.equalsIgnoreCase("YES"))) {
     
                System.out.print("\n");
                return true;
            }
            else if ((cont.equalsIgnoreCase("N")) || (cont.equalsIgnoreCase("NO"))) {
     
                return false;
            }
            else {
     
                return false;
            }
        }
    }


    why is it always asking me the boolean method two times? i cant get rid of it....


  2. #2
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Smile Re: boolean value in a simple loop (do-while)

    ive already solved the mistaken part...

                if ((Continue()) == true) {
     
                    loopCount++;
                }

    instead of making a condition to count the loop. i just simply define a count which is


    loopCount++;

    just want to know a liitle infomation why is it running like that..?

    i know its a liitle bit stupid to define the count of the loop in an IF condition if its 'false' or 'true' because the loop wont run (NOR COUNT) if its false... (it will only generate the loopCount or increment if it is true)

    im just trying to make up something.. please.....
    Last edited by chronoz13; October 16th, 2009 at 12:28 PM.

  3. #3
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: boolean value in a simple loop (do-while)

    You're calling the method continue twice. Each time, it will ask the user if you want to continue. What you should do is only call it once, and store the result of that call in a variable so you can access that same answer multiple times instead of asking the user for another answer.

  4. The Following User Says Thank You to helloworld922 For This Useful Post:

    chronoz13 (October 17th, 2009)

  5. #4
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: boolean value in a simple loop (do-while)

    ahh but i supposed its not efficient... i just have to make a loop count... because it wont count if the loop will be false...

    whahahahha no sense at all..... just count it.....(it will only count if its true) ryt? hehehehe

  6. #5
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: boolean value in a simple loop (do-while)

    If you don't want it to "over count" use a while loop:

    while(continue())
    {
         loopCount++;
         System.out.println("Enter a number: ");
         if (Scanner.nextInt() < 0)
         {
              negativeNumbersCount++;
         }
    }

  7. #6
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: boolean value in a simple loop (do-while)

    ahh so it makes no sense if i subtracted 1 from the loopcount...

    never thought of that using while loop
    so it wont over count.. whahahah...

Similar Threads

  1. can any one do this in a loop? (for loop)
    By chronoz13 in forum Loops & Control Statements
    Replies: 4
    Last Post: October 4th, 2009, 09:31 PM
  2. [SOLVED] Modification of Boolean function in Java program
    By lotus in forum Java SE APIs
    Replies: 4
    Last Post: July 10th, 2009, 11:15 AM
  3. [SOLVED] Java exception "result already defined"
    By rptech in forum Object Oriented Programming
    Replies: 3
    Last Post: May 20th, 2009, 06:48 AM
  4. Java operaton on boolean varibles
    By big_c in forum Java Theory & Questions
    Replies: 5
    Last Post: May 12th, 2009, 05:40 AM