can anyone help me

import javax.swing.JOptionPane;

public class GPA

{

public static void main(String[] args)

{

String subString = JOptionPane.showInputDialog(null," Enter Grade for Math","Input",JOptionPane.QUESTION_MESSAGE);

System.out.println(subString);

printGpa(subString);

}

public static void printGpa(String subString)

{

double result;

if(subString.equals("a"))

result = 4.0;

System.out.println(" Gpa for security is "+ result);

}

}

i am new to java can any one help me in doing this, i am getting an error that result is not initialized but i had initialized it in the if statement. i think that in the printGpa method if loop is not working can any one help me with this program.

what i actually want to do is to give input a and if input is a then the result should be 4.0 that's it.

I will be very much thankful to you for your help

[946 byte] By [surkuma] at [2007-11-27 8:20:32]
# 1
What should it display if subString is not "a"?
Hippolytea at 2007-7-12 20:08:51 > top of Java-index,Java Essentials,New To Java...
# 2

What if subString is not "a"? Result would not get initialized in that case. Try this:

if(subString.equals("a"))

{

result = 4.0;

}

//add in else ifs for grades b - d

else

{

result = 0.0;

}

This way, no matter what subString is, result will get initialized with some value.

CaptainMorgan08a at 2007-7-12 20:08:51 > top of Java-index,Java Essentials,New To Java...
# 3

double result;

if(subString.equals("a"))

result = 4.0;

System.out.println(" Gpa for security is "+ result);

If subString doesn't equal "a", then what will the value of result be when you hit the println? It will be undetermined. You have to set a variable to something before you access it. Either give it a default value, or put in an else statement that will assign it.

ps, it's not called an if loop. For and while are loops, not if.

hunter9000a at 2007-7-12 20:08:51 > top of Java-index,Java Essentials,New To Java...
# 4

actually this is not complete program if subString is not 'a' then just print any error statement using System.out.println

my actual problem is that eventhough i am initializing the result in the if statement. i am getting error that result is not initialized.

you think that we just give input 'a' so the result should print 4.0 but it is not , i am getting errors.

surkuma at 2007-7-12 20:08:52 > top of Java-index,Java Essentials,New To Java...
# 5
If your code is incomplete is it any wonder there are errors?
Hippolytea at 2007-7-12 20:08:52 > top of Java-index,Java Essentials,New To Java...
# 6
yup its working thanks for your help
surkuma at 2007-7-12 20:08:52 > top of Java-index,Java Essentials,New To Java...