Hi wat's wrong with my "do while" loop it never allows me to enter the grade even if I enter 'A' or 'B' or 'C'..?

import java.util.Scanner;
public class ArraysOfStudentDetails {
	public static void main( String [] args )
	{
		Scanner in = new Scanner( System.in );
		final int SIZE = 10;
		String[] names = new String[SIZE];
		char[] module1 = new char[SIZE];
		char[] module2 = new char[SIZE];
		char[] module3 = new char[SIZE];
		char[] module4 = new char[SIZE];
		char[] module5 = new char[SIZE];
		int j;
		char grade;
		String holding;
		for( int i = 0; i < SIZE; i ++)
		{
			j = i + 1;
			System.out.println( "Please enter student #" + j + 
					" details:");
			System.out.print( "Enter name for student #" + j );
			names[i] = in.nextLine();
			do
			{
				System.out.print( "Enter grade #1" );
				holding = in.nextLine();
				grade = holding.charAt(0);
			}
			while( grade != 'A' || grade != 'B' || grade != 'C'
				|| grade != 'D' || grade != 'E' );
			module1[i] = grade;
			
			do
			{
				System.out.print( "Enter grade #2" );
				holding = in.nextLine();
				grade = holding.charAt(0);
			}
			while( grade != 'A' || grade != 'B' || grade != 'C'
				|| grade != 'D' || grade != 'E' );
			module2[i] = grade;
			
			
			do
			{
				System.out.print( "Enter grade #3" );
				holding = in.nextLine();
				grade = holding.charAt(0);
			}
			while( grade != 'A' || grade != 'B' || grade != 'C'
				|| grade != 'D' || grade != 'E' );
			module3[i] = grade;
			
			
			do
			{
				System.out.print( "Enter grade #4" );
				holding = in.nextLine();
				grade = holding.charAt(0);
			}
			while( grade != 'A' || grade != 'B' || grade != 'C'
				|| grade != 'D' || grade != 'E' );
			module4[i] = grade;
			
			
			do
			{
				System.out.print( "Enter grade #5" );
				holding = in.nextLine();
				grade = holding.charAt(0);
			}
			while( grade != 'A' || grade != 'B' || grade != 'C'
				|| grade != 'D' || grade != 'E' );
			module5[i] = grade;
					
			System.out.println();
			System.out.println();
		
		}
			
			
	}
			
		

}

Dani AI

Generated

Quick summary of what went wrong and a safe fix.

The root cause was logical: using a chain of NOT-EQUALs joined by OR makes the whole condition always true. As hinted, the correct boolean logic is to require the grade be not any of the allowed values (use AND between the != checks) — otherwise at least one != is always true and the loop never ends. ’s explanation about how the loop condition controls re-entry is useful too. Contrary to ’s note, comparing primitive char with ==/!= is perfectly valid in Java (no need for compareTo) — Java’s operators work on primitives as described in the language docs (operators).

A more robust approach:

  • Guard against empty input before calling charAt(0) (that method throws an exception for empty strings — see String.charAt docs).
  • Accept lower- or upper-case by normalizing with Character.toUpperCase.
  • Factor the repeated validation into one helper so you don’t copy-paste five loops (also avoids the stray || that appeared in your module3 block).

Example helper (keeps your behaviour but is safer):

private static char readGrade(Scanner in, String prompt) {
    String allowed = "ABCDE";
    while (true) {
        System.out.print(prompt);
        String s = in.nextLine();
        if (s == null) continue;
        s = s.trim();
        if (s.isEmpty()) continue;
        char c = Character.toUpperCase(s.charAt(0));
        if (allowed.indexOf(c) >= 0) return c;
        System.out.println("Please enter one of: " + allowed);
    }
}

Additional pointers: use this helper for each module grade, validate input length to avoid exceptions, and consider an enum or single array-driven loop if you need to scale beyond five modules. For Scanner behavior see the Scanner.nextLine docs.

Recommended Answers

All 9 Replies

It should be AND (&&) statements right instead of OR?

