-3

I am currently working on a date difference program for school, and I cannot figure out why it entirely skips over the "secondMonth" input. It prints out the final lines but will only accept input for the "secondYear" and breaks the program. I wrote the program in Eclipse and I am compiling it within Eclipse. Any help would be very much appreciated.

My code:

if (secondMonth.equalsIgnoreCase("janurary"))
{
    secondMonthNumber = 1;
}
else if (secondMonth.equalsIgnoreCase("february"))
{
    secondMonthNumber = 2;
}
else if (secondMonth.equalsIgnoreCase("march"))
{
    secondMonthNumber = 3;
}
else if (secondMonth.equalsIgnoreCase("april"))
{
    secondMonthNumber = 4;
}
else if (secondMonth.equalsIgnoreCase("may"))
{
    secondMonthNumber = 5;
}
else if (secondMonth.equalsIgnoreCase("june"))
{
    secondMonthNumber = 6;
}
else if (secondMonth.equalsIgnoreCase("july"))
{
    secondMonthNumber = 7;
}
else if (secondMonth.equalsIgnoreCase("august"))
{
    secondMonthNumber = 8;
}
else if (secondMonth.equalsIgnoreCase("september"))
{
    secondMonthNumber = 9;
}
else if (secondMonth.equalsIgnoreCase("october"))
{
    secondMonthNumber = 10;
}
else if (secondMonth.equalsIgnoreCase("november"))
{
    secondMonthNumber = 11;
}
else
{
    secondMonthNumber = 12;
}


System.out.print("Enter year: ");
secondYear = scan.nextInt();


System.out.print("These dates are " + (firstYear - secondYear) + "years and " + (firstMonthNumber - secondMonthNumber) + "months apart.");
shmosel
  • 42,915
  • 5
  • 56
  • 120
Daniel
  • 1
  • 1
  • Your program ends after you input your second month that's why. If you want more inputs ask for it. – Turan Nov 09 '17 at 02:41
  • Your code is doing exactly what you've written it to do. Where do you think it goes after `secondMonth =`? Your code doesn't do anything that would ask for more input. It just ends after that line executes. – Ken White Nov 09 '17 at 02:41
  • it works the way you programmed it. java executes the statements one by one in `main` from the beginning of the function till end. There are no loops or other programming stuff, so it just finishes when all is done. – Serge Nov 09 '17 at 02:42
  • and, btw, it is not python, so indentation plays no role there. – Serge Nov 09 '17 at 02:43
  • When I compile and run the program, it will not let me enter a value for the secondMonth. It terminates before I can input anything. – Daniel Nov 09 '17 at 02:44
  • 2
    After `nextInt`, there's still a newline character in the buffer, so `nextLine` returns immediately and your program exists – MadProgrammer Nov 09 '17 at 02:45
  • *I cannot figure out why it entirely skips over the "secondMonth" input.* You're not showing that part in your code. – shmosel Nov 09 '17 at 03:07

1 Answers1

-3

Are you trying to add a loop in your code? You can either make it go on forever (while loop) or you can code a for loop (it'll go on a set amount of times).

public static void main(String args[]){

   while(true){

   /*enter code in here *\

   }

}

You could make an Arraylist so you can save the information you get from the user when it scans the users input.

or...

You could add a for loop (can only work x amount of times (named times in the example)).

//declare pvsm up here
//create a stationary array
int[] dates = new int[times];
for(int x  = 0; x < times; x++){

   dates[x] = /*information you are trying to load *\
}
pyth12
  • 9
  • 6