Hello, I'm just learning programming and in my Computer Science class have been told to use loops to perform several operations. However I am currently having issues with replacing all white spaces with nothing. Below is my full program and the method I am having this issue with is removeWhitespaces. Any help is greatly appreciated, I just can't figure out what I'm doing wrong as all the console outputs for that portion is the original string I type in.
import java.util.Scanner; public class SentenceAnalysis { public static int countWhitespaces(String sentence){ boolean isDigit = true; int timesSpace = 0; char ch; for(int i = 0; i < sentence.length(); i++){ ch = sentence.charAt(i); if (ch == ' ') timesSpace++; } return timesSpace; } public static String removeWhitespaces(String text){ char ch; for(int i = 0; i < text.length(); i++){ ch = text.charAt(i); if (ch == ' '){ ch = '\0'; } if (ch == '.'){ ch = '\0'; } if (ch == ','){ ch = '\0'; } } return text; } public static void main(String[] args) { int n = 0; Scanner input = new Scanner(System.in); String sentence = input.nextLine(); int result = countWhitespaces(sentence); System.out.println("Number of spaces: " + result); boolean digits = isNumeric(sentence); System.out.println("Has only digits: " + digits); String text = removeWhitespaces(sentence); System.out.println(text); } public static boolean isNumeric(String sentence){ char ch; int timesDigit = 0; for(int i = 0; i < sentence.length(); i++){ ch = sentence.charAt(i); boolean isDigit = (ch=='0') || (ch=='1') || (ch=='2') || (ch=='3') || (ch=='4') || (ch=='5') || (ch=='6') || (ch=='7') || (ch=='8') || (ch=='9'); if (isDigit == true) timesDigit++; } if (timesDigit < sentence.length()) return false; else return true; } }