Reputation: 31
I'm making a Java Program that uses JOptionPane
about days. I'm not talking about date (10/14/2016 etc.), but days: Sunday, Monday
etc.
My problem is the function that adds a specific number of days to the current day (e.g. today is Tuesday then add 5 days into it).
I'm using arrays to access/refer to a specific day for display and output purposes.
I've referenced Sunday to Saturday, with array indexes of 0 to 6 respectively.
The problem is that, assuming the current day is Saturday, if the user added 3 days into it, the program crashes.
I believe this crashes because since Saturday is located at index 6 and the algorithm attempted to access the 9th index, which doesn't exist. Since Java is a safe-to-go language, it doesn't display 'null', it decided to crash instead.
What's supposed to happen in that situation is that from Saturday, the program would display Tuesday, not:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 9
I'm using Netbeans as my IDE and I'm self learning java since my college teaches me c++, visual basic, c# for my current year and curriculum. If you want the details, then,
Here's the source code for the main class:
package weekdaysprogram;
import java.util.*;
public class weekdayParametersProgramMain
{
static Scanner console = new Scanner (System.in);
public static void main(String[] args)
{
weekdayParametersProgram firstObject = new weekdayParametersProgram();
firstObject.inputMainMenu();
}
}
Here's the source code for the second class:
package weekdaysprogram;
import javax.swing.JOptionPane;
public class weekdayParametersProgram
{
int currentDay; //variable used for referencing a certain day
String[] day = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}; //Array of days
public void inputMainMenu() //Main menu function
{
int choice;
String inputStr;
String mainMenuStr = "Day Class\n"
+ "Enter choice: \n"
+ "1. Set Day \n"
+ "2. Print Day \n"
+ "3. Print Next Day \n"
+ "4. Print Previous Day \n"
+ "5. Calculate Day \n"
+ "6. Exit";
inputStr = JOptionPane.showInputDialog(mainMenuStr);
choice = Integer.parseInt(inputStr);
switch (choice)
{
case 1:
setDay();
break;
case 2:
printDay();
break;
case 3:
printNextDay();
break;
case 4:
printPreviousDay();
break;
case 5:
calculateDay();
break;
case 6:
exit();
break;
default:
JOptionPane.showMessageDialog(null, "Error Try Again", "Error", JOptionPane.ERROR_MESSAGE);
inputMainMenu();
break;
}
}
public void calculateDay() //the 5th function I'm getting an error at aka the culprit of my error
{
String message = "Current Day: " + currentDay + " - " + day[currentDay] + "\n"
+ "Please enter the amount of days to be added: ";
int tempNum1 = currentDay;
String tempNum2 = JOptionPane.showInputDialog(message);
int tempNum3 = Integer.parseInt(tempNum2);
for (int count=0; count <= tempNum3; count++)
{
if (count == 7) tempNum1 = 0;
else if (count==0) continue; //this is a patch, since there would be a confusion if the user inputted 1 for the addtional day
else tempNum1++;
}
String nextMessage = day[currentDay] + " + " + tempNum3 + " days = " + day[tempNum1];
JOptionPane.showMessageDialog(null, nextMessage, "New Day", JOptionPane.INFORMATION_MESSAGE);
inputMainMenu();
}
public void setDay() //1st function for set day
{
String inputStr;
String message = "Enter day index: \n"
+ "0 = " + day[0] + "\n"
+ "1 = " + day[1] + "\n"
+ "2 = " + day[2] + "\n"
+ "3 = " + day[3] + "\n"
+ "4 = " + day[4] + "\n"
+ "5 = " + day[5] + "\n"
+ "6 = " + day[6] + "\n";
inputStr = JOptionPane.showInputDialog(message);
switch (inputStr)
{
case "0":
currentDay = 0;
break;
case "1":
currentDay = 1;
break;
case "2":
currentDay = 2;
break;
case "3":
currentDay = 3;
break;
case "4":
currentDay = 4;
break;
case "5":
currentDay = 5;
break;
case "6":
currentDay = 6;
break;
default:
JOptionPane.showMessageDialog(null, "Please try again.", "Error", JOptionPane.ERROR_MESSAGE);
setDay();
break;
}
inputMainMenu();
}
public void printDay() //2nd function for printing out the current day
{
JOptionPane.showMessageDialog(null, day[currentDay], "Current Day", JOptionPane.INFORMATION_MESSAGE);
inputMainMenu();
}
public void printNextDay() //3rd function for printing out the next day
{
int tempNum = currentDay;
if (tempNum == 6) tempNum = 0;
else tempNum += 1;
JOptionPane.showMessageDialog(null, day[tempNum], "Next Day", JOptionPane.INFORMATION_MESSAGE);
inputMainMenu();
}
public void printPreviousDay() //4th function for printing out the previous day
{
int tempNum = currentDay;
if (tempNum == 0) tempNum = 6;
else tempNum -= 1;
JOptionPane.showMessageDialog(null, day[tempNum], "Previous Day", JOptionPane.INFORMATION_MESSAGE);
inputMainMenu();
}
public void exit()
{
System.exit(0);
}
}
Upvotes: 0
Views: 75
Reputation: 1596
The problem is in calculateDay()
method
Lets discus your code line by line.
Suppose currentDay=5
before entering calculateDay()
method
Now read the comments carefully inside the code
String message = "Current Day: " + currentDay + " - " + day[currentDay] + "\n"
+ "Please enter the amount of days to be added: ";//here currentDay =6
int tempNum1 = currentDay;//tempNum1=6
String tempNum2 = JOptionPane.showInputDialog(message);//suppose input "3"
int tempNum3 = Integer.parseInt(tempNum2);//tempNum3 = 3
for (int count=0; count <= tempNum3; count++)
{
if (count == 7) tempNum1 = 0;
else if (count==0) continue;
else tempNum1++;//for count value 1,2,3 tempNum1 will increase so it will be 7, 8 finally 9. So tempNum1=9
}
String nextMessage = day[currentDay] + " + " + tempNum3 + " days = " + day[tempNum1];//here tempNum1=9 So ArrayIndexOutOfBound occurs
To avoid this error add another checking like you did in your other methods
JOptionPane.showMessageDialog(null, nextMessage, "New Day", JOptionPane.INFORMATION_MESSAGE);
for (int count=0; count <= tempNum3; count++)
{
if (count == 7) tempNum1 = 0;
else if (count==0) continue;
else {
tempNum1++
if(tempNum1=7) tempNum=0;
}
}
Upvotes: 0
Reputation: 4113
In the calculateDay()
method the execution can fail for some inputs.
tempNum3
will be the integer 6.count
will start at 0 and loop until tempNum3
, which is 6.tempNum1
will be incremented each time.tempNum1
was initialized with currentDay
(which could have been also 6) and tempNum1
will have a value greater than 6, leading to an IndexOutOfBoundsException
when the String nextMessage
is built.Edit: Loris' answer also provides a solution (use the modulo operator), which I suggest you do everytime you change an index of the days
array. (Be careful with negative values though)
Upvotes: 0
Reputation: 7638
Instead of
for (int count=0; count <= tempNum3; count++)
{
if (count == 7) tempNum1 = 0;
else if (count==0) continue; //this is a patch, since there would be a confusion if the user inputted 1 for the addtional day
else tempNum1++;
}
you could use
tempNum1 = (currentDay + tempNum3) % 7;
The %
is the modulo operator, so the result will always be between 0 and 6.
Upvotes: 3