• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

whats wrong with my code

 
Manal Ahmad
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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");
input.readLine();
mid1=Integer.parseInt(inputString );
System.out.println("mid1:"+mid1);
System.out.print("insert the grade of mid2");
input.readLine();
mid2=Integer.parseInt("inputString");
System.out.println("mid2:"+mid2);
System.out.print("insert the grade of final");
input.readLine();
finalex=Integer.parseInt("inputString");
System.out.println("finalex:"+finalex);
sum=mid1+mid2+finalex;
System.out.print("the average is ")
System.out.print("AVE:"+sum/3);
}
}

when i compile it its write to a comment for (inputString that i didnt initilize it ???
 
Jack Conway
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Nigel Browne
Ranch Hand
Posts: 703
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
Manal Ahmad
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks alot 4 replying me
 
Pete Tyo
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It would be nice if you used the UBB code tag so your code is more readable.

Pete
 
miguel lisboa
Ranch Hand
Posts: 1281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
K Riaz
Ranch Hand
Posts: 375
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Pete Tyo:
It would be nice if you used the UBB code tag so your code is more readable.

Pete


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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic