Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

having problem in verifying a character

 
Garry Meax
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
here is what i am required to do here:

Write a program that prompts a professor to input grades for different course for 10 students. Prompt the professor to enter one grade at a time using the prompt � Enter name for student # 1� and �Enter grade #1.� Verify that the professor enters only A, B, C, D, or F. use variables for the student numbers (1 through 10) and grade numbers (1 through 5).

import java.io.*;

class name_and_grade
{
static char grade;

static void get_name() throws IOException
{

BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String name;

System.out.println("Enter Name");
name = in.readLine();

}

static void get_grade()
{

try
{
System.out.println("Enter Grade");
grade = (char)System.in.read();
verify_grade();
}catch(Exception E)
{
System.out.println("Erroe");
}
}

static void verify_grade()
{
if(grade != 'A' || grade != 'B' || grade != 'C' || grade != 'D' || grade != 'F')
{
System.out.println("Grade has to be eithe A, B, C, D, or F");
}
}

public static void main(String args[]) throws IOException
{
for(int i=1; i<=10; i++)
{
get_name();
get_grade();
}

}
}


i don't get any errors when i compile this code.

Out put after entering 1st Name and 1st Grade:
Enter Name
Tony
Enter Grade
A
Grade has to be eithe A, B, C, D, or F
Enter Name
Enter Grade

my question is why it is showing this message(Grade has to be eithe A, B, C, D, or F) even the grade i entered was 'A'.

and after that message it skips where i am supposed to enter name and jumps to enter grade part.

why is so?

thanks
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> if(grade != 'A' || grade != 'B' || grade != 'C' || grade != 'D' || grade != 'F')

only one has to be true for the if block to execute
you enter A, so A != B - which is true

perhaps you want

if(!(grade == 'A' || grade == 'B' || grade == 'C' || grade == 'D' || grade == 'F'))

but it might be simpler to have

String grades = "ABCDEF";
String grade = in.readLine();
if(grades.indexOf(grade) < 0)
{
//error message
}
 
Garry Meax
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Michael Dunn:


perhaps you want

if(!(grade == 'A' || grade == 'B' || grade == 'C' || grade == 'D' || grade == 'F'))
}


this helps to verify the grades but still it skips where i have to enter name for the second time and jumps to where i have to enter grade for the second time.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if it's in a loop, and you're still using this line
grade = (char)System.in.read();

read() only reads 1 byte, which might be A, next time it will read the
carriage return, then possibly the newLine character (I think that's the order)

best to use readLine(), which gobbles it all up (produces a string)
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As a side note, Sun has published some recommended coding conventions. In particular, they suggest that class names begin in upper-case and use CamelCase to separate words in the name. On the other hand, variable and method names start with lower-case and still use camelCase to separate words. Typically underscores are only used in the names of static final member variables which are named in all UPPER_CASE.

If you follow some of these basic conventions, it will make it easier for us to read your code. If you are interested in learning more about Sun's coding conventions, go here. Although I don't think it is important to completely adopt all of these conventions, at the very least, you need to pick a set of conventions and be consistent. The naming convention that I described above is the most important to me because it is so broadly used.

Layne
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic