开发者

Why doesn't 12th month execute properly?

开发者 https://www.devze.com 2023-02-10 15:39 出处:网络
String months[] = { null , \"Jan\" , \"Feb\" , \"Mar\" , \"Apr\", \"May\", \"Jun\", \"Jul\", \"Aug\", \"Sep\", \"Oct\",
String months[] = { null , "Jan" , "Feb" , "Mar" , "Apr", 
                    "May", "Jun", "Jul", "Aug", "Sep", "Oct", 
                    "Nov", "Dec" };

try
{
  System.out.print("Enter month <1 for Jan - 12 for Dec>:  ");

  m = input.nextInt();
  MonthData();
}
catch(Exception e)
{
  System.out.println("Invalid month!"); //error message to  inform user of wrong input
}   

break;

static void MonthData()
{            

  System.out.println("----------------------------------------");
  System.out.println(months[m]+" Expenditure <max 10 items>");

  try
  {
    for(int i=0; i<10; i++)
    {
      System.out.print("Enter item "+(i+1)+" <Press ENTER to exit> : ");
      monthItems[m][i] = input.next();

      if (monthItems[m][i].length() == 0)
      {
        return;
      } 
      else 
      {
        System.out.print("Enter amount : $");
        amount[m][i] = input.nextDouble();
        System.out.println("");
      }   

    }

  }
  catch(Exception e)
  {
    System.out.println("");
  }   
}

Hi Everyone

Just would like to say this code had a bit of help.But my question is why doesn't the function for December execute properly.

For example, when I clicked on the 12th month it just skips the function and ends the program.

Thank开发者_如何转开发s in advance.


I don't know why, but the following:

catch(Exception e){
        System.out.println("");
    } 

won't print your exception 'e' and thus you're not going to get a clear idea of what's going on.

At the very least:

e.printStackTrace();

will most likely give you a lot more info as to what's happening and is good practise generally (you can use logging frameworks etc but the principle remains the same).


maybe its because you have declared an array of size 12 and access the last element of it with index 12 but it should be 11 since you start counting at 0 ... December = Month 11

0

精彩评论

暂无评论...
验证码 换一张
取 消