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.