1
import java.util.Scanner;
public class GentYears 
{ //creating method
  public static void main(String [] args)
  { 
    Scanner input = new Scanner(System.in);
    int startingYear = input.nextInt();

    startingYear++; 

    while(isDistinct(startingYear) == false)
    {
      startingYear++;
    }
    System.out.println(startingYear); //output specific element
  }

  public static boolean isDistinct(int year)
  {
    boolean isDistinct = true;
    int [] digits = new int[10]; // array decleration

    while(!(year == 0))
    {
      digits[year % 10]++;
      year /= 10;
    }
    for(int i = 0; i < digits.length; i++) //populate array with a loop
    {
      if(digits[i] > 1)
      {
        isDistinct = false;
        i = digits.length;
      }
    }
    return isDistinct;
  }
}

Pretty much what my code is doing is it is typing in a year and the program will output the next year with distinct digits. I am trying to error check my program so for any value above 10000 and any value below 1 will cause the program to say "invalid input" and end the program. Feel free to re-arrange my code if you see a way to make it better. Also, is there a way to easily implement a class into this program? I'm not too familiar with classes so I kinda need help with it.

  • `If possible, put it into a class to make it easier to understand for my teacher` We are not here to do your work for you, only to help you. –  Dec 19 '18 at 15:00
  • Also is there a reason why an `if x < 1 or x > 10000` wouldn't work for you? –  Dec 19 '18 at 15:01
  • it would, I'm just not sure where to implement it. – Joshua Gentile Dec 19 '18 at 15:04

0 Answers0