Maybe I am misunderstanding the point of the program, but your while is set up with NOT EQUALs. So, while the grade is not equal to A,B,C..... you will enter grades in the do part. If you enter A,B,C... then you won't be fulfilling the condition in the while and you won't re-enter the do part. Not sure if that's what your looking for, but I hope it helps.

It should be AND (&&) statements right instead of OR?

Nope, problem is with the use of "!=" which is basically same as using method "equals()" which is only allowed/available on object type. Character is primitive type hence the error which pop up.
Solution is to use compareTo() method. With simple creation of our own compare method we can get "true" or "false" return that can be used in while loop

public boolean compareChars(Character read, Character grade) {    
    return (read.compareTo(grade) == 0);  
}

Actually you people are right it should have been &&; ahhh I am messing up!

Nope, problem is with use of "!=" which is basically as using of "equals()" method which is only allowed on object type. Character is primitive type hence the error which pop up.
Solution is use of compareTo() method. With simple creation of our own compare method we can get "true" or "false" return that can be used in while loop

public boolean compareChars(Character read, Character grade) {    
    return (read.compareTo(grade) == 0);  
}

Thanks but no thanks :P

PS: Do not forget to use keyword "static" when adding the above method to your program.

Nope, problem is with the use of "!=" which is basically same as using method "equals()" which is only allowed/available on object type. Character is primitive type hence the error which pop up.
Solution is to use compareTo() method. With simple creation of our own compare method we can get "true" or "false" return that can be used in while loop

public boolean compareChars(Character read, Character grade) {    
    return (read.compareTo(grade) == 0);  
}

Actually the problem was indeed the || instead of &&;
this works

import java.util.Scanner;
public class ArraysOfStudentDetails {
	public static void main( String [] args )
	{
	
		Scanner in = new Scanner( System.in );
		final int SIZE = 10;
		String[] names = new String[SIZE];
		char[] module1 = new char[SIZE];
		char[] module2 = new char[SIZE];
		char[] module3 = new char[SIZE];
		char[] module4 = new char[SIZE];
		char[] module5 = new char[SIZE];
		int j;
		char grade;
		String holding;
		for( int i = 0; i < SIZE; i ++)
		{			
			j = i + 1;
			System.out.println( "Please enter student #" + j + 
					" details:");
			System.out.print( "Enter name for student #" + j );
			names[i] = in.nextLine();
			do
			{
				System.out.print( "Enter grade #1" );
				holding = in.nextLine();
				grade = holding.charAt(0);
			}
			while( grade != 'A' && grade != 'B' && grade != 'C'
				&& grade != 'D' && grade != 'E' );
			module1[i] = grade;
			
			do
			{
				System.out.print( "Enter grade #2" );
				holding = in.nextLine();
				grade = holding.charAt(0);
			}
			while( grade != 'A' && grade != 'B' && grade != 'C'
				&& grade != 'D' && grade != 'E' );
			module2[i] = grade;
			
			
			do
			{
				System.out.print( "Enter grade #3" );
				holding = in.nextLine();
				grade = holding.charAt(0);
			}
			while( grade != 'A' && grade != 'B' && grade != 'C'
				|| grade != 'D' && grade != 'E' );
			module3[i] = grade;
			
			
			do
			{
				System.out.print( "Enter grade #4" );
				holding = in.nextLine();
				grade = holding.charAt(0);
			}
			while( grade != 'A' && grade != 'B' && grade != 'C'
				&& grade != 'D' && grade != 'E' );
			module4[i] = grade;
			
			
			do
			{
				System.out.print( "Enter grade #5" );
				holding = in.nextLine();
				grade = holding.charAt(0);
			}
			while( grade != 'A' && grade != 'B' && grade != 'C'
				&& grade != 'D' && grade != 'E' );
			module5[i] = grade;
					
			System.out.println();
			System.out.println();
		
		}
			
			
	}
			
		

}

Thanks a lot everyone

LOL, seriously I need holidays...

Actually the problem was indeed the || instead of &&;
Thanks a lot everyone

told 'ya ;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.