Java – numbers and occurrences in the array
My job is to use the package implementation of array. You can add and delete numbers as needed So far, I have successfully completed everything except the number list, which should be like this:
I don't know where to start, so some pointers will be good, please and thank you (Note: I don't allow anything except arrays, so ArrayList, collection, etc. can't help me.)
Edit: I created another array for the count. In some cases, the code seems to work properly However, sometimes when I run it, it will give me a completely wrong output. I don't know how to solve it? For example, if I enter numbers (in this order) 11,22,11,33,22, I get a reply:
import java.util.Scanner;
public class Bag {
int index = 0;
int[] array = new int[50];
public static void main(String[] args) {
int x = 0;
Bag bag = new Bag();
Scanner scan = new Scanner(system.in);
while (x == 0) {
System.out.print("Add(A),Delete(D),Find(F),Size(S),Min(m),Max(M),List(L),Quit(Q) >> ");
char c = scan.next().charAt(0);
switch (c) {
case 'A':
int y = scan.nextInt();
bag.Add(y);
break;
case 'D':
int d = scan.nextInt();
bag.Delete(d);
break;
case 'F':
int z = scan.nextInt();
bag.Find(z);
break;
case 'S':
bag.Size();
break;
case 'm':
bag.Min();
break;
case 'M':
bag.Max();
break;
case 'L': bag.List();
break;
case 'Q': bag.Quit();
}
}
}
public void Add(int y) {
array[index] = y;
index++;
System.out.println(" " + y + " is added to the Bag. ");
}
public void Delete(int d) {
for (int i = 0; i < index; i++) {
if (d == array[i]) {
while (i < index) {
array[i] = array[i + 1];
i++;
}
System.out.println(" " + d + " is deleted from the Bag.");
index--;
return;
}
}
System.out.println(" Cannot delete " + d
+ ". It does not exist in the Bag.");
}
public void Find(int z)
{
int count = 0;
for (int i = 0; i < array.length; i++) {
if (array[i] == z) {
count++;
}
}
System.out.println(" There is (" + count + ") " + z
+ " in the Bag.");
}
public void Size() {
System.out.println(" There are " + index + " numbers in the Bag.");
}
public void Min() {
int min = array[0];
for (int i = 1; i < index; i++) {
if(min > array[i]) {
min = array[i];
}
}
System.out.println(" The minimum number in the Bag is " + min + ".");
}
public void Max() {
int max = array[0];
for (int i = 1; i < index; i++) {
if(max < array[i]) {
max = array[i];
}
}
System.out.println(" The maximum number in the Bag is " + max + ".");
}
public void Quit() {
System.out.println("Bye…");
}
public int Count(int c) {
int elements = 0;
for (int i = 0; i < array.length; i++) {
if (array[i] == c) {
elements++;
}
}
return elements;
}
public void delete(int c) {
for (int i = 0; i < index; i++) {
if (c == array[i]) {
while (i < index) {
array[i] = array[i + 1];
i++;
}
index--;
return;
}
}
}
public void List() {
System.out.println("+--------+--------+");
System.out.println("| Number | Occurs |");
System.out.println("+--------+--------+");
for(int i = 0; i < index; i++){
if(Count(array[i]) == 1){
System.out.printf("|%8d|%8d|\n",array[i],Count(array[i]));
System.out.println("+--------+--------+");
}
else{
System.out.printf("|%8d|%8d|\n",Count(array[i]));
System.out.println("+--------+--------+");
for(int j = 0; j <= Count(array[i]); j++){
delete(array[i]);
}
}
}
}
}
Solution
It looks like you have a problem with the list () method Change it to:
public void List() {
System.out.println("+--------+--------+");
System.out.println("| Number | Occurs |");
System.out.println("+--------+--------+");
while(index > 0){
int del = array[0];
int count = Count(del);
System.out.printf("|%8d|%8d|\n",del,count);
System.out.println("+--------+--------+");
for(int j = 0; j < count; j++){
delete(del);
}
}
}
+--------+--------+ | Number | Occurs | +--------+--------+ | 11| 3| +--------+--------+ | 22| 2| +--------+--------+ | 33| 1| +--------+--------+
In addition, array Changing length to index will allow adding 0
public int Count(int c) {
int elements = 0;
for (int i = 0; i < index; i++) {
if (array[i] == c) {
elements++;
}
}
return elements;
}
