Java: Solving the Arrays Dilemma

  • Comp Sci
  • Thread starter Vintageflow
  • Start date
  • Tags
    Arrays Java
In summary, the conversation is about a programming assignment involving arrays. The task is to write a program that removes duplicate elements from an array of 10 integers input by the user, with the constraint that the integers are between 0 and 100. The program should include at least one function in addition to the main method, and should not use any class-level variables. The conversation also includes a snippet of code with two methods, one for inputting the numbers and another for removing duplicates, and a question about how to remove an element in arrays.
  • #1
Vintageflow
5
0
hello all, I was given an assignment on arrays, here is the task:

Write a program that inputs an array of 10 integers from the user, and removes the duplicate array elements. You may assume that all the integers are between 0 and 100, and you may input them from the user however you wish. The easiest way to do this is to store only the non-repeating elements (only store a number the first time you see it). Please write at least 1 function in addition to the main method, and pass the array into that function as a parameter. Don't use any class-level variables. Each variable should be declared inside a method. Here is some sample output:
Please enter 10 integers, hitting return after each one:
5
75
10
75
5
80
10
5
5
50
You entered 5 unique numbers:
5 75 10 80 50

here is what my program looks like:

import java.util.Scanner;

public class DeDup
{
static Scanner scan = new Scanner(System.in);

public static void main(String[] args)
{
int[] nums;
int numbers, array;
boolean number;

System.out.print("Please enter 10 integers, hitting return after each one: ");
numbers = scan.nextInt();
nums = new int[10];

for(int count = 1; count < 10; count++)
{
nums[count] = scan.nextInt();
}
}
public static int[] numMethod()
{
int i, j=0, arraySize=10;
int[] inputNum = new int[arraySize];

for (i=0; i<arraySize; i++)
{
inputNum = scan.nextInt();
}

for (i=0; i<arraySize; i++)
{
for(j=0; j<i; j++)
{
if (inputNum == inputNum[j])
inputNum = inputNum[j-1];
j--;
}
}

System.out.print(inputNum+" ");

return(inputNum);

}
}

my first method works fine (inputing the 10 numbers using an array), but I'm having trouble on the second method, where I get rid of the duplicate numbers and only display the unique numbers. How do you get rid of the duplicates? Is the second method even right?

thank you all for the help!
 
Physics news on Phys.org
  • #2
I added [ code] and [ /code] tags, without the leading spaces, around your code.
Vintageflow said:
hello all, I was given an assignment on arrays, here is the task:

Write a program that inputs an array of 10 integers from the user, and removes the duplicate array elements. You may assume that all the integers are between 0 and 100, and you may input them from the user however you wish. The easiest way to do this is to store only the non-repeating elements (only store a number the first time you see it). Please write at least 1 function in addition to the main method, and pass the array into that function as a parameter. Don't use any class-level variables. Each variable should be declared inside a method. Here is some sample output:
Please enter 10 integers, hitting return after each one:
5
75
10
75
5
80
10
5
5
50
You entered 5 unique numbers:
5 75 10 80 50

here is what my program looks like:
Code:
import java.util.Scanner;

public class DeDup
{
 static Scanner scan = new Scanner(System.in);
 
  public static void main(String[] args)
  {
  	 int[] nums;
	 int numbers, array;
	 boolean number;
	 
	 System.out.print("Please enter 10 integers, hitting return after each one: ");
	 numbers = scan.nextInt();
	 nums = new int[10];
	 
	 for(int count = 1; count < 10; count++)
	 {
	 	nums[count] = scan.nextInt();
	 }
  } 
  public static int[] numMethod()
  {
   int i, j=0, arraySize=10;
   int[] inputNum = new int[arraySize];
               
   for (i=0; i<arraySize; i++)
   {
     inputNum[i] = scan.nextInt();
   }
               
   for (i=0; i<arraySize; i++)
   {
	  for(j=0; j<i; j++)
	  {	
		if (inputNum[i] == inputNum[j])
        inputNum[i] = inputNum[j-1];
		  j--;  
     }                  
   }

     System.out.print(inputNum[i]+" ");
     
	  return(inputNum);

  }
}
my first method works fine (inputing the 10 numbers using an array), but I'm having trouble on the second method, where I get rid of the duplicate numbers and only display the unique numbers. How do you get rid of the duplicates? Is the second method even right?

thank you all for the help!
 
  • #3
Your second function should not be doing input - you already have done the input in your Main method, so there is no reason for your numMethod to ask for them to be input again. Instead, pass the array as a parameter to your numMethod method.

I'm a little rusty with Java, so don't remember if a function can return an array type. You can't do this in C or C++, but possibly you can in Java.

If this is allowed, have your numMethod (which BTW is not a useful name) return a different array with the duplicates removed. If returning an array is not allowed, write the numMethod method with two parameters, one of which represents the input array and the other, the array with duplicates removed.
 
  • #4
Thank you for the reply Mark44 :)

One more question, if you remember, do you know how to remove an element in arrays?
 
  • #5
Vintageflow,

Yes, you can pass an array to a method. There are a number of problems with what you've written though.

Your assignment has the following instructions:

1. Write a program that inputs an array of 10 integers from the user, and removes the duplicate array elements.
2. You may assume that all the integers are between 0 and 100, and you may input them from the user however you wish. The easiest way to do this is to store only the non-repeating elements (only store a number the first time you see it).
3. Please write at least 1 function in addition to the main method, and pass the array into that function as a parameter.
4. Don't use any class-level variables. Each variable should be declared inside a method.

Here are some of the problems that I've found.
1. scan is a class-level variable. (violates #4 of your requirements)
2. the int array and boolean number variables in the main method are not being used by anything. Same for the j=0 in the numMethod().
3. The first scan.nextInt() call puts its number into the numbers variable and doesn't do anything with it.
4. The nums[] array doesn't put anything into its first position. Your for loop should start with int count = 0;
5. The numMethod() method doesn't take an array as required, it returns one. The method also isn't being called by anything. Your program never leaves the main method. (#3 of your requirements)
6. If the user types something other than a number, the program throws an error and ends. You should always catch basic errors like this.

I'll give you the shell of the main method and another method. Mark is right on the naming thing so let's change that to a method called checkNumber. Keep in mind that the position of the first array is 0. Therefore, int[] nums = new int[1] is an array of size one but the data is entered in the num[0] position. You can use this to your advantage in the checknumber method.

Code:
import java.util.Scanner;

public class DeDup {

    public static void main(String[] args) {
        // I'm setting this to zero for now.  There is a reason for this...
        int[] nums = new int[0];
        Scanner scan = new Scanner(System.in);

        System.out.println("Please enter 10 integers, hitting return after each one: ");

        for (int count = 0; count < 10; count++) {
            try {
                nums = checkNumber(scan.nextInt(), nums);
            } catch (Exception e) {
                // If the user typed something other than a number, what should the program do?

            }
        }

        System.out.println("You entered " + nums.length + " unique numbers:");
        // Write something to display the numbers
		
    }

    private static int[] checkNumber(int number, int[] nums){
        // newNums is always an array that is one larger than nums
        int[] newNums = new int[nums.length+1];
        for(int i = 0; i < nums.length; i++){
            // What do you want to do if you find a match?
            if(number == nums[i]){
                // Hint, you want to return something

            }
            // Add to the new array
            newNums[i]=nums[i];
        }
        // If you got here without finding a match, what do you want to do with the number?

        return newNums;
    }
}
 
  • #6
Borg, here is my attempt to the answer to the questions you left in the program:

// If the user typed something other than a number, what should the program do?
- an if statement, saying that a word or character can not be used.

// What do you want to do if you find a match?
- remove it? If it is, I do not know how to remove an element in an array.

// Hint, you want to return something
- you want to return the numbers that are NOT going to be displayed?

// If you got here without finding a match, what do you want to do with the number?
- you want to display those numbers in your main method.

If these questions are wrong I'm sorry, arrays are confusing to me. :/
 
  • #7
Vintageflow said:
Borg, here is my attempt to the answer to the questions you left in the program:

// If the user typed something other than a number, what should the program do?
- an if statement, saying that a word or character can not be used.
Yes but, it doesn't need an if statement. Also, do you need to do something with count if an error occurs?
Vintageflow said:
// What do you want to do if you find a match?
- remove it? If it is, I do not know how to remove an element in an array.
// Hint, you want to return something
- you want to return the numbers that are NOT going to be displayed?
Let's ignore the if(number == nums) statement for now. What is this method doing? Let's say that the incoming array (nums) has six numbers in it. At the beginning, we made newNums able to hold 7 numbers (nums.length+1).
In the loop we are copying numbers from nums and putting them into newNums one at a time. When the loop finishes, you will have two arrays - one that has the original numbers (nums) and one that has the originals plus a final place for the new number (newNums). You don't have to remove anything - just choose which one you want to return.
Vintageflow said:
// If you got here without finding a match, what do you want to do with the number?
- you want to display those numbers in your main method.
Has the new number been put into newNums yet? The for loop above is only copying values from nums (which doesn't have the new number in it).
Vintageflow said:
If these questions are wrong I'm sorry, arrays are confusing to me. :/
Not a problem. We're here to help.
 
Last edited:

Related to Java: Solving the Arrays Dilemma

1. What is Java and what is its role in solving the arrays dilemma?

Java is a programming language that is commonly used for developing web, mobile, and desktop applications. In the context of solving the arrays dilemma, Java provides a variety of built-in methods and functions that allow for efficient manipulation and processing of arrays.

2. What is an array and why is it important in Java?

An array in Java is a data structure that can hold a fixed number of elements of the same type. It is important in Java because it allows for the storage and manipulation of multiple values in a single variable, making it easier to work with large sets of data.

3. What is the "arrays dilemma" and how does Java solve it?

The arrays dilemma refers to the challenge of efficiently processing and manipulating arrays of data. It can be difficult to keep track of indices and properly access and modify elements in an array. Java solves this dilemma by providing built-in methods and functions, such as for loops and array lists, that simplify the process of working with arrays.

4. Can arrays be used for more than just storing data?

Yes, arrays can also be used for sorting and searching data. Java provides sorting algorithms, such as the quicksort and mergesort, that can efficiently sort arrays of data. Additionally, arrays can be used in conjunction with other data structures, such as linked lists, to implement more complex algorithms and data processing tasks.

5. Are there any limitations to using arrays in Java?

Yes, there are a few limitations to using arrays in Java. One limitation is that arrays have a fixed size, meaning the number of elements in an array cannot be changed once it is initialized. Additionally, arrays can only store elements of the same type, so they may not be suitable for storing heterogeneous data.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
18
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
21
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
12
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
Back
Top