خرید بک لینک

Vote count: 0

Good day, this is my first question here in stackoverflow and I hope I could get along with fellow coders here.

I'm making a Java Program that uses JOptionPane about days. I'm not talking about date (10/14/2016 etc.) I'm talking about Sunday, Monday etc.

Here's what I plan to do, when you run the program, a panel appears that asks the user to input a number and the program runs a task depending on the function wanted by the user (1 for setting a day, 2 for printing the day, 3 for printing the next day, 4 for printing the previous day, 5 for adding a specific number of days from the current day, 6 for exiting the program)

So far, the functions for 1, 2, 3, 4 and 6 are ruing just fine without any errors, but my problem is the 5th function, aka adding 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, assume 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

Also, I'm trying to use 2 different classes in a single package while using different methods. Please bear with my inferior algorithms.

I'm using Netbeans as my IDE and I'm self leaing java since my college teaches me c++, visual basic, c# for my current year and curriculum. If you want the details, then,

I'm using Netbeans IDE 8.0.2, ruing on a windows 7 ultimate desktop with these specs:

Intel(R) Pentium(R) CPU G620 @ 2.60 GHz 2.60 GHz (Dual Core) 2.00 GB Ram (1.72 GB usable)

Here's the source code for the main class:

package weekdaysprogram;

import java.util.*;

public class weekdayParametersProgramMain 
{

    static Scaer console = new Scaer (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 Classn"
                + "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 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 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 exit()
    {
        System.exit(0);
    }
}

If I'm making you cringe, I'M SORRY! Please feel free to arrange the whole source code if you want, I've yet to lea java under the eyes of an actual professional at java! I've only been leaing java so far with reading online tutorials!

asked 16 secs ago

برچسب: نویسنده: استخدام کار تاريخ: شنبه 2 مرداد 1395 ساعت: 23:45

صفحه بندی