1

I made this piece of code that should ask the user enter the length of first arrays that will contain how much numbers that he want, than recieving every number untill the amount is done. after that he need to ask for the length of digits array, and than recieving the digits. than, I need to check if all the numbers contain all the digits, if its true, print it, if not, print false. In this code I already replace the int[] with string arrays, Because I think this is the way it whould work. when I tried to make an Int arrays, with only single digit numbers, It works great, The problem comes when you want number like 22 with 2 digits, the checking breaks and not working.

String numInNumbersArray,
digitInDigitsArray;
int counterNumbers=0;
System.out.println("Please enter the length of the numbers array: ");
int numbersLength=s.nextInt();
String[] numbersArray=new String [numbersLength];

System.out.printf("Please enter %d numbers: ",numbersLength);
    for(int i=0;i<numbersArray.length;i++){
        numInNumbersArray=s.next();
        numbersArray[i]=numInNumbersArray;
    }
System.out.println("Please enter the length of the digits array: ");
int digitsLength=s.nextInt();
String[] digitsArray=new String [digitsLength];
System.out.printf("Please enter %d digits between 0 - 9 and - if you want: ",digitsLength);
    for(int i=0;i<digitsArray.length;i++){
        digitInDigitsArray=s.next();
        digitsArray[i]=digitInDigitsArray;
        }
    for(int i=0;i<digitsArray.length;i++){
        for(int j=0;j<numbersArray.length;j++){
            if(numbersArray[j].equals(digitsArray[i])){
                counterNumbers++;
            }
        }
    }
    if(counterNumbers==numbersArray.length){
        System.out.println("true\n");
    }else{
        System.out.println("false\n");
    }

So I obviously need a String array, But I have no idea how to continue from here to check every digit from a number. ( I cannot use advanced methods, Only simple checks). Your help or tuning would be great. thanks.

EDIT: It must be string because it also maight contain '-' (negetive numbers) to make it more clealry to understand I will add examples:

digits arrays for example : (1,2,-,3)
and numbers array : (1,-2,3)

this should return true.

and digits: (1,2,3)
numbers: (12,-3,123)

should return false.

rassko
  • 45
  • 6
  • `numbersArray[j]==digitsArray[i]` No, you compare Strings with ==.. Argh ! For more infos, see [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Alexis C. Nov 29 '14 at 22:12
  • Oops. thats my bad, But it won't change my main problem anyway, Because I need to move on the whole number for each digit and check, and thats my main problem – rassko Nov 29 '14 at 22:16
  • Your problem is not fully clear:" I need to check if all the numbers contain all the digits" - you have to say in better way, or even give example, when it is true, and when false. But anyway base for this will be dividing numbers, that you are getting into digits. So first you are saving input to int[] array, and then on every int in this array you are making something like this: do { newArray[index++] = number % 10; number /= 10; } while (number > 0) – Mateusz Pryczkowski Nov 29 '14 at 22:23
  • Is this a typo: digits arrays for example : (1,2,-,3). Should this be digits arrays for example : (1,-2,3). If not, I think I misunderstood your quesiton – David.Jones Nov 29 '14 at 22:39
  • there is 2 arrays, one with numbers the user input, like 21 123 -10 or what ever he wants, and should be another array the user input with "legal" digits like 1,2,-,3 and so on (depends what the input is), I need to check if the all the digits in each number from the number array, contains all the legal digits – rassko Nov 29 '14 at 22:46

1 Answers1

0

You problem lies within your check to see if the arrays are equals.

Imagine if numbersArray = {"2", "2"} and digitsArray = {"2", "2"} like you stated above.

This loop:

for(int i=0;i<digitsArray.length;i++){
    for(int j=0;j<numbersArray.length;j++){
        if(numbersArray[j].equals(digitsArray[i])){
            counterNumbers++;
        }
    }
}

will compare the first "2" - numbersArray[0] with digitsArray[0] AND digitsArray[1], making counterNumbers = 2. Then, the first for loop will process to i=1, where it will come the second "2" - numbersArray[1] with digitsArray[0] AND digitsArray[1] making counterNumbers = 4.

Do you see your fault here?

Here is a hint, you should be comparing each array "digit-by-digit" rather than comparing the first "digit" in numbersArray to all of the "digits" in digitsArray. NOTE: This hint is only useful if you have each array (numbersArray and digitsArray) in the same order. You should look into sorting them to ensure that.

Since this seems like a homework assignment i'll stop here and let you try to fix it yourself.

David.Jones
  • 1,413
  • 8
  • 16
  • thanks for your comment, as I wrote, I know what needed to be done, I just dont know how to do that, thats what I asked for :) a hint of how to start do that. – rassko Nov 29 '14 at 22:36