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--;
^
{afterpublic static void main? (2) Are you attempting a method inside a method? Because that's not allowed.newis a reserved word and no variable in Java can have that name. 2. It's incorrect to define a method inside another method.