File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes whats wrong with my code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "whats wrong with my code" Watch "whats wrong with my code" New topic

whats wrong with my code

Manal Ahmad

Joined: Feb 22, 2005
Posts: 21
import java .io.*;

public class grader{

public static void main (String args[])throws IOException
BufferedReader input= new BufferedReader
(new InputStreamReader(System .in));

String inputString;

int mid1,mid2,finalex ;
float sum;

System.out.print("insert the grade of mid1");
mid1=Integer.parseInt(inputString );
System.out.print("insert the grade of mid2");
System.out.print("insert the grade of final");
System.out.print("the average is ")

when i compile it its write to a comment for (inputString that i didnt initilize it ???
Jack Conway
Ranch Hand

Joined: Aug 10, 2003
Posts: 30
I think you should be using

Otherwise the variable inputString is remaining uninitialised.

Once you've fixed that, you'll need to catch the IOExceptions from the readLines.

Good luck!

Co-author, <a href="" target="_blank" rel="nofollow">Java Programming and Enterprise Java</a> (free download)
Nigel Browne
Ranch Hand

Joined: May 15, 2001
Posts: 673
There are a few things that are wrong with your code.
1) You try to use inputString before it is initialised. Amend you code so that inputString has a value
2) You are missing a semi-colon off one of your lines of code
3) You should be catching exceptions but your not

I hope these comments will help you on your way.
Paul Sturrock

Joined: Apr 14, 2004
Posts: 10336

Indeed, you have not initialized inputString. You "initialize" an object by creating an instance of that object. When you write:

You have done nothing more than created a reference called "inputString" which points to nothing. You have not created a String object, just the 'marker' that will be used to refer to the String once you create it.

If instead you write:

You have initialized it. What "initialized" means is that an object's constructor has been called.

Now, some caveats:

The construct I used above is considered bad practice, due to peculuarities of the String class (which I won't explain here, for fear of clouding the issue at hand). I only use it here because it is clear you are constructing a "new" object. Instead, this is the way to initialize a String:

You may also fix this compilation error by assigning inputString to a pre-existing String object instance (and this is particularaly pertinent given what you application seems to be trying to do). You do this like this:

Also, there is one occasion where you can "initialize" an object for use by your reference without actually constructing an object. This is when you assign the value to be null:

JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Manal Ahmad

Joined: Feb 22, 2005
Posts: 21
thanks alot 4 replying me
Pete Tyo
Ranch Hand

Joined: May 11, 2005
Posts: 38
It would be nice if you used the UBB code tag so your code is more readable.

miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
just my 2 cents:
you just need to initialize when its a local variable - a variable that's inside a method (or code block like a for loop); else you dont:

[ May 30, 2005: Message edited by: miguel lisboa ]

java amateur
K Riaz
Ranch Hand

Joined: Jan 08, 2005
Posts: 375
Originally posted by Pete Tyo:
It would be nice if you used the UBB code tag so your code is more readable.


Agreed. I avoid helping when I see a large chunk of code without UBB tags. Afterall, if the user cannot be bothered, which should I?
subject: whats wrong with my code