0

I know some people have asked about duplicates already, and I've looked through all those answers but none seem to help me/ I still can't get it to compile. Is there something obvious in my code that I am missing???? Help (new to programming!)

I need to write a method that takes one argument, an array of String values, and returns a new array that has no duplicate values and no need to order the original array. For example

String [] abc = {"A", "C", "C", "B", "A", "C", "B", "B", "A"};
String [] new = removeDuplicates (abc);
for (int i = 0; i < new.length; i++)
{
    System.out.print (new[i] + " ");
}
System.out.println();

The ouput should be: A C B

Here is my code:

    import java.util.*;

    class Duplication
    {
        public static void main (String [] args)
        {
            String [] values = {"A", "C", "C", "B", "A", "C", "B", "B", "A"};
            String [] newList = removeDuplicates (values);
        }
        private static String removeDuplicates (String [] originalArray)
        {
            for (int i = 0; i < newList.length; i++)
            {
                System.out.print (newList[i] + " ");
            }
            System.out.println();

            String noDuplicates = originalArray.length;
            for (int d = 0; d < noDuplicates; d++)
            {
                for (int e = d + 1; e < noDuplicates; e++)
                {
                    if(originalArray[d] == originalArray[e])
                    {
                        originalArray[e] = originalArray[noDuplicates-1];
                        noDuplicates--;
                        e--;
                    }
                }
            }
        }  
    }

Six errors:

    Duplication.java:9: error: incompatible types
    String [] newList = removeDuplicates (values);
                                     ^
    required: String[]
    found:    String
    Duplication.java:14: error: cannot find symbol
    for (int i = 0; i < newList.length; i++)
                        ^
    symbol:   variable newList
    location: class Duplicates
    Duplication.java:16: error: cannot find symbol
        System.out.print (newList[i] + " ");
                          ^
    symbol:   variable newList
    location: class Duplicates
    Duplication.java:21: error: incompatible types
    String noDuplicates = originalArray.length;
                                       ^
    required: String
    found:    int
    Duplication.java:32: error: bad operand types for binary operator '-'
                originalArray[e] = originalArray[noDuplicates-1];
                                                             ^
    first type:  String
    second type: int
    Duplication.java:35: error: bad operand type String for unary operator '--'
                noDuplicates--;
                            ^
3
  • 1
    please rename the array from new to something else.. Commented Apr 9, 2017 at 16:37
  • 1
    Two questions: (1) where is your { after public static void main? (2) Are you attempting a method inside a method? Because that's not allowed. Commented Apr 9, 2017 at 16:38
  • 1. new is a reserved word and no variable in Java can have that name. 2. It's incorrect to define a method inside another method. Commented Apr 9, 2017 at 16:45

4 Answers 4

1

This is invalid in java:

public static void main (String [] args)
String [] values = {"A", "C", "C", "B", "A", "C", "B", "B", "A"};
String [] newList = removeDuplicates (values);

you have to add the { } to define the scope of the static main method:

public static void main (String [] args){
      String[] values = {"A", "C", "C", "B", "A", "C", "B", "B", "A"};
      String[] newList = removeDuplicates (values);
}
Sign up to request clarification or add additional context in comments.

Comments

1

1) Your method removeDuplicates () return a String. In your main block, you are expecting an array of String.

2) The scope variable newList is inside the main block. So, this variable is not visible in the method removeDuplicates

3) "originalArray.length" return an "int", not a String.

Comments

1

You should read the error messages.

 Duplication.java:9: error: incompatible types
 String [] newList = removeDuplicates (values);

This function return String which is not String []

 private static String removeDuplicates (String [] originalArray)

You should declared it as

 private static String [] removeDuplicates (String [] originalArray)

You should declare newList within the scope of the function to use it, this causes

Duplication.java:14: error: cannot find symbol
for (int i = 0; i < newList.length; i++)

Lengths are integer and should be treated as such like:

int noDuplicates = originalArray.length;

Read carefully the compiling errors, they address all the problems.

Comments

0

Thats all the code you need. You can read for the collections here https://docs.oracle.com/javase/tutorial/collections/

import java.util.*;

    class Duplication
    {
        static String [] values = {"A", "C", "C", "B", "A", "C", "B", "B", "A"};
        public static void main (String [] args){
            System.out.println(removeDuplicates(values));

        }
        static Set removeDuplicates(String [] originalArray)
        {
            List arList = new ArrayList<String>(Arrays.asList(values));
            return new LinkedHashSet<String>(arList);
        }  
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.