Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Loop Character Replacement Issue

  1. #1
    Junior Member
    Join Date
    Sep 2014
    Posts
    18
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Loop Character Replacement Issue

    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;
    	}
     
    }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 697 Times in 679 Posts

    Default Re: Loop Character Replacement Issue

    An interesting question that will require you to see the code differently in order to correct it. To help you make that change in perspective, answer the question, "How does the method removeWhiteSpaces() change the input String object, text?"

    Not necessary, but it would enable a better design, are you allowed to use StringBuilder?

  3. #3
    Junior Member
    Join Date
    Sep 2014
    Posts
    18
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Loop Character Replacement Issue

    Okay so is the problem that when it checks and replaces the value that it is not saving the value after? If so I'm a little confused on how to change it.

    The only restrictions are that I have to use loops and the specific method public static String removeWhitespaces(String text)

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 697 Times in 679 Posts

    Default Re: Loop Character Replacement Issue

    is the problem that when it checks and replaces the value that it is not saving the value
    Pretty much. The only 'value' being changed is ch, and that has no effect on the original String object. So the answer to my question is, "It doesn't."

    Another way:

    Since Strings are immutable, "changing" a String object actually creates a new one. On the other hand, a StringBuilder can be modified and when complete can be easily converted to a String object using the toString() method.

    With that in mind, another possible algorithm is to use a loop that adds each desirable character from the original String object to a StringBuilder object, skipping those that aren't wanted.

  5. The Following User Says Thank You to GregBrannon For This Useful Post:

    TheMatrixMage (October 7th, 2014)

  6. #5

    Default Re: Loop Character Replacement Issue

    There are a few ways to do it. You can convert your String to an array of characters - String#toCharArray. And then you need to iterate over the array and replace char with new value.

    char[] chars = "lorem ipsum sit amet".toCharArray();
    char newValue = 'X';
    for (int i = 0; i < chars.length; i++) {
    if (chars[i] == 'm') {
    chars[i] = newValue;
    }
    }
    System.out.println(new String (chars)); // loreX ipsuX sit aXet

Similar Threads

  1. [SOLVED] Infinite loop issue
    By Sharmeen in forum Loops & Control Statements
    Replies: 1
    Last Post: October 20th, 2012, 12:18 PM
  2. While Loop Issue
    By greystreet34 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 30th, 2012, 09:55 PM
  3. [SOLVED] JTextArea font/character count issue
    By psychobeagle12 in forum AWT / Java Swing
    Replies: 7
    Last Post: April 25th, 2012, 06:17 PM
  4. [SOLVED] issue with Character.isUpperCase class
    By suxen in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 24th, 2011, 09:30 PM
  5. Basic loop issue
    By Nismoz3255 in forum Loops & Control Statements
    Replies: 3
    Last Post: February 23rd, 2011, 04:10 PM