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

Thread: not looping properly, please help.

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default not looping properly, please help.

    program is functional, but for only one user entry. I can't figure out why its not looping properly. Does anyone see anything that I am not seeing? Any and all help is greatly appreciated.

    import  java.util.Scanner;
    public class GradeGrouper
    {
    	public static void main (String [] args)
    	{
    		int score;
    		char grade;
    		int countA;
    		int countB;
    		int countC;
    		int countD;
    		int countF;
    		System.out.println ("The program will tally the number of Letter Grades, as well as");
    		System.out.println (" the total quantity of grades entered.");
    		System.out.println ("Enter a series of non-negative integers to be compiled.");
    		System.out.println ("Enter a negative number when complete.");
    		Scanner keyboard = new Scanner (System.in);
    		score = keyboard.nextInt ();
    		do
    		{			
    			countA = 0;
    			countB = 0;
    			countC = 0;
    			countD = 0;
    			countF = 0;
    				if (score >= 90)
    				{grade = 'A';
    					countA++;}
    				else if (score >= 80)
    				{grade = 'B';
    					countB++;}
    				else if (score >= 70)
    				{grade = 'C';
    					countC++;}
    				else if (score >= 60)
    				{grade = 'D';
    					countD++;}
    				else
    				{grade = 'F';
    					countF++;}
    		}
    		while (score <= 0);	
    			System.out.println ("The total quantity of integers entered is ");
    			System.out.println (+ (countA + countB + countC + countD + countF));
    			System.out.println ("The number of A's entered is " + countA);
    			System.out.println ("The number of B's entered is " + countB);
    			System.out.println ("The number of C's entered is " + countC);
    			System.out.println ("The number of D's entered is " + countD);
    			System.out.println ("The number of F's entered is " + countF);
     
     
    	}
    }


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

    Default Re: not looping properly, please help.

    What do you expect it to do? What does it do 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!

  3. #3
    Member
    Join Date
    Mar 2011
    Posts
    66
    My Mood
    Relaxed
    Thanks
    12
    Thanked 4 Times in 4 Posts

    Default Re: not looping properly, please help.

    Look at your do-while loop condition:
    do
    		{
     
                    ...............
     
    		}
    		while (score <= 0);

    I believe the loop only loops while the condition is true and if it's false, then the loop ends. Think about what the condition is stating. Also, I noticed that the score input is outside of the loop, so I think it might only do one loop regardless.

  4. #4
    Junior Member
    Join Date
    Oct 2011
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: not looping properly, please help.

    WOW, did I really miss that?!? I'll test that in a little bit, the code is on a different confuser.

  5. #5
    Junior Member
    Join Date
    Oct 2011
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: not looping properly, please help.

    Quote Originally Posted by KevinWorkman View Post
    What do you expect it to do? What does it do instead?
    It is supposed to loop through the grade assignment / grade tally 'if - else/if' loop provided the user enters a non-negative integer. It was (is until I change it) only allowing for one user entry because of what KevinWorkman pointed out, my condition says (while score is less than 0). that is definitely causing a problem. Might not be the only one, but its a big one. I'll post up a functional code if that fixes it.

  6. #6
    Junior Member
    Join Date
    Oct 2011
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: not looping properly, please help.

    okay, fixing the condition allowed it to stay in the loop for however many entries the user wants, and it exits on the negative integer, but something is wrong with the loop. More specifically, the operators in the loop. For some reason, no matter how many entries the program sees, it only registers one, and assigns the correct letter grade and adjusts the correct grade tally, but it ignores all the other entries. any insight? code below...

    import  java.util.Scanner;
    public class GradeGrouper
    {
    	public static void main (String [] args)
    	{
    		int score;
    		char grade;
    		int countA;
    		int countB;
    		int countC;
    		int countD;
    		int countF;
    		System.out.println ("The program will tally the number of Letter Grades, as well as");
    		System.out.println ("the total quantity of grades entered.");
    		System.out.println ("Enter a series of non-negative integers to be compiled.");
    		System.out.println ("Enter a negative number when complete.");
    		Scanner keyboard = new Scanner (System.in);
    		do
    		{	
    			score = keyboard.nextInt ();		
    			countA = 0;
    			countB = 0;
    			countC = 0;
    			countD = 0;
    			countF = 0;
    			while (score >= 0);
    			{
    				if (score >= 90)
    				{grade = 'A';
    					countA++;}
    				else if (score >= 80)
    				{grade = 'B';
    					countB++;}
    				else if (score >= 70)
    				{grade = 'C';
    					countC++;}
    				else if (score >= 60)
    				{grade = 'D';
    					countD++;}
    				else
    				{grade = 'F';
    					countF++;}
    			}
    		}
    			System.out.println ("The total quantity of integers entered is ");
    			System.out.println (+ (countA + countB + countC + countD + countF));
    			System.out.println ("The number of A's entered is " + countA);
    			System.out.println ("The number of B's entered is " + countB);
    			System.out.println ("The number of C's entered is " + countC);
    			System.out.println ("The number of D's entered is " + countD);
    			System.out.println ("The number of F's entered is " + countF);
     
    	}
    }

  7. #7
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: not looping properly, please help.

    You must study doWhile loop. Is your program compiling?

  8. #8
    Junior Member
    Join Date
    Oct 2011
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: not looping properly, please help.

    yes its compiling. It'll run continuously in the loop, until the negative integer is entered, but its only registering an processing the first entry. That is what is confusing me, it seems that the loop is running correctly, but something inside is wrong?

    unfortunately, "studying" the do while loop isn't really an option. I don't have a textbook. I am pretty sure that one would help, but... Let me be clear, I am NOT looking for the answer, but knowledge about a doWhile loop that will help me figure it out.

  9. #9
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: not looping properly, please help.

    You should run this though a debugger, or at least add a bunch of print statements, to figure out what's going on.

    Recommended reading: http://www.javaprogrammingforums.com...t-println.html

    Why do you have a do without a while? Why do you have a while inside that?

    Even if you don't have a textbook, you can still study the topic: This is the first result for googling "java while loops": The while and do-while Statements (The Java™ Tutorials > Learning the Java Language > Language Basics)

    Read through those tutorials, add some print statements, and ask yourself this question: when do you expect the while loop to exit?
    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!

  10. #10
    Junior Member
    Join Date
    Oct 2011
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: not looping properly, please help.

    Alright, I'll work on that, give me a bit. Thank you.

Similar Threads

  1. JlList not getting updated properly
    By frozenmonkey in forum What's Wrong With My Code?
    Replies: 10
    Last Post: July 12th, 2011, 08:39 AM
  2. my FileReader(i think) isn't performing properly..
    By weej25aya in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 21st, 2011, 10:06 PM
  3. Complete but not working properly
    By Noob101 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 12th, 2011, 03:17 PM
  4. Code doesn't execute properly
    By fride360 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 12th, 2011, 12:36 PM
  5. if else statement not working properly
    By tina G in forum Algorithms & Recursion
    Replies: 1
    Last Post: March 29th, 2010, 08:04 AM