Hi super cool experts!

Big Java noobie here and first time poster, so sorry in advance if this forum isn't for people like me. I'm trying to write a silly math program as my first user input test, but I seem to be getting error with the variables from the buffered reader.

Here is my code. :)

import java.io.*;

public class MathNerd {
    public static void main (String args []) {

    String firstNumber;
    String secondNumber;
    String thirdNumber;
    String fourthNumber;
    String fithNumber;
    String yesandNo;
    BufferedReader readfirstNumber;
    BufferedReader readsecondNumber;
    BufferedReader readthirdNumber;
    BufferedReader readfourthNumber;
    BufferedReader readfithNumber;
    BufferedReader readyesandNo;

    readfirstNumber = new BufferedReader (new InputStreamReader (System.in));
    readsecondNumber = new BufferedReader (new InputStreamReader (System.in));
    readthirdNumber = new BufferedReader (new InputStreamReader (System.in));
    readfourthNumber = new BufferedReader (new InputStreamReader (System.in));
    readfithNumber = new BufferedReader (new InputStreamReader (System.in));
    readyesandNo = new BufferedReader (new InputStreamReader (System.in));



    //Into into the program
        System.out.println ("I'm a super geek math wiz that knows how to do math stuff");
        System.out.println ("\nDo you believe me?");
    try {
        yesandNo = readyesandNo.readLine ();
        System.out.println ("oh, you think " + yesandNo + " hu?");
        System.out.println ("Lets test my skill since your such a smarty pants");
        }
    //try catch
    catch (IOException ioe) {
        System.out.println ("This is a yes or no question....wowsers");
        }
    //Program requests first number     
        System.out.println ("\nGive me any positive whole number math nooblet");
    try {
        firstNumber = readfirstNumber.readLine ();
        System.out.println ("Pfft, is that the best number you could come up with, super lame");
        System.out.println ("Processing....");
        }
    //try catch 
    catch (IOException ioe) {
        System.out.println ("I said give me a number you non geeky pool of mud slug");
        }
    //Program requests second number    
        System.out.println ("Ok, give me another number and i'll multiply it by the last number");
        System.out.println ("\nOh, and try to give me a better number this time...");
    try {
        secondNumber = readsecondNumber.readLine ();
        System.out.println ("Hummm " + secondNumber + " isn't to bad for a math noob");
        System.out.println ("Processing....");
        }
    //try catch 
    catch (IOException ioe) {
        System.out.println ("I said give me a number you non geeky pool of mud slug");
        }
    //Program requests third number 
        System.out.println ("\nGive me another number and i'll divide it by the outcome of the first two");
    try {
        thirdNumber = readthirdNumber.readLine ();
        System.out.println ("Are you kidding me? " + thirdNumber + " is the best you could come up with?");
        System.out.println ("Sigh... oh well, we can all be math gurus like me I guess");
        System.out.println ("Processing....");
        }
    //try catch 
    catch (IOException ioe) {
        System.out.println ("I said give me a number you non geeky pool of mud slug");
        }
    //Program requests fourth number    
        System.out.println ("Now we're getting into the tricky parts");
        System.out.println ("Give me a number this time to multiply agaisnt the outcome of the first three");
    try {
        fourthNumber = readfourthNumber.readLine ();
        System.out.println ("YAWWWWNNNNNN");
        System.out.println ("Processing....");
        }
    //try catch 
    catch (IOException ioe) {
        System.out.println ("I said give me a number you non geeky pool of mud slug");
        }
    //Program requests fith number  
        System.out.println ("Give me one last number and I will divide it by the outcome of the last four");
    try {
        fithNumber = readfithNumber.readLine ();
        System.out.println ("Processing....");
        }
    //try catch 
    catch (IOException ioe) {
        System.out.println ("I said give me a number you non geeky pool of mud slug");
        }

        double oneNumber = Double.parseDouble(firstNumber);
        double twoNumber = Double.parseDouble(secondNumber);
        double threeNumber = Double.parseDouble(thirdNumber);
        double fourNumber = Double.parseDouble(fourthNumber);
        double fiveNumber = Double.parseDouble(fithNumber);

        double calcOne = oneNumber * twoNumber;
        double calcTwo = calcOne / threeNumber;
        double calcThree = calcTwo * fourNumber;
        double calcFour = fiveNumber / calcThree;


    //Final answer and message
        System.out.println ("Awwww yeah, the number is " + calcFour);
        System.out.println ("Yes, the rumors are true, I am in fact...Da man!");
        System.out.println ("Nerd word!");

    }
    }

*** Here is the error I keep getting.***

 MathNerd.java:98: error: variable firstNumber might not have been initialized
                double oneNumber = Double.parseDouble(firstNumber);
                                                      ^
MathNerd.java:99: error: variable secondNumber might not have been initialized
                double twoNumber = Double.parseDouble(secondNumber);
                                                      ^
MathNerd.java:100: error: variable thirdNumber might not have been initialized
                double threeNumber = Double.parseDouble(thirdNumber);
                                                        ^
MathNerd.java:101: error: variable fourthNumber might not have been initialized
                double fourNumber = Double.parseDouble(fourthNumber);
                                                       ^
MathNerd.java:102: error: variable fithNumber might not have been initialized
                double fiveNumber = Double.parseDouble(fithNumber);
                                                       ^
5 errors

Welcome.
We are happy to help anyone, beginner or expert, as long as they are willing to listen and put in some effort.

might not have been initialized

Means what it says. The compiler has worked out the possible routes through ypur program, and seen that one or more results in the var not being initialised before it's used. In this case it's probably what happens if a try block fails so the program goes via the catch instead. If you initialise them when you declare them this problem cannot happen - eg String firstNumber = "";

ps You don't need all those BufferedReaders - in fact they may cause problems by interfering with each other. Just have one reader and use that for all your input.

Sounds good. I'm actually looking to learn and experiment with Java more than for a direct answer. :)

So If I understand correctly, I can just create one bufferedreader and associate it with each input?

Does Java know that the bufferedreader is specifically for each try? So is it proper format to do something like this?

mainReader = new BufferedReader (new InputStreamReader (System.in));


try {
        yesandNo = mainReader.readLine ();
        System.out.println ("oh, you think " + yesandNo + " hu?");
        System.out.println ("Lets test my skill since your such a smarty pants");
        }


try {
        firstNumber = mainReader.readLine ();
        System.out.println ("Pfft, is that the best number you could come up with, super lame");
        System.out.println ("Processing....");
        }



//try catch 
    catch (IOException ioe) {
        System.out.println ("I said give me a number you non geeky pool of mud slug");
        }

Am I off base with this?

Did that code compile? Try compiling code to see if the compiler will say its Ok. If you get errors, copy and paste the full text of the error messages here.

Yes, you can use a single reader just lik ethat. But every try must have its own catch.

I got around to working on this again and I seem to keep getting the same error no matter how I switch this around. Am I doing this wrong at a functionality level? When I do one non int user input it seems to work (leaving out the math), but when I start doing parse and multiple int inputs it throws back "might not have been initialized". Any more helpfull thoughts?

import java.io.*;
public class MathNerd {
    public static void main (String args []) {
    String firstNumber;
    String secondNumber;
    String thirdNumber;
    String fourthNumber;
    String fithNumber;
    String yesandNo;
    BufferedReader mainReader;
    mainReader = new BufferedReader (new InputStreamReader (System.in));

    //Into into the program
        System.out.println ("I'm a super geek math wiz that knows how to do math stuff");
        System.out.println ("\nDo you believe me?");
    try {
        yesandNo = mainReader.readLine ();
        System.out.println ("oh, you think " + yesandNo + " hu?");
        System.out.println ("Lets test my skill since your such a smarty pants");
        }
    //try catch
    catch (IOException ioe) {
        System.out.println ("This is a yes or no question....wowsers");
        }
    //Program requests first number     
        System.out.println ("\nGive me any positive whole number math nooblet");
    try {
        firstNumber = mainReader.readLine ();
        System.out.println ("Pfft, is that the best number you could come up with, super lame");
        System.out.println ("Processing....");
        }
    //try catch 
    catch (IOException ioe) {
        System.out.println ("I said give me a number you non geeky pool of mud slug");
        }
    //Program requests second number    
        System.out.println ("Ok, give me another number and i'll multiply it by the last number");
        System.out.println ("\nOh, and try to give me a better number this time...");
    try {
        secondNumber = mainReader.readLine ();
        System.out.println ("Hummm " + secondNumber + " isn't to bad for a math noob");
        System.out.println ("Processing....");
        }
    //try catch 
    catch (IOException ioe) {
        System.out.println ("I said give me a number you non geeky pool of mud slug");
        }
    //Program requests third number 
        System.out.println ("\nGive me another number and i'll divide it by the outcome of the first two");
    try {
        thirdNumber = mainReader.readLine ();
        System.out.println ("Are you kidding me? " + thirdNumber + " is the best you could come up with?");
        System.out.println ("Sigh... oh well, we can all be math gurus like me I guess");
        System.out.println ("Processing....");
        }
    //try catch 
    catch (IOException ioe) {
        System.out.println ("I said give me a number you non geeky pool of mud slug");
        }
    //Program requests fourth number    
        System.out.println ("Now we're getting into the tricky parts");
        System.out.println ("Give me a number this time to multiply agaisnt the outcome of the first three");
    try {
        fourthNumber = mainReader.readLine ();
        System.out.println ("YAWWWWNNNNNN");
        System.out.println ("Processing....");
        }
    //try catch 
    catch (IOException ioe) {
        System.out.println ("I said give me a number you non geeky pool of mud slug");
        }
    //Program requests fith number  
        System.out.println ("Give me one last number and I will divide it by the outcome of the last four");
    try {
        fithNumber = mainReader.readLine ();
        System.out.println ("Processing....");
        }
    //try catch 
    catch (IOException ioe) {
        System.out.println ("I said give me a number you non geeky pool of mud slug");
        }
        double oneNumber = Double.parseDouble(firstNumber);
        double twoNumber = Double.parseDouble(secondNumber);
        double threeNumber = Double.parseDouble(thirdNumber);
        double fourNumber = Double.parseDouble(fourthNumber);
        double fiveNumber = Double.parseDouble(fithNumber);
        double calcOne = oneNumber * twoNumber;
        double calcTwo = calcOne / threeNumber;
        double calcThree = calcTwo * fourNumber;
        double calcFour = fiveNumber / calcThree;
    //Final answer and message
        System.out.println ("Awwww yeah, the number is " + calcFour);
        System.out.println ("Yes, the rumors are true, I am in fact...Da man!");
        System.out.println ("Nerd word!");
    }
    }



C:\Java Apps\MathNerd>javac mathnerd.java
mathnerd.java:82: error: variable firstNumber might not have been initialized
        double oneNumber = Double.parseDouble(firstNumber);
                                              ^
mathnerd.java:83: error: variable secondNumber might not have been initialized
        double twoNumber = Double.parseDouble(secondNumber);
                                              ^
mathnerd.java:84: error: variable thirdNumber might not have been initialized
        double threeNumber = Double.parseDouble(thirdNumber);
                                                ^
mathnerd.java:85: error: variable fourthNumber might not have been initialized
        double fourNumber = Double.parseDouble(fourthNumber);
                                               ^
mathnerd.java:86: error: variable fithNumber might not have been initialized
        double fiveNumber = Double.parseDouble(fithNumber);
                                               ^
5 errors

you set the values of those variables in try blocks, but since an exception can be thrown, it's impossible for the compiler to say whether they'll be initialized or not. he has to check for worst-case-scenario, which means, he assumes they aren't initialized. if you want to solve this, you'll need to initialize a default value at the moment you declare them.

Woot, I think I understand the concept behind the error now. Thanks a bunch guys! My first try at user input is now working. :)

import java.io.*;
public class MathNerd {
    public static void main (String args []) {
    String firstNumber;
    String secondNumber;
    String thirdNumber;
    String fourthNumber;
    String fithNumber;
    String yesandNo;
    BufferedReader mainReader;
    mainReader = new BufferedReader (new InputStreamReader (System.in));

    //Into into the program
        System.out.println ("I'm a super geek math wiz that knows how to do math stuff");
        System.out.println ("\nDo you believe me?");
    try {
        yesandNo = mainReader.readLine ();
        System.out.println ("oh, you think " + yesandNo + " hu?");
        System.out.println ("Lets test my skill since your such a smarty pants");

    //Program requests first number     
        System.out.println ("\nGive me any positive whole number math nooblet");

        firstNumber = mainReader.readLine ();
        System.out.println ("Pfft, is that the best number you could come up with, super lame");
        System.out.println ("Processing....");
        double oneNumber = Double.parseDouble(firstNumber);

    //Program requests second number    
        System.out.println ("Ok, give me another number and i'll multiply it by the last number");
        System.out.println ("\nOh, and try to give me a better number this time...");
        secondNumber = mainReader.readLine ();
        System.out.println ("Hummm " + secondNumber + " isn't to bad for a math noob");
        System.out.println ("Processing....");
        double twoNumber = Double.parseDouble(secondNumber);

    //Program requests third number 
        System.out.println ("\nGive me another number and i'll divide it by the outcome of the first two");
        thirdNumber = mainReader.readLine ();
        System.out.println ("Are you kidding me? " + thirdNumber + " is the best you could come up with?");
        System.out.println ("Sigh... oh well, we can all be math gurus like me I guess");
        System.out.println ("Processing....");
        double threeNumber = Double.parseDouble(thirdNumber);

    //Program requests fourth number    
        System.out.println ("Now we're getting into the tricky parts");
        System.out.println ("Give me a number this time to multiply agaisnt the outcome of the first three");
        fourthNumber = mainReader.readLine ();
        System.out.println ("YAWWWWNNNNNN");
        System.out.println ("Processing....");
        double fourNumber = Double.parseDouble(fourthNumber);

    //Program requests fith number  
        System.out.println ("Give me one last number and I will divide it by the outcome of the last four");
        fithNumber = mainReader.readLine ();
        System.out.println ("Processing....");
        double fiveNumber = Double.parseDouble(fithNumber);

        double calcOne = oneNumber * twoNumber;
        double calcTwo = calcOne / threeNumber;
        double calcThree = calcTwo * fourNumber;
        double calcFour = fiveNumber / calcThree;
        //Final answer and message
        System.out.println ("Awwww yeah, the number is " + calcFour);
        System.out.println ("Yes, the rumors are true, I am in fact...Da man!");
        System.out.println ("Nerd word!");
        }
    //try catch 
    catch (IOException ioe) {
        System.out.println ("I said give me a number you non geeky pool of mud slug");
        }


    }
    }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.