Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Variable not initialized error in IF statements. probable simple fix.

  1. #1
    Junior Member
    Join Date
    Jul 2013
    Posts
    13
    Thanks
    2
    Thanked 2 Times in 2 Posts

    Default Variable not initialized error in IF statements. probable simple fix.

    I am receiving an error variable might not have been initialized near the end of my program.

    The program itself is a bill calculation example in which the bill is calculated on which type of account, with character 'r or R' representing a regular account and 'p or P' representing a Premium account.

    If it is a regular account, amount due is calculated by $10 base charge plus two cents per each additional minute beyond the first free 50.

    If it is a premium account, billing is separated by day and night minute usage.
    Day minutes are charged at ten cents a minutes (beyond 75 free minutes) (day time is considered between 6 am and 6 pm)
    Night minutes are charged at 50 cents a minute (beyond 100 free minutes) (night time is considered 6 pm to 6 am)

    Premium accounts have a base charge of $25, with the total amount due being $25 + fees for minutes used in the day and night.


    the problem i receive in variables might not have been initialized occur after my if statements regarding calculating cost for minutes used in the day and night near the end of the program.

    The two variables producing errors are: line 82 : dayCost and nightCost

    i believe that i initiate these variables at if statements prior
    if (dayMinutes > 75)
                       dayCost = (dayMinutes - 75)*.1;
    if (nightMinutes> 100)
                       nightCost = (nightMinutes -100)*.05;

    both of the errors are located at
    amountdue = dayCost + nightCost

    Can anyone spot why i am receiving this code might not have been intialized error that is preventing me from running the program?



    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package chap4.pkg15.phoneswitchexamp;
     
    /**
     *
     * @author WES
     */
    import java.io.*;
    import javax.swing.JOptionPane;
     
    public class Chap415PhoneSwitchexamp {
     
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
           int accountNum;
           char serviceCode;
           int rMinutesUsed;
           int dayMinutes;
           int nightMinutes;
           int totalMinutes;
           double dayCost;
           double nightCost;
           double amountDue;
     
     
           String inputAccountNum;
           String inputCode;
           String inputRMinutes;
           String inputDayMinutes;
           String inputNightMinutes;
           String outputStr;
     
           inputAccountNum = JOptionPane.showInputDialog("Enter account number");
           accountNum = Integer.parseInt(inputAccountNum);
     
           inputCode = JOptionPane.showInputDialog("Entr service code");
           serviceCode = (inputCode.charAt(0));
     
           switch (serviceCode)
           {
               case 'r':
               case 'R':
     
                   inputRMinutes = JOptionPane.showInputDialog("Enter amount of "
                           + " minutes used in billing period");
                   rMinutesUsed = Integer.parseInt(inputRMinutes);
                   amountDue = 10;
     
                   if (rMinutesUsed > 50)
                       amountDue = 10 + (rMinutesUsed - 50)*.2;
     
                   outputStr = "Account Num: " +accountNum +"\n Account Type"
                           + ": Regular\nMinutes Used: " +rMinutesUsed 
                           + "\nAmount Due: " +String.format("%.2f",amountDue);
     
                   JOptionPane.showMessageDialog(null,outputStr,"Account"
                           + " Information",JOptionPane.INFORMATION_MESSAGE); 
                   break;
     
               case 'p':
               case 'P':
     
                   inputDayMinutes = JOptionPane.showInputDialog("Enter amount"
                           + "of minutes used between 6:00 a.m. to 6:00 p.m.");
                   dayMinutes = Integer.parseInt(inputDayMinutes);
                   inputNightMinutes = JOptionPane.showInputDialog("Enter amount"
                           + "of minutes used between 6:00 p.m. to 6:00 a.m.");
                   nightMinutes = Integer.parseInt(inputNightMinutes);
                   totalMinutes = dayMinutes + nightMinutes;
     
     
                   if (dayMinutes > 75)
                       dayCost = (dayMinutes - 75)*.1;
                   if (nightMinutes> 100)
                       nightCost = (nightMinutes -100)*.05;
     
                   amountDue =  25 + (nightCost +dayCost);
     
                   outputStr = "Account Num: " +accountNum +"\n Account Type"
                           + ": Premium\nMinutes Used: " +totalMinutes
                           + "\nAmount Due: " +String.format("%.2f",amountDue);
     
                   JOptionPane.showMessageDialog(null,outputStr,"Account"
                           + " Information",JOptionPane.INFORMATION_MESSAGE);
                   break;
     
               default:
     
                   JOptionPane.showMessageDialog(null,"Invalid Account type",
                           "Account Informatino", JOptionPane.INFORMATION_MESSAGE);    
          }
        }
    }


    a little searching goes a long way.
    initializing the variables as 0 to initialize them in instances where they werent initialized in if statements fixed the problem.
    please delete this post if desired.


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Variable not initialized error in IF statements. probable simple fix.

    String words;
    String name;
    int age;
    double valueInEuros;
    The above variables have been declared. The following variables are declared and initialized:
    String sentence = "";
    int units = 14;
    double area = a * b * c;

    You must make sure every possible path in the code will have initialized any variables used.

    For example:
    int x;
    if(3 < b) {
       x = 4;
    }
    //At this point in the code it is possible that x was not initialized
    //if b is less than 3, x will not have been initialized, and the compiler will complain about this possibility.
    The simple solution is to initialize variables when they are declared, as in the latter samples above.

Similar Threads

  1. variable might not have been initialized!!
    By bassie in forum What's Wrong With My Code?
    Replies: 4
    Last Post: December 4th, 2012, 12:39 PM
  2. HELP! I am creating a simple MP3 player and have an error dont know how to fix it
    By Siobhan Burke in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 21st, 2012, 10:07 AM
  3. Variable might not have been initialized
    By JavaGirl9001 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: December 7th, 2011, 11:24 AM
  4. Replies: 4
    Last Post: October 20th, 2011, 11:26 AM
  5. [SOLVED] Simple error that I can't figure out how to fix
    By javapenguin in forum What's Wrong With My Code?
    Replies: 8
    Last Post: July 11th, 2011, 07:27 AM