Adding space between uppercase character java - java

I am a beginner in Java. Below is a piece of code that I wrote. the purpose is to add a space between each upper case character for example for string="ILoveMyDog" to
"I Love My Dog". However the outcome of this code is "ILoveMy Dog". Can someone help me figured out what is wrong? upperCaseList is another method to extract the index of all uppercase character and put them into a list, and I am pretty sure that part is correct
for (int i = 0; i < upperCaseList.size(); i++) {
newStr = w.replace(w.substring(upperCaseList.get(i), upperCaseList.get(i)+1), " "+ w.substring(upperCaseList.get(i), upperCaseList.get(i)+1));
}
return newStr

It is because you are overwriting the result of String.replace by reassigning newStr inside the loop. So, you only see the effect of the last replacement.
Assuming the content of upperCaseList is 1, 5, 7.
After Loop iteration 1, newStr has I LoveMyDog
After Loop iteration 2, newStr has ILove MyDog (you are not using the previous result, rather using the original string)
After Loop iteration 3, newStr has ILoveMy Dog
Try this,
String newStr = w;
for (int i = 0; i < upperCaseList.size(); i++) {
newStr = newStr.replace(w.substring(upperCaseList.get(i), upperCaseList.get(i)+1), " "+ w.substring(upperCaseList.get(i), upperCaseList.get(i)+1));
}
There are plenty of ways to solve this though. Rather than storing the list of indices containing the uppercase characters and using String.substring and String.replace, you can use StringBuilder to build up the string from the original string by looping through the characters and checking if it is upper/lower cased.
StringBuilder resultBuilder = new StringBuilder();
for (int i = 1; i < w.length(); i++) { //Note: Starting at index 1
if (Character.isUpperCase(w.charAt(i))) {
resultBuilder.append(" ")
.append(w.charAt(i));
} else {
resultBuilder.append(w.charAt(i));
}
}
System.out.println(resultBuilder.toString());

Not sure how you are creating the upperCaseList, I suggest to have one loop for all. Hope below code will work for your requirement.
public void test(){
String str ="ILoveMyDog";
StringBuilder strBuilder = new StringBuilder();
for (int i = 0; i< str.length() ; i++) {
if(Character.isUpperCase(str.charAt(i))){
//add space
strBuilder.append(" ");
}
//add the orginal char
strBuilder.append(str.charAt(i));
}
//use toString method
System.out.println(strBuilder.toString());
}

Related

How do I get rid of the comma at the end? (Java)

I'm working on an assignment and the code is working perfectly, except for one small little problem.
For example at the end it's supposed to say, "t,e,s,t" but instead it prints out "t,e,s,t,".
import javax.swing.JOptionPane;
public class program {
public static int divide(String input) {
int length=0;
String output = "";
for (int i=0; i<input.length(); i++) {
length++;
output += input.charAt(i);
output += ",";
}
JOptionPane.showMessageDialog(null, "Eingabe: " +input+ "\nAnzahl der Zeichen: " +length+ "\nZeichen: \n" +output);
return length;
}
}
I'd just like to say for those who are planning on giving tips, please note that this is an assignment so I'm not really allowed to make any MAJOR changes to it. This program HAS to be:
Solved using a for loop
The output HAS to be in JOptionPane
It has to be in a method (for I'll have to write an inputDialog later in the main-method, but that's unimportant right now).
My only problem with it, for example the output would have to say (I'll translate the output in english, since I'm at an austrian school) if the string was "hello", the program would say "letters: h, e, l, l, o" but instead it says "h, e, l, l, o," with a comma at the end, how do I get rid of it?
(Also sorry if I messed up any variables, I renamed them all from German to English for this post so I hope I didn't mess any of them up)
One of the possibilities is to print comma not after each character, but before every character except the first one.
You only want to add a comma if there is more data to come after. So you can do it in two ways:
add the comma before the text if there's already something in the string:
String output = "";
for (int i=0; i<input.length(); i++) {
length++;
if (output.length() > 0) output += ",";
output += input.charAt(i);
}
add the comma after unless it's the last element:
String output = "";
for (int i=0; i<input.length(); i++) {
length++;
output += input.charAt(i);
if (i < input.length() - 1) output += ",";
}
Personally I like the first way.
Hello I used an if statement to check if we are at the first letter and if we are then we don't write a comma, here is the code:
{
int length = 0;
String output = "";
int i = 0;
for ( i = 0; i < input.length(); i++)
{
if(i == 0)
{
length++;
output += input.charAt(i);
}
else
{
output += ",";
output += input.charAt(i);
length++;
}
}
Console.WriteLine(output);
}
You only need to check whether you are at the last character, and if you are, then break out from the loop.
for (int i=0; i<input.length(); i++) {
length++; //you do not seem to need this
output += input.charAt(i);
if (i==(input.length()-1)) break; //checking whether we are at the last character
output += ",";
}
Two additional notes:
Please follow the Java Naming Conventions and use PascalCase for your class names, this is very important;
It would make your code much more efficient if you'd use StringBuilder instead of String, to concatenate characters and dynamically build your string. String is immutable and per each concatenation, you're actually creating a new instance of it, which is expensive.
I would use a StringJoiner
StringJoiner output = new StringJoiner(",");
for (int i = 0; i < input.length(); i++) {
length++;
output.add(input.substring(i, i + 1));
}
JOptionPane.showMessageDialog(… + output.toString());
so You are not allowed to use StringJoiner
String output = "";
String comma = "";
for (int i = 0; i < input.length(); i++) {
length++;
output += comma;
output += input.charAt(i);
comma = ",";
}
Your loop iterates through every character, appending both the character and a comma. This includes the final character. You need to find a way to avoid adding a comma after the final iteration;
Using String and appending characters one by one is very inefficient. This is what StringBuilder is designed for;
What is the purpose of the length variable? It can be replaced with input.length() - 1;
String.format() makes your code easier to read rather than chaining together string concatenations;
Don't be afraid to use a healthy amount of spacing throughout your code. It is much harder to read otherwise.
Something like this should work well:
public static int divide(String input) {
int length = input.length() - 1;
String output;
if (input.length() == 0) output = input;
else {
StringBuilder outputBuilder = new StringBuilder(input.charAt(0));
for (int i = 1; i < input.length(); i++) {
outputBuilder.append(',').append(input.charAt(i));
}
output = outputBuilder.toString();
}
JOptionPane.showMessageDialog(null, String.format("Eingabe: %s\nAnzahl der Zeichen: %d\nZeichen: \n%s", input, length, output);
return length;
}
Once you learn, use a StringJoiner or similar modern device. My favourite link is at the bottom. There will also be a time when you learn to use a StringBuilder or StringBuffer for assembling a string.
In the meantime, I still like what I learned in my first year of programming: when one iteration through a loop needs to be different, take it out of the loop. In my experience this often gives the clearest code. In your case:
String input = "Wort";
String output = "" + input.charAt(0);
for (int i = 1; i < input.length(); i++) {
output += ",";
output += input.charAt(i);
}
System.out.println("Zeichen: " + output);
Output:
Zeichen: W,o,r,t
In this case I have taken the first character outside the loop and add it to the output (without comma) already in the declaration of output. The loop starts from index 1 (not 0). Inside the loop I have to add the comma before adding the next char. In other cases one may put the last item outside the loop instead, the result will be the same.
Links
Answer by Lii to a similar question demonstrating all of String.join(), Collectors.joining() and StringJoiner.
StringBuilder vs String concatenation in toString() in Java

Getting the first letter of a word in an array to capitalize

So I know that to capitalize, you need to use .toUpperCase()
Now I have a String Array, and in that array there are words, like cat, bag, tag, dog etc.
My goal is to capitalize them so that the array says Cat, Bag, Tag, Dog
The line of code that I am using and am being suggested to use is
String firstLetter = Character.toString(array[i].charAt(0));
However, my program compiles, but crashes at that point.
Is this techinically the right way of doing this?
You can use a for loop and modify each string accordingly:
for(int i = 0; i < array.length; i++) {
if (array[i].length < 1) continue; //in case there is an empty string
String firstLetter = array[i].substring(0,1).toUpperCase();
String restOfString = array[i].substring(1,array[i].length-1);
array[i] = firstLetter + restOfString;
}
You need to use substring() to get the 1st char and the rest of each string:
for (int i = 0; i < array.length; i++) {
array[i] = array[i].substring(0, 1).toUpperCase() + array[i].substring(1);
}
If you're using Java 8:
String[] capitalizedArray = Arrays.stream(array)
.map(word -> word.substring(0, 1).toUpperCase() + word.substring(1)).toArray(String[]::new);

Reversing A String. Every other word reversing

So i've got a for loop that's reversing every other word in a string. I can't determine which condition is causing this.
for (int i = 0; i < words.length; i++)
{
stringBuilder.append(words[(words.length-1)-i]);
stringBuilder.reverse()
}
newMessage = stringBuilder.toString();
return Message
stringBuilder.reverse() reverse the whole string that you are currently building at each iteration.
Try:
for (int i = 0 ; i < words.length ; i++) {
String word = words[(words.length-1)-i];
String reverse = new StringBuilder(word).reverse().toString();
stringBuilder.append(reverse).append(" ");
}
Or even simpler, reversing at the end:
for (int i = 0 ; i < words.length ; i++) {
stringBuilder.append(words[(words.length-1)-i]).append(" ");
}
newMessage = stringBuilder.reverse().toString();
Edit based on comments:
for (String w : words) {
String reverse = new StringBuilder(w).reverse().toString();
stringBuilder.append(reverse).append(" ");
}
newMessage = stringBuilder.toString();
stringBuilder.reverse(); is reversing the whole word comment that line and your code wont reverse your new message
var string = "hello world";
function reverseWords(string) {
var words = string.split(' '),
finals = [];
words.forEach(function(word) {
finals.push(word.split('').reverse().join(''););
});
return finals.join(' ');
}
reverseWords(string); // "olleh dlrow"
First of all, your loop is more complex then it needs to be. If you want to reverse words starting from the end, you should just use the loop index to do that, you don't need the (words.length-1)-i calculation.
Another thing, when you call reverse() on a StringBuilder you are reversing the whole string not just the appended portion. What you can do is use a temp StringBuilder to perform the reversal and a temp String variable to separate reversal from appending.
Something like this:
StringBuilder reversedBuilder = new StringBuilder();
for (int i = words.length - 1; i >= 0; i --)
{
String reversed = reversedBuilder.append(words[i]).reverse().toString(); // reverse the word
stringBuilder.append(reversed).append(" ");
reversedBuilder.setLength(0); // clear the reversed
}
If you want, you can do this in a single line of code (added with comments for clarification):
for (int i = words.length - 1; i >= 0; i --)
{
stringBuilder.append(new StringBuilder() // create a temp string builder
.append(words[i]) // add the current word to temp string builder
.reverse() // reverse the current word in the temp string builder
.toString()) // add the reversed word to stringBuilder
.append(" "); // add the space to stringBuilder
}

Removing duplicates from a string

I am trying to remove duplicates from a String in Java. Here i what I have tried
public void unique(String s)
{
// put your code here
char[]newArray = s.toCharArray();
Set<Character> uniquUsers = new HashSet<Character>();
for (int i = 0; i < newArray.length; i++) {
if (!uniquUsers.add(newArray[i]))
newArray[i] =' ';
}
System.out.println(new String(newArray));
}
Problem with this is when I try to remove the duplicate I replace it with a space. I tried replacing the duplicate with '' but it cannot be done or I cant set the duplicate place to null. What is the best way to do this?
If you use regex, you only need one line!
public void unique(String s) {
System.out.println(s.replaceAll("(.)(?=.*\\1)", ""));
}
This removes (by replacing with blank) all characters that found again later in the input (by using a look ahead with a back reference to the captured character).
If I understand your question correctly, perhaps you could try something like:
public static String unique(final String string){
final StringBuilder builder = new StringBuilder();
for(final char c : string.toCharArray())
if(builder.indexOf(Character.toString(c)) == -1)
builder.append(c);
return builder.toString();
}
You can use BitSet
public String removeDuplicateChar(String str){
if(str==null || str.equals(""))throw new NullPointerException();
BitSet b = new BitSet(256);
for(int i=0;i<str.length();i++){
b.set(str.charAt(i));
}
StringBuilder s = new StringBuilder();
for(int i=0;i<256;i++){
if(b.isSet(i)){
s.append((char)i);
}
}
return s.toString();
}
You can roll down your own BitSet like below:
class BitSet {
int[] numbers;
BitSet(int k){
numbers = new int[(k >> 5) + 1];
}
boolean isSet(int k){
int remender = k & 0x1F;
int devide = k >> 5;
return ((numbers[devide] & (1 << remender)) == 1);
}
void set(int k){
int remender = k & 0x1F;
int devide = k >> 5;
numbers[devide] = numbers[devide] | (1 << remender);
}
}
This will work for what you are attempting.
public static void unique(String s) {
// r code here
char[] newArray = s.toCharArray();
Set<Character> uniqueUsers = new HashSet<>();
for (int i = 0; i < newArray.length; i++) {
uniqueUsers.add(newArray[i]);
}
newArray = new char[uniqueUsers.size()];
Iterator iterator = uniqueUsers.iterator();
int i = 0;
while (iterator.hasNext()) {
newArray[i] = (char)iterator.next();
i++;
}
System.out.println(new String(newArray));
}
without changing almost anything in your code, change the line
System.out.println(new String(newArray));
for
System.out.println( new String(newArray).replaceAll(" ", ""));
the addition of replaceAll will remove blanks
import java.util.*;
class StrDup{
public static void main(String[] args){
String s = "abcdabacdabbbabbbaaaaaaaaaaaaaaaaaaabbbbbbbbbbdddddddddcccccc";
String dup = removeDupl(s);
}
public static String removeDupl(String s){
StringBuilder sb = new StringBuilder(s);
String ch = "";
for(int i = 0; i < sb.length(); i++){
ch = sb.substring(i,i+1);
int j = i+1;
int k = 0;
while(sb.indexOf(ch,j)!=-1){
k = sb.indexOf(ch,j);
sb.deleleCharAt(k);
j = k;
}
}
return sb.toString();
}
}
In the code above, I'm doing the following tasks.
I'm first converting the string to a StringBuilder. Strings in Java are immutable, which means they are like CDs. You can't do anything with them once they are created. The only thing they are vulnerable to is their departure, i.e. the end of their life cycle by the garbage collector, but that's a whole different thing. Foe example:
String s = "Tanish";
s + "is a good boy";
This will do nothing. String s is still Tanish. To make the second line of code happen, you will have to assign the operation to some variable, like this:
s = s + "is a good boy";
And, make no mistake! I said strings are immutable, and here I am reassigning s with some new string. But, it's a NEW string. The original string Tanish is still there, somewhere in the pool of strings. Think of it like this: the string that you are creating is immutable. Tanish is immutable, but s is a reference variable. It can refer to anything in the course of its life. So, Tanish and Tanish is a good boy are 2 separate strings, but s now refers to the latter, instead of the former.
StringBuilder is another way of creating strings in Java, and they are mutable. You can change them. So, if Tanish is a StringBuilder, it is vulnerable to every kind of operation (append, insert, delete, etc.).
Now we have the StringBuilder sb, which is same as the String s.
I've used a StringBuilder built-in method, i.e. indexOf(). This methods finds the index of the character I'm looking for. Once I have the index, I delete the character at that index.
Remember, StringBuilder is mutable. And that's the reason I can delete the characters.
indexOf is overloaded to accept 2 arguments (sb.indexOf(substr ,index)). This returns you the position of the first occurrence of string within the sb, starting from index.
In the example string, sb.indexOf(a,1) will give me 4. All I'm trying to say to Java is, "Return me the index of 'a', but start looking for 'a' from index 1'. So, this way I've the very first a at 0, which I don't want to get rid of.
Now all I'm doing inside the for loop is extracting the character at ith position. j represents the position from where to start looking for the extracted character. This is important, so that we don't loose the one character we need. K represents the result of indexOf('a',j), i.e. the first occurrence of a, after index j.
That's pretty much it. Now, as long as we have a character ch lying in the string (indexOf(....) returns -1, if it can't find the specified character (...or the string as i specified before) as a duplicate, we will obtain it's position (k), delete it using deleteCharAt(k) and update j to k. i.e., the next duplicate a (if it exists) will appear after k, where it was last found.
DEMONSTRATION:
In the example I took, let's say we want to get rid of duplicate cs.
So, we will start looking for the first c after the very first c, i.e. index 3.
sb.indexOf("c",3) will give us 7, where a c is lying. so, k = 7. delete it, and then set j to k. Now, j = 7. Basically after deleting the character, the succeeding string shifts to left by 1. So, now at 7th pos we have d, which was at 8 before. Now, k = indexOf("c",7) and repeat the entire cycle. Also, remember that indexOf("c",j) will start looking right from j. which means if c, is found at j, it will return j. That's why when we extracted the first character, we started looking from position 1 after the character's position.
public class Duplicates {
public static void main(String[] args) {
String str="aabbccddeeff";
String[] str1 = str.split("");
ArrayList<String> List = new ArrayList<String>
Arrays.asList(str1);
List<String> newStr = List.stream().distinct().collect(Collectors.toList());
System.out.print(newStr);
}
}

Finding characters in a string

i'm doing an encoding program where i'm supposed to delete every character in the string which appears twice. i've tried to traverse through the string but it hasn't worked. does anyone know how to do this? Thanks.
public static String encodeScrambledAlphabet(String str)
{
String newword = str;
String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
newword += alphabet;
newword = newword.toUpperCase();
for (int i = 0, j = newword.length(); i < newword.length() && j >=0; i++,j--)
{
char one = newword.charAt(i);
char two = newword.charAt(j);
if (one == two)
{
newword = newword.replace(one, ' ');
}
}
newword = newword.replaceAll(" ", "");
return newword;
}
Assuming that you would like to keep only the first occurrence of the character, you can do this:
boolean seen[65536];
StringBuilder res = new StringBuilder();
str = str.toUpperCase();
for (char c : str.toCharArray()) {
if (!seen[c]) res.append(c);
seen[c] = true;
}
return res.toString();
The seen array contains flags, one per character, indicating that we've seen this character already. If your characters are all ASCII, you can shrink the seen array to 128.
Assuming by saying deleting characters that appears twice, you mean AAABB becomes AAA, below code should work for you.
static String removeDuplicate(String s) {
StringBuilder newString = new StringBuilder();
for (int i = 0; i < s.length(); i++) {
String s1 = s.substring(i, i + 1);
// We need deep copy of original String.
String s2 = new String(s);
// Difference in size in two Strings gives you the number of
// occurences of that character.
if(s.length() - s2.replaceAll(s1, "").length() != 2)
newString.append(s1);
}
return newString.toString();
}
Efficiency of this code is arguable :) It might be better approach to count the number of occurences of character by a loop.
So, from the code that you've shown, it looks like you aren't comparing every character in the string. You are comparing the first and last, then the second and next to last. Example:
Here's your string:
THISISTHESTRINGSTRINGABCDEFGHIJKLMNOPQRSTUVWXYZ
First iteration, you will be comparing the T at the beginning, and the Z at the end.
Second iteration, you will be comparing the H and the Y.
Third: I and X
etc.
So the T a the beginning never gets compared to the rest of the characters.
I think a better way to do this would be to to do a double for loop:
int length = newword.length(); // This way the number of iterations doesn't change
for(i = 0; i < length; i++){
for(j = 0; j < length; j++){
if(i!=j){
if(newword.charAt(i) == newword.charAt(j)){
newword.replace(newword.charAt(i), ' ');
}
}
}
}
I'm sure that's not the most efficient algorithm for it, but it should get it done.
EDIT: Added an if statement in the middle, to handle i==j case.
EDIT AGAIN: Here's an almost identical post: function to remove duplicate characters in a string

Categories