0

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

}

Edited by DARK_BYTE: n/a

4
Contributors
9
Replies
10
Views
8 Years
Discussion Span
Last Post by Jocamps
0

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.

0

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

Edited by peter_budo: typo

0

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

Edited by DARK_BYTE: n/a

0

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

Edited by DARK_BYTE: n/a

0

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.