I am trying to create an array and remove the repetition except for the spaces (" "). I have seen some of the methods online, but I'm trying to do it without copying. I've tried many ways but I just don't understand why my methods don't work.
This is what I've tried:
In the first one the methodology is to receive input, call function, create an arraylist, traverse through the string whilst adding each character to the arraylist, traversing through the list a second time to compare each character to the character at the first index, checking if they are equal then removing them from the arraylist
The issue seems to be "checking if they are equal then removing them from the arraylist" :
if(character.get(i).equals(character.get(k))) {
character.remove(arr.charAt(i));
}
And for the second one
for(int k = 0;k < characters.length;k++) {
if(characters[i]==characters[k]) {
System.out.print(character);
character.remove(characters[i]);
}
import java.util.*;
public class NoRepeats {
public static void main(String args[]) {
System.out.print("Please enter a phrase: ");
Scanner scanner = new Scanner(System.in);
String input = scanner.nextLine();
char[] characters = chars(input);
}
public static char[] chars(String arr) {
ArrayList<Character> character = new ArrayList<Character>();
for(int i = 0; i<arr.length();i++) {
character.add(arr.charAt(i));
for(int k = 1; k<arr.length();k++) {
if(character.get(i).equals(character.get(k))) {
character.remove(arr.charAt(i));
}
}
}
System.out.print(character);
return null;
}
}
import java.util.*;
public class NoRepeats {
public static void main(String args[]) {
System.out.print("Please enter a phrase: ");
Scanner scanner = new Scanner(System.in);
String input = scanner.nextLine();
//receive input
char[] characters = chars(input);
//call function
}
public static char[] chars(String arr) {
ArrayList<Character> character = new ArrayList<Character>();
//create an empty arraylist
char[] characters = arr.toCharArray();
for(int j = 0; j<characters.length;j++) {
character.add(characters[j]);
}
for(int i = 0; i<characters.length;i++) {
/*traverse through the orignal array adding a character to
the empty array list each time*/
for(int k = 0;k < characters.length;k++) {
if(characters[i]==characters[k]) {
System.out.print(character);
character.remove(characters[i]);
}
}
}
return null;
}
}
Expected input:
“And I think to myself: what a wonderful world!”
Expected output:
And I thk o myself: w ru !
The first one produces this error:
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index 1 out of bounds for length 1 at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64) at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:70) at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:266) at java.base/java.util.Objects.checkIndex(Objects.java:359) at java.base/java.util.ArrayList.get(ArrayList.java:427) at NoRepeats.chars(NoRepeats.java:20) at NoRepeats.main(NoRepeats.java:10)
The second one produces this error:
"Exception in thread "main" java.lang.IndexOutOfBoundsException: Index 118 out of bounds for length 2 at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64) at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:70) at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:266) at java.base/java.util.Objects.checkIndex(Objects.java:359) at java.base/java.util.ArrayList.remove(ArrayList.java:504) at NoRepeats.chars(NoRepeats.java:33) at NoRepeats.main(NoRepeats.java:11) "
First need to formulate your algorithm in words (or kind od pseudocode). Something like this: Problem: remove duplicated chars from string except space. Solution (pseudocode):
Now you can implement this in any language you want. Implementation in java can be: