• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

having problem in verifying a character

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Whip out those weird instruments of science and probe away! I think it's a tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic