Prime numbers in an array with single digits

I am exceptionally new to programming, but I am working to improve my programming skills. I am currently working on a problem that I have given myself, where I am trying to take a variable number and make each of its digits a separate number in the array. I don’t care about the order of the numbers, so if they change, then it doesn’t matter to me. I know that people have asked this question many times, but they always seem to use a lot of things that I don’t understand. Since my school does not offer Java courses, I only know what I learned on my own, so if you could explain any terms that you use in code that is not extremely trivial, that would be great. Right now, I wrote:

int number = 1234567890; while (number > 0) { System.out.println(number%10); number = number/10; 

This is great for printing numbers individually, but I can't figure out how to add them to the array. I am very grateful for any help you can give, and please keep in mind that I prefer simplicity to small sizes. Thank you in advance!

PS Some of the answers I saw on similar questions include what I consider to be arrays of strings. In order for the part of the program that I was working on to still work, I believe that I need to use an array of integers. If you're interested, the rest of the code is used to determine if the numbers in the array are different, to achieve the final result of determining if the digits of the digits are different. It looks like this:

 int repeats=0; int[] digitArray; digitArray = new int[10]; for (int i = 0; i < digitArray.length; i++) for (int j = 0; j < digitArray.length; j++) if ((i != j) && (digitArray[i]==digitArray[j])) unique = unique+1; System.out.println(unique==0); 
+7
source share
4 answers

Method number.toString (). length () will return the number of digits. Same as the length of your required array. Then you use your code as before, but instead of printing, you add a digit to the array.

 int number = 1234567890; int len = Integer.toString(number).length(); int[] iarray = new int[len]; for (int index = 0; index < len; index++) { iarray[index] = number % 10; number /= 10; } 
+7
source

I would prefer to use ArrayList , because to use an array you will need to specify the size in advance, for which you need to know the number of digits in your number that you do not know.

So, either work with the array, or iterate over the number twice - once to determine the size, and then to do the actual work. Else, get ahead with the ArrayList .

Adding an element to an ArrayList pretty simple. You just need to call the List#add(E) method with the appropriate parameter.

So here is an extension of your solution: -

 // Declare a List<Integer>, since it will store integers only. List<Integer> digits = new ArrayList<Integer>(): int number = 1234567890; while (number > 0) { int digit = number % 10; // Store digit in a variable number = number/10; // Add digit to the list digits.add(digit); } 

Alternatively, if you want to have only unique numbers in the List , then you should use a HashSet , which automatically removes duplicates.

+5
source

With Java 8:

Integer.toString (n) .chars (). The mapping (a-> a-'0 '). ToArray ()

0
source
 char [] arr = scan.next().toCharArray(); 

This code will read the number from scan.next() , and then it will give it as an input to the char array, which will have a number in its indices as one digit in a digit.

Hope this helps.

0
source

All Articles