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();
		
		}
			
			
	}
			
		

}

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.