Welcome to Dream.In.Code
Become a Java Expert!

Join 150,194 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 2,021 people online right now. Registration is fast and FREE... Join Now!




Variable might not have been initialized

 
Reply to this topicStart new topic

Variable might not have been initialized

porknbeans
15 Sep, 2008 - 08:46 AM
Post #1

New D.I.C Head
*

Joined: 15 Sep, 2008
Posts: 4

hey i am very new to programming and just started a java course and i need help with making a tip calculator
i pretty much have it all done except for 2 errors i keep getting
"Variable a might not have been initialized"
"Variable b might not have been initialized"

here is my code
CODE
import javax.swing.JOptionPane;
class Tip
{
    public static void main(String[] args)
    {
        int a;
        int b;
        double sub; //Declare Radius
        double grat; //Declare Area
        double total;
        String x;
        x=JOptionPane.showInputDialog(null,
            "Enter Subtotal","VALUE FOR A",
            JOptionPane.QUESTION_MESSAGE);

        x=JOptionPane.showInputDialog(null,
            "Enter Gratuiuty Rate","VALUE FOR B",
            JOptionPane.QUESTION_MESSAGE);



        //Assign radius
        sub = a;

        //Compute Area
        grat = b / a;

        total = sub = grat;

        //Display Results
        System.out.println("The total is " + total);
        

    }
}

User is offlineProfile CardPM
+Quote Post

1lacca
RE: Variable Might Not Have Been Initialized
15 Sep, 2008 - 09:59 AM
Post #2

code.rascal
Group Icon

Joined: 11 Aug, 2005
Posts: 3,822



Thanked: 12 times
My Contributions
You don't assign a value to a and b (probably you want to parse the String x after receiving the input from the user.
User is offlineProfile CardPM
+Quote Post

porknbeans
RE: Variable Might Not Have Been Initialized
15 Sep, 2008 - 10:29 AM
Post #3

New D.I.C Head
*

Joined: 15 Sep, 2008
Posts: 4

i dont quite understand what you mean. you think you could show me. sorry im still very new to this
User is offlineProfile CardPM
+Quote Post

vik09
RE: Variable Might Not Have Been Initialized
15 Sep, 2008 - 12:21 PM
Post #4

New D.I.C Head
*

Joined: 8 Jul, 2008
Posts: 32


My Contributions
When you declare a variable give it a value.
So... give your variables some values like
CODE
int a = 0;

Do this with all your variables ints, double, String.

This post has been edited by vik09: 15 Sep, 2008 - 12:41 PM
User is offlineProfile CardPM
+Quote Post

porknbeans
RE: Variable Might Not Have Been Initialized
15 Sep, 2008 - 12:49 PM
Post #5

New D.I.C Head
*

Joined: 15 Sep, 2008
Posts: 4

if i set it to 0 will i still be able to enter a numer with JOptionPane?
User is offlineProfile CardPM
+Quote Post

pbl
RE: Variable Might Not Have Been Initialized
15 Sep, 2008 - 03:14 PM
Post #6

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,587



Thanked: 233 times
Dream Kudos: 75
My Contributions
You still don't assing a value to them
JOptionPanes return String
so you'll have to to something like that and then A and B will be initialized

CODE

        x=JOptionPane.showInputDialog(null,
            "Enter Subtotal","VALUE FOR A",
            JOptionPane.QUESTION_MESSAGE);
        a = Integer.parseInt(x);

        x=JOptionPane.showInputDialog(null,
            "Enter Gratuiuty Rate","VALUE FOR B",
            JOptionPane.QUESTION_MESSAGE);
        b = Integer.parseInt(x);



QUOTE(vik09 @ 15 Sep, 2008 - 01:21 PM) *

When you declare a variable give it a value.
So... give your variables some values like
CODE
int a = 0;

Do this with all your variables ints, double, String.


Do what you want but this is NOT a good idea...

Take the last example, if Porknbeans would have to that he would never had the error message saying that A and B wher not initialized and he may have look for hours why the values of A and B were 0.

By declaring your variables without initializing them, you will receice computer warning telling you "you hava a logic problem ! Why do you declare variable if you never use them ?"
User is offlineProfile CardPM
+Quote Post

porknbeans
RE: Variable Might Not Have Been Initialized
15 Sep, 2008 - 03:37 PM
Post #7

New D.I.C Head
*

Joined: 15 Sep, 2008
Posts: 4

thank you so much. would have never gotten it without you.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 04:22AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month