List of number divisors

This is my first post, so DO stomp me if I wrote something stupid.

I just started IT classes, and today at the while class my teacher gave us the following homework:

Write a program that reads a positive integer n and displays in a single graphic window all its divisors from the interval [2; n-1].

So far I have come up with a code that works, but the result is a bit wrong:

import java.util.Arrays; import javax.swing.JOptionPane; public class Divisors { public static void main(String[] args) { String n = JOptionPane.showInputDialog(null, "Enter a natural number"); Integer i = Integer.parseInt(n); int d = i - 1; int x = 2; int[] dvr = new int[i]; // [i] because bigger numbers need more iterations while (x >= 2 && x <= d) { double y = i % x; if (y == 0) { dvr[x] = x; x = x + 1; } else { x = x + 1; } } JOptionPane.showMessageDialog(null, "The divisors of " + i + " are:\n" + Arrays.toString(dvr)); } } 

The problem is that the loop fills the array with lots of zeros, and a screenshot of the results of the tutor shows a window that lists only the divisors.

I tried to do this with an ArrayList, but this black magic is for me right now, and my teacher has not yet taught us how to use anything outside that is used in my code.

Any help was greatly appreciated.

+5
source share
4 answers

The main problem that you are facing is that you will have an unknown number of values ​​that you want to print, but you use an array to store them, and the arrays have a fixed size. Since you have an int array, it will be completely populated with a default zero value.

Ideally, you should print only the first group of nonzero values ​​in your array, but you keep the divisors scattered throughout the array.

dvr[x] = x; stores each value with an index of this value, when you really just need to save each new value in the next open spot in the array.

Create a separate index variable and save each value using instead:

  int index = 0; while (x >= 2 && x <= d) { ... if (y == 0) { dvr[index++] = x; ... 

Then, when your main loop is done, you can create a new “display array” that contains only dividers, not zeros. At this point, index tells you how much it should be:

  int[] display = Arrays.copyOf(dvr, index); JOptionPane.showMessageDialog(null, "The divisors of " + i + " are:\n" + Arrays.toString(display)); 
+4
source

In Java, the default value for int is zero. That is why you see so many zeros.

Since you define the size of the array as i , which is larger than required, since the number of divisors will always be less than i .

So, instead of printing the entire array, you should print it only to the full number of divisors, for which you should use a separate variable instead of x .

Here is a modified version where I use a separate index variable to keep track of the number of divisors starting with 0 . In the end, you can simply print the array to index

 import java.util.Arrays; import javax.swing.JOptionPane; public class Divisors { public static void main(String[] args) { String n = JOptionPane.showInputDialog(null, "Enter a natural number"); Integer i = Integer.parseInt(n); int d = i - 1; int index = 0; int x=2; int[] dvr = new int[i]; // [i] because bigger numbers need more iterations while (x >= 2 && x <= d) { double y = i % x; if (y == 0) { dvr[index] = x; x = x + 1; index= index + 1; } else { x = x + 1; } } JOptionPane.showMessageDialog(null, "The divisors of " + i + " are:\n" + Arrays.copyOfRange(drv, 0, index)); } } 
+3
source

Set datastructure avoids duplicates, you can use this to overcome the problem of duplicating divisors added to the data structure.

  import java.util.*; import javax.swing.JOptionPane; public class Divisors { public static void main(String[] args) { String n = JOptionPane.showInputDialog(null, "Enter a natural number"); Integer i = Integer.parseInt(n); int d = i - 1; int x = 2; Set<Integer> divisors = new HashSet<>(); while (x >= 2 && x <= d) { double y = i % x; if (y == 0) { divisors.add(x); x = x + 1; } else { x = x + 1; } } List<Integer> l = new ArrayList<>(divisors); JOptionPane.showMessageDialog(null, "The divisors of " + i + " are:\n" + l); } } 
+1
source

Use ArrayList to create a dynamic array.
Below code will help you.
What will change in your program.

  • import java.util. *;
  • select the variable ArrayList
  • call toString method of Arraylist object
 import java.util.*; import javax.swing.JOptionPane; public class NewClass3 { public static void main(String[] args) { String n = JOptionPane.showInputDialog(null, "Enter a natural number"); Integer i = Integer.parseInt(n); int d = i - 1; int x = 2; List<Integer> dvr = new ArrayList<>(); while (x >= 2 && x <= d) { double y = i % x; if (y == 0) { dvr.add(x); x=x+1; } else { x = x + 1; } } JOptionPane.showMessageDialog(null, "The divisors of " + i + " are:\n" + dvr.toString()); } } 
+1
source

Source: https://habr.com/ru/post/1233548/


All Articles