This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Error checking code - Need help - Please!!

 
Rose Evans
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok guys, what am I doing wrong? Again...
I am trying to put an error checking code into my program so if a user enters anything above a 10, it will give them a message to re-enter their quiz score. I have it half way working, and it does bring up the error message if they enter something over 10, but it also brings up the same message if they enter something under 10, which it should do. Any suggestions would be greatly appreciated?
Here a snippet of my code so far..
________________________________________________________________________

System.out.print("Please enter your name: ");
String name = SavitchIn.readLine();

System.out.print("\nPlease enter your first quiz grade: ");
quiz1 = SavitchIn.readLineInt();

System.out.print("\nPlease enter your second quiz grade: ");
quiz2 = SavitchIn.readLineInt();

if ((quiz1 > 10) || (quiz2 > 10));

{ //Error checking, Try again, code.

System.out.println("The quiz score you entered is too high.");
System.out.println("Quiz scores must be between 0 - 10");
System.out.println("Please re-enter your quiz score.");
System.out.print("\nPlease enter your first quiz grade: ");
quiz1 = SavitchIn.readLineInt();
System.out.print("\nPlease enter your second quiz grade: ");
quiz2 = SavitchIn.readLineInt();

//I tried putting an else statment right here, but it kept telling me
//I was missing an If statement, so who knows?

System.out.print("\nPlease enter your midterm exam: ");
midtermExam = SavitchIn.readLineInt();
System.out.print("\nPlease enter your final exam score: ");
finalExam = SavitchIn.readLineInt();
}
_____________________________________________________________

I'd appreciate any information you can give me. Thanks!!!
 
Rose Evans
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I meant to say it should NOT bring up the error message if they enter the correct numbers 1 thru 10. Heck I cant even type right half the time. Ha.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
your problem is the semi-colon at the end of this line
if ((quiz1 > 10) || (quiz2 > 10));

but you want to look at creating some type of loop for this.
your code handles one error situation, what if the user types the wrong info twice?

here's one way (not the best way, but it's simple)

 
Rose Evans
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michael,
Thanks again! I'll go see what I can do with it now. You are one smart guy!!!
 
Rose Evans
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Michael,

You're one smart dude. It took me forever to figure out exactly where I should put that code, but I finally got it where it should be. I surely hope one day I can be as good as you are!!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic