Need help with my logic

T

TechGurl

I am trying to write a program that will total the number of grades
entered and also give me a total of all the A's B' C's etc. Thanks


Error that I am getting:

Exception in thread "main" java.lang.Error: Unresolved compilation
problems:
numberOfA cannot be resolved //how I am to resolve this and the
others
numberOfB cannot be resolved
numberOfC cannot be resolved
numberOfD cannot be resolved
numberOfF cannot be resolved

at ExamScorer.main(ExamScorer.java:22)

public class ExamScorer
{
public static void main(String[] args)
{


System.out.println("Enter exam score: ");
int score;
char grade;
int sum;
int next;
String answer;
int countGrade = 0;
int countScore = 0;
Scanner keyboard = new Scanner(System.in);

{
System.out.println();
System.out.println("Enter all of the exam scores.");
sum=0;
int NumberOfA = 0;
int NumberOfB = 0;
int NumberOfC = 0;
int NumberOfD = 0;
int NumberOfF = 0;
next = keyboard.nextInt();
score = keyboard.nextInt();
grade = keyboard.next();

}
sum = sum + next;
numberOfA++;
numberOfB++;
numberOfC++;
numberOfD++;
numberOfF++;
next = keyboard.nextInt();


if (score >= 90)
grade = 'A';
else if (score >= 80)
grade = 'B';
else if (score >= 70)
grade = 'C';
else if (score >= 60)
grade = 'D';
else
grade = 'F';


System.out.println("Score = " + score);
countScore++;
System.out.println("Grade = " + grade);
countGrade++;


}

}
 
L

Lew

TechGurl said:
Error that I am getting:

Exception in thread "main" java.lang.Error: Unresolved compilation
problems:
numberOfA cannot be resolved //how I am to resolve this and the
others
numberOfB cannot be resolved
numberOfC cannot be resolved
numberOfD cannot be resolved
numberOfF cannot be resolved

at ExamScorer.main(ExamScorer.java:22)

public class ExamScorer
{
public static void main(String[] args)
{
System.out.println("Enter exam score: ");
int score;
char grade;
int sum;
int next;
String answer;
int countGrade = 0;
int countScore = 0;
Scanner keyboard = new Scanner(System.in);

{

These inner braces limit the scope of the variables declared within them.
System.out.println();
System.out.println("Enter all of the exam scores.");
sum=0;
int NumberOfA = 0;
int NumberOfB = 0;
int NumberOfC = 0;
int NumberOfD = 0;
int NumberOfF = 0;
next = keyboard.nextInt();
score = keyboard.nextInt();
grade = keyboard.next();

}

So outside them, the variables cannot be resolved.
sum = sum + next;
numberOfA++;
numberOfB++;
numberOfC++;
numberOfD++;
numberOfF++;

Remove the inner braces.

- Lew
 
M

marcus.vinicius.lima

Hello,
There is another error, the case of the variable.

Please note that numberOfA is different from NumberOfA

TechGurl said:
Error that I am getting:

Exception in thread "main" java.lang.Error: Unresolved compilation
problems:
numberOfA cannot be resolved //how I am to resolve this and the
others
numberOfB cannot be resolved
numberOfC cannot be resolved
numberOfD cannot be resolved
numberOfF cannot be resolved

at ExamScorer.main(ExamScorer.java:22)

public class ExamScorer
{
public static void main(String[] args)
{
System.out.println("Enter exam score: ");
int score;
char grade;
int sum;
int next;
String answer;
int countGrade = 0;
int countScore = 0;
Scanner keyboard = new Scanner(System.in);

{

These inner braces limit the scope of the variables declared within them.
System.out.println();
System.out.println("Enter all of the exam scores.");
sum=0;
int NumberOfA = 0;
int NumberOfB = 0;
int NumberOfC = 0;
int NumberOfD = 0;
int NumberOfF = 0;
next = keyboard.nextInt();
score = keyboard.nextInt();
grade = keyboard.next();

}

So outside them, the variables cannot be resolved.
sum = sum + next;
numberOfA++;
numberOfB++;
numberOfC++;
numberOfD++;
numberOfF++;

Remove the inner braces.

- Lew
 
T

TechGurl

Lew thanks for the response. I corrected why you and Marcus suggest
however the program still doesn't calculate the total number of grades
that I imput. If I input 12 different grades I need for the result to
be Total number of grades: 12

What am I missing?


TechGurl said:
Error that I am getting:

Exception in thread "main" java.lang.Error: Unresolved compilation
problems:
numberOfA cannot be resolved //how I am to resolve this and the
others
numberOfB cannot be resolved
numberOfC cannot be resolved
numberOfD cannot be resolved
numberOfF cannot be resolved

at ExamScorer.main(ExamScorer.java:22)

public class ExamScorer
{
public static void main(String[] args)
{
System.out.println("Enter exam score: ");
int score;
char grade;
int sum;
int next;
String answer;
int countGrade = 0;
int countScore = 0;
Scanner keyboard = new Scanner(System.in);

{

These inner braces limit the scope of the variables declared within them.
System.out.println();
System.out.println("Enter all of the exam scores.");
sum=0;
int NumberOfA = 0;
int NumberOfB = 0;
int NumberOfC = 0;
int NumberOfD = 0;
int NumberOfF = 0;
next = keyboard.nextInt();
score = keyboard.nextInt();
grade = keyboard.next();

}

So outside them, the variables cannot be resolved.
sum = sum + next;
numberOfA++;
numberOfB++;
numberOfC++;
numberOfD++;
numberOfF++;

Remove the inner braces.

- Lew
 
P

Patricia Shanahan

TechGurl said:
Lew thanks for the response. I corrected why you and Marcus suggest
however the program still doesn't calculate the total number of grades
that I imput. If I input 12 different grades I need for the result to
be Total number of grades: 12

What am I missing?

Knowledge of how to debug a program that compiles but does not do what
you want?

See http://home.earthlink.net/~patricia_shanahan/debug/ for my favorite
approach.

Patricia
 
S

Simon Brooke

Lew said:
TechGurl said:
Error that I am getting:

Exception in thread "main" java.lang.Error: Unresolved
compilation problems:
numberOfA cannot be resolved //how I am to resolve this
and the
others
numberOfB cannot be resolved
numberOfC cannot be resolved
numberOfD cannot be resolved
numberOfF cannot be resolved

at ExamScorer.main(ExamScorer.java:22)

public class ExamScorer
{
public static void main(String[] args)
{
System.out.println("Enter exam score: ");
int score;
char grade;
int sum;
int next;
String answer;
int countGrade = 0;
int countScore = 0;
Scanner keyboard = new Scanner(System.in);

{

These inner braces limit the scope of the variables declared within them.
System.out.println();
System.out.println("Enter all of the exam
scores."); sum=0;
int NumberOfA = 0;
int NumberOfB = 0;
int NumberOfC = 0;
int NumberOfD = 0;
int NumberOfF = 0;
next = keyboard.nextInt();
score = keyboard.nextInt();
grade = keyboard.next();

}

So outside them, the variables cannot be resolved.

Also, numberOfA != NumberOfA; Java is case sensitive.

--
(e-mail address removed) (Simon Brooke) http://www.jasmine.org.uk/~simon/

;; IE 3 is dead, but Netscape 4 still shambles about the earth,
;; wreaking a horrific vengeance upon the living
;; anonymous
 
T

TechGurl

Patricia,

Thanks for the link!! However that information is way above my head. I
have never programmed before and to me its like translating a language
into English and I don't know the language. I am not asking for anyone
to do it for me. Just looking for pointers such as what expression or
variable I should use to get it to count because obviously count++ is
not counting.
 
P

Patricia Shanahan

TechGurl said:
Patricia,

Thanks for the link!! However that information is way above my head. I
have never programmed before and to me its like translating a language
into English and I don't know the language. I am not asking for anyone
to do it for me. Just looking for pointers such as what expression or
variable I should use to get it to count because obviously count++ is
not counting.

How do you know whether it is counting or not?

Patricia
 
L

Lew

Patricia said:
How do you know whether it is counting or not?

Patricia

And how many times is it counting?

Doesn't there have to be some way to repeat the instructions that read in a
grade and increment your counter?

It seems that you would benefit from a grounding in programming basics as they
apply to Java. The key concept here is "control flow."

Read through the Sun Java tutorial fully. (Yes, it will take some time, but
it's a sound investment.)
http://java.sun.com/docs/books/tutorial/java/index.html
Pay particular attention to the chapter on "Language Basics". Don't worry too
much about the material the first time through; just let it float into your
mind, then reread the chapter. Be an active reader - ask yourself questions
like "which control flow would let me increment my counter 12 times?"

Another good resource is _Thinking in Java_, by Bruce Eckel, available online at
http://www.mindview.net/Books/TIJ/

This book does a good job of inculcating habits of thought that help with Java
programming.

As you become more comfortable with the basics, you will find it progressively
easier to answer questions like, "why didn't my counter increase the right
number of times?"

Without those basics even straight-out answers to your questions, like "try a
for(...) or while(...) loop" will only be of limited value.

- Lew
 
M

Mark Space

Patricia said:


I didn't read all the way through that, so sorry if I missed this, but
there's two things I thought I'd mention, since the OP says she couldn't
understand the link.

1. Hand Execution

Learn how to execute your programs by hand. This is probably the best
thing you can ever learn if you are a programmer.

Print out your program and go through it line by line. Write variable
names on a piece of paper as you encounter them, and write their value
next to them. When the variable changes value later, cross out the old
value and write the new one next to it on the right.

This will solve a lot of problems, and also make you much better about
designing programs in the future.

2. Divide and Conquer

Once your programs get very large, you really can't hand execute the
whole thing any more. Learn how to divide your program into chunks
(modules, classes, methods, subroutines, etc....) Then make sure each
chunk is working correctly before putting them all together. This will
save you a lot of time when you write (and debug) large programs.

Also see "Unit Testing."


Ok, good luck.
 
M

Martin Gregorie

Mark said:
I didn't read all the way through that, so sorry if I missed this, but
there's two things I thought I'd mention, since the OP says she couldn't
understand the link.

1. Hand Execution

Learn how to execute your programs by hand. This is probably the best
thing you can ever learn if you are a programmer.

Print out your program and go through it line by line. Write variable
names on a piece of paper as you encounter them, and write their value
next to them. When the variable changes value later, cross out the old
value and write the new one next to it on the right.

This will solve a lot of problems, and also make you much better about
designing programs in the future.

2. Divide and Conquer

Once your programs get very large, you really can't hand execute the
whole thing any more. Learn how to divide your program into chunks
(modules, classes, methods, subroutines, etc....) Then make sure each
chunk is working correctly before putting them all together. This will
save you a lot of time when you write (and debug) large programs.

Also see "Unit Testing."

3. Diagnostic tracing:

Add a boolean debug variable to your classes to control diagnostic
output. Set it either through the constructor or with a separate method:

void setDebug(boolean d)
{
debug = d;
}

In each method put at least one debugging statement at the head of a
method that accepts information to show the arguments:

if (debug)
System.stderr.println("method(" + variable + ","...")");

and at the tail of a method that returns anything put a debugging
statement to show that is returned:

if (debug)
System.stderr.println(retval + " = method()");

If the method is short, a single statement at the end can often be used
to handle both the arguments and the returned value. You might also
trace the result of particularly significant operations within the
method and in the constructor if it does anything significant apart
from initializing variables.

Now you can easily trace the execution flow through your program and see
results at significant points within methods. You'll find that this
approach is *much* faster than stepping through the code with a debugger
if the application is of any significant size and - bonus - can be
activated in a live environment to diagnose problems caused by
unexpected data. I normally use a -d option passed into the main()
method to control debugging throughout the program for exactly this
reason. The "if (debug) statement;" construct adds almost no runtime
overhead.

4. Learn about good program design and development.

Get a copy of Kernighan & Pike's "The Practice of Programming" (Addison
Wesley). Its applicable to all modern programming languages, contains
examples in Java, C and C++ and is very good on all aspects of
programming from design to testing and debugging.

HTH
 
M

Mark Space

Martin said:
3. Diagnostic tracing:

Add a boolean debug variable to your classes to control diagnostic
output. Set it either through the constructor or with a separate method:

This is a very good point, and everything Martin says here is true.

This technique is so important, that most languages support it directly.
Both C and Java support the assert statement which does the
if..then... part for you. (In C, it's a macro, not a statement.)

In Java, the assert statement can be controlled from the command line
just as Martin describes. Combine this with the divide-and-conquer
strategy, and you can turn assertion on and off in differ parts (chunks)
of your program. This allows you to only see asserts from parts of your
program, and the other parts run at full speed. (Because the compiler
optimizes assert away when it's disabled.)

The disadvantage of assert is that it's less flexible than an
if..then... so you'll sometimes still have to use them as Martin describes.

I do disagree with Martin on one point: using a debugger. My preferred
method is to use different boolean debug variables for different parts
of my program. (Divide and conquer, again.) Then, instead of setting
the debug variables on the command line, I set them with the debugger.
This allows me to change what areas I want to see output from, with out
having to stop and restart the program. This is much, much faster. And
it's much faster than stepping and tracing too, which should only be
used when you've narrowed a problem down to a very small section of code
with the boolean debug variables, and you need a lot of fine detail to
find the root cause.

4. Learn about good program design and development.

Get a copy of Kernighan & Pike's "The Practice of Programming" (Addison

I've never heard of this before, I'll have to check it out. Thanks!

M
 
T

TechGurl

Thanks Lew. I needed that!!
Lew said:
And how many times is it counting?

Doesn't there have to be some way to repeat the instructions that read in a
grade and increment your counter?

It seems that you would benefit from a grounding in programming basics as they
apply to Java. The key concept here is "control flow."

Read through the Sun Java tutorial fully. (Yes, it will take some time, but
it's a sound investment.)
http://java.sun.com/docs/books/tutorial/java/index.html
Pay particular attention to the chapter on "Language Basics". Don't worry too
much about the material the first time through; just let it float into your
mind, then reread the chapter. Be an active reader - ask yourself questions
like "which control flow would let me increment my counter 12 times?"

Another good resource is _Thinking in Java_, by Bruce Eckel, available online at
http://www.mindview.net/Books/TIJ/

This book does a good job of inculcating habits of thought that help with Java
programming.

As you become more comfortable with the basics, you will find it progressively
easier to answer questions like, "why didn't my counter increase the right
number of times?"

Without those basics even straight-out answers to your questions, like "try a
for(...) or while(...) loop" will only be of limited value.

- Lew
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,015
Latest member
AmbrosePal

Latest Threads

Top