aspose file tools*
The moose likes Beginning Java and the fly likes Grade Calculator Alot of Errors While Compiling Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Grade Calculator Alot of Errors While Compiling " Watch "Grade Calculator Alot of Errors While Compiling " New topic
Author

Grade Calculator Alot of Errors While Compiling

Canh Ly
Greenhorn

Joined: Jun 22, 2012
Posts: 7
Hi I am writing a program that is designed to calculate your score in assignments, in class participation, labs, 2 midterms, and a final exam, I wrote the code to the best of my knowledge and upon review it looks like it should compile. When I compile, the errors I get are below. I only copied a few of the errors so you could get a better understanding. I attached the code that I wrote so far, and was wondering if anyone could help me understand my errors please with an explanation it would much appreciated.


GradesProgram4.java:38: non-static method getName() cannot be referenced from a static context
name = getName();
^
GradesProgram4.java:44: non-static method getName() cannot be referenced from a static context
ass6 = getName();
^
GradesProgram4.java:44: incompatible types
found : java.lang.String
required: int
ass6 = getName();
^
GradesProgram4.java:45: non-static method getName() cannot be referenced from a static context
ass7 = getName();
^
GradesProgram4.java:45: incompatible types
found : java.lang.String
required: int
ass7 = getName();

^
GradesProgram4.java:164: cannot find symbol
symbol : variable Keyboard
location: class GradesProgram4
int Inclass = Integer.parseInt(Keyboard.readLine());
^

39 errors


Gaurangkumar Khalasi
Ranch Hand

Joined: Jun 02, 2012
Posts: 187
Canh Ly wrote:
GradesProgram4.java:38: non-static method getName() cannot be referenced from a static context
name = getName();
^

GradesProgram4.java:44: non-static method getName() cannot be referenced from a static context
ass6 = getName();
^
GradesProgram4.java:45: non-static method getName() cannot be referenced from a static context
ass7 = getName();
^


From the static method (i.e. main()), you can call only static members(Both methods and/or variables) but not non-static members(in your case getName() is a non-static).

Reason: Because non-static members are the instance variables. They are attached with Object (and not with Class).
If you want to use them than you have to create an object of the class and than you can do it.

Canh Ly wrote:
GradesProgram4.java:44: incompatible types
found : java.lang.String
required: int
ass6 = getName();
^

GradesProgram4.java:45: incompatible types
found : java.lang.String
required: int
ass7 = getName();

^

The above errors are easily understood. You can also do it yourself.
You are trying to assign String value(will get from getName() method) to integer variables(ass6 & ass7).


Canh Ly wrote:
GradesProgram4.java:164: cannot find symbol
symbol : variable Keyboard
location: class GradesProgram4
int Inclass = Integer.parseInt(Keyboard.readLine());
^

Here it is a spelling mistake. You have written 'Keyboard' rather than 'keyboard'(See line no 04).
Canh Ly
Greenhorn

Joined: Jun 22, 2012
Posts: 7
Im still having trouble understanding what to do, so when I am trying to assign a string a value it isn't what I'm supposed to do? I thought that if you do that it will retrieve what the user put in and save it so it can be called to the print? I dont know how to switch the static and non, is that where I have to put on a line Static and void?
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8244
    
  23

Canh Ly wrote:I attached the code that I wrote so far...

Canh,

I've broken up those enormous lines in your code. They screw up the windowing for your thread.
Please DON'T use lines more than about 100 characters long inside code blocks (I suggest you read the UseCodeTags page again. Thoroughly.)

Thanks

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4467
    
    8

Hi Canh. Welcome to the Ranch!

You really shouldn't ever reach anywhere near this number of compiler errors. Compile early and compile often: write a few lines at a time, and compile. Fix problems as they arrive. If you write this much code before trying to compile it you're going to enter a world of pain - there are clearly some fundamental areas of Java you don't understand, and with this many errors you're just going to get lost.

So I'd actually recommend starting again. Write a new class that just contains an empty main method. Compile it. If it's OK, add two or three lines. Compile them. Continue like this.

To focus on one of your problems: the incompatible types. Java is strongly typed. That means that you need to decide what data type each variable will hold, and each method will return. And the compiler will then make sure you never assign the wrong type.

For example, you declare ass6 to hold int values like like this:
You then declare getName to return a String like this:
And then you try to assign the result of a getName() call to ass6 (and the compiler helpfully tells you where the problem is):
That means you're trying to assign a String value to an int variable. Either the variable really ought to be a String, or you need to convert from a String to an int. There's a method in the Integer class that can do this (although it seems unlikely that a "name" is really a number, so that's probably not what you need here).

With respect to the static/non-static confusion, I think you need to go away and review how objects work in Java. This is particularly true because you're currently trying to write your code in a procedural way. Java is an object-oriented language, and a well-designed Java program has to take notice of this. The Java tutorials at http://docs.oracle.com/javase/tutorial/java/javaOO/index.html would be a good place to start.
Greg Ferguson
Ranch Hand

Joined: Jun 04, 2012
Posts: 34
Hi Canh,

That is an insane amount of code you got there. For what you're trying to accomplish, I would do some research on using arrays. That way you could cut this:

down to something like this:

Then you could do something similar to this:

And you can learn to cut down all of your redundant code to just a couple of methods like this:

Learn to create classes and store relevant methods in them and then call what you need from the class that contains your main() method; it will make things a lot cleaner. And just one more nitpick, use lowercase or camelcase (ex. int getGrades) lettering for variable and methods. Otherwise you end up with mistakes like creating the variable keyboard and trying to use Keyboard -- which doesn't exist.
And to quote what Matthew said:
Compile early and compile often: write a few lines at a time, and compile. Fix problems as they arrive. If you write this much code before trying to compile it you're going to enter a world of pain

is some really good advice. Best wishes.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11480
    
  16

Go back to the version you have that give you no compiler errors. Only add 2-3 lines at a time before you re-compile.

What's that? You don't have any previous versions? Then start from scratch.

Seriously.

writing 300+ lines of code and only THEN trying to get it to compile is going to be nothing but pain and agony. You would seriously do better to start over.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Steven Bruton
Greenhorn

Joined: Mar 07, 2011
Posts: 13
Its obvious he did not write this code, its clearly some sort of debugging exercise for homework. If he had enough knowledge to write this code he could not possibly have a problem debugging these simple errors (eclipse suggests fixes for most of them).

Look at the error on line 6...
Line 6: " [code=javascript]public static void main(String[] args) throws IOException{"
Canh Ly
Greenhorn

Joined: Jun 22, 2012
Posts: 7
Steven Bruton wrote:Its obvious he did not write this code, its clearly some sort of debugging exercise for homework. If he had enough knowledge to write this code he could not possibly have a problem debugging these simple errors (eclipse suggests fixes for most of them).

Look at the error on line 6...
Line 6: " [code=javascript]public static void main(String[] args) throws IOException{"


Hi, I did in fact write the code, I've been watching videos and getting examples from other people in order to write the code that I had. After reading the replies, I defiantly know how to fix the redundancy of my code with for loops which I have failed to put in. Thanks for all the help though, I'm going to have to rewrite it with a skeleton now and hope for the best.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Grade Calculator Alot of Errors While Compiling