# Next crash to Java

**Sorry for posting in the wrong forum .. Please refer to: https://codereview.stackexchange.com/questions/63055/next-departure-in-java**

Hi I am trying to calculate the next departure time based on the current system time in Java.

I'm still unfamiliar with many aspects of Java, but I've managed to get the program to work for most use cases with a switch and while statement, but it looks like I'm using too many workarounds to get the code to work as intended.

The next step is what I am trying to complete, a less tedious task and more elegant. I was wondering if someone could point me in the right direction on how a task can be accomplished in a similar way, but with different java functionality? Such as arraylists, tables, etc., which are fine for what I am trying to accomplish.

Here is the code:

```
public class TrainDepartures{
public static void main(String[] args) {
long time = System.currentTimeMillis();
int initialTime = (int) (time / (1000 * 60) % 60);
int findNextDeparture = (int) (time / (1000 * 60) % 60);
boolean foundDeparture = false;
loop: while (foundDeparture != true) {
findNextDeparture++;
switch (findNextDeparture) {
case 10:
System.out.print("Departure in " + (Math.abs(initialTime - 10)) + " min.");
break loop;
case 20:
System.out.print("Departure in " + (Math.abs(initialTime - 20)) + " min.");
break loop;
case 30:
System.out.print("Departure in " + (Math.abs(initialTime - 30)) + " min.");
break loop;
case 40:
System.out.print("Departure in " + (Math.abs(initialTime - 40)) + " min.");
break loop;
case 50:
System.out.print("Departure in " + (Math.abs(initialTime - 50)) + " min.");
break loop;
case 52:
System.out.print("Departure in " + (Math.abs(initialTime - 52)) + " min.");
break loop;
case 55:
System.out.print("Departure in " + (Math.abs(initialTime - 55)) + " min.");
break loop;
case 56:
System.out.print("Departure in " + (Math.abs(initialTime - 56)) + " min.");
break loop;
case 57:
System.out.print("Departure in " + (Math.abs(initialTime - 57)) + " min.");
break loop;
}
}
}
}
```

source to share

You can replace the whole operator `switch`

with

```
System.out.print("Departure in " + (Math.abs(initialTime - findNextDeparture)) + " min.");
```

You handle each case exactly the same, so you don't need complexity or verbosity `switch`

.

(This assumes you also want to print this string if `findNextDeparture==53`

, for example, any other value you haven't specified yet.)

source to share

Here's what you can try. Change the switch statement in String format:

```
final int departureTime = Math.abs(initialTime - findNextDeparture);
String result = String.format("Departure in %d min", departureTime);
System.out.print(result);
```

The output of the Switch instruction and the above code are the same.

source to share

```
public class TrainDepartures
{
public static void main(String[] args)
{
long time = System.currentTimeMillis();
int initialTime = (int) (time / (1000 * 60) % 60);
//these are the minutes you care about.
int[] departureTimes = new int[]{10,20,30,40,50,52,55,56,57};
int lastValidId = 0;
int timeDiff = 0;
for (int d = 0; d < departureTimes.length; d++)
{
if ( initialTime < departureTimes[d])
{
//keep track of the smallest value greater than the current time.
lastValidId = d;
timeDiff = (departureTimes[lastValidId] - initialTime);
if (d == 0)//first time, were done.
break;
}
else
{
timeDiff = (departureTimes[lastValidId] - initialTime);
if ( timeDiff < 0 )
{
//wrap around when you cross into the next hour...
timeDiff = (departureTimes[0]+60) - initialTime;
}
break;
}
}
System.out.print("Departure in " + timeDiff + " min.");
}
}
```

source to share