Creating a program that displays the next year that does not have distinctive numbers in Java?

I wanted to know if anyone could help me deal with this problem. The purpose of the program is that the user enters the year, say, 1980, and the program returns next year with distinctive numbers, the distinctive numbers are all different, for example, 2013-2019, 2012 is not distinctive, since number two occurs twice, 2013 -2019 years are different, because the numbers are found only once.

User enters a year. The program calculates the next year, which has distinctive numbers

This is the code that I have right now. Its sole purpose right now is to return true, if the number the user enters is different, I could find a way to do this using the String and charAt type. I don’t think you can increase the strings, but if the user can enter int, and the program continues to increase until it finds a number that is different, but all I can think of is a function that takes a string. Is there a similar method that I could do using int as a parameter, or is there a way I could make it work with strings?

 public static boolean hasDuplicates(String text){
        for(int i = 0; i < text.length() - 1; i++){
            for(int j = i + 1; j < text.length(); j ++){
                if(text.charAt(i) == text.charAt(j)){
                    return true;
                }
            }
        }

        return false;
    }

Any advice is appreciated. Also my apologies for the lack of tags. Edit: Thank you. Full code at http://pastebin.com/FQj1yeCk

+3
source share
4

Integer.toString(i), int.

, , :

//int 'year' entered by user
while(hasDuplicates(Integer.toString(year))) {
    year += 1;
}
+1

int, , , :

int num = 1950;

int thousands = (num / 1000) % 10;
int hundreds = (num / 100) % 10;
int tens = (num / 10) % 10;
int ones = num % 10;

System.out.printf("%d %d %d %d",  thousands, hundreds, tens, ones);

1 9 5 0.

+3

Considering

int i;

you can get its String value with Integer.toString(i).

+1
source

To convert between String and int, you can use Integer.parseInt (text), and it will provide you back with an Integer object that will not be available for input if you want.

To convert another path, you can insert your int into an Integer object and call the toString string on it.

So you can create an overloaded method as follows:

public static boolean hasDuplicates(int value){
    return hasDuplicates(new Integer(value).toString());
}
0
source

All Articles