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 7 of 7

Thread: Removing redundancy from String

  1. #1
    Junior Member
    Join Date
    Jun 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Removing redundancy from String

    Hello all,

    I'm trying to eliminate the redundancy from this string using charAt. But can't quite nail it right. I'm trying to start with the first number in the string and cycle through all the others to make sure it's unique, and then if it is, write it out. Can you please take look and comment?

    import java.io.*;
    class charAt{
        public static void main(String[] args) {
    String tf = "111111123422256";        
     
    for (int i=0;i<(tf.length()-1);i++){
     
    for (int z=0;z<(tf.length()-i);z++){
     
    char c = tf.charAt(i); char k = tf.charAt(i+z);
     
    		if(c==k){}
     
    		else{
     
     
    System.out.println(c);}}System.out.println(tf.charAt(tf.length()-1));
     
    }}}
    I realize this is off. The output should look like: 123456.

    Please help. Thanks.


  2. #2
    Member angstrem's Avatar
    Join Date
    Mar 2013
    Location
    Ukraine
    Posts
    200
    My Mood
    Happy
    Thanks
    9
    Thanked 31 Times in 29 Posts

    Default Re: Removing redundancy from String

    What happens if i + z > tf.length?

  3. #3
    Junior Member
    Join Date
    Jun 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Removing redundancy from String

    fourteen 6s

  4. #4
    Member angstrem's Avatar
    Join Date
    Mar 2013
    Location
    Ukraine
    Posts
    200
    My Mood
    Happy
    Thanks
    9
    Thanked 31 Times in 29 Posts

    Default Re: Removing redundancy from String

    Why not to follow the following algorithm:
    1. For a given character, check all the substring before this character
    2. If the substring does not contain this character - print it
    3. Repeat (1) - (2) for each character of the string

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

    pbrockway2 (June 18th, 2013)

  6. #5
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    939
    Thanks
    6
    Thanked 202 Times in 178 Posts

    Default Re: Removing redundancy from String

    I agree with angstrem. And, imho, some such "recipe" (algorithm) is the place to start rather than code.

  7. #6
    Junior Member
    Join Date
    Jun 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Removing redundancy from String

    I'm trying this now but still no luck:

    import java.io.*;
    class charAt{
        public static void main(String[] args) {
    String tf = "111111123422256";        
     
    for (int i=0;i<(tf.length());i++){
     
    for (int k=0;k<i;k++){
     
    if(tf.charAt(i)==tf.charAt(i-k)){}
     
    		else{
     
    System.out.println(tf.charAt(i));
     
     
    }}}}}

  8. #7
    Member angstrem's Avatar
    Join Date
    Mar 2013
    Location
    Ukraine
    Posts
    200
    My Mood
    Happy
    Thanks
    9
    Thanked 31 Times in 29 Posts

    Default Re: Removing redundancy from String

    Say, you have string "12345". Say, i = 4 (that is, tf.charAt(i) == '5').
    Now look at the code that checks all the substring:
    for (int k=0;k<i;k++)
      if(tf.charAt(i)!=tf.charAt(k)) System.out.println(tf.charAt(i));
    I've simplified it a bit to make it more readable, but this is essentially the same thing.
    We have a loop. It checks each number before current. If it is distinct, it prints current number. In our case, all 4 numbers are distinct from '5'. Hence, '5' will be printed 4 times. Not something you want.

Similar Threads

  1. Best way of adding and removing?
    By J-moges in forum Java Theory & Questions
    Replies: 3
    Last Post: May 23rd, 2013, 01:43 PM
  2. Help with removing objects
    By Seacats in forum Object Oriented Programming
    Replies: 4
    Last Post: May 2nd, 2013, 08:32 AM
  3. Replies: 2
    Last Post: March 28th, 2013, 09:54 AM
  4. Replies: 1
    Last Post: April 19th, 2012, 02:46 AM
  5. Removing duplicates from an Array
    By Rizza in forum Collections and Generics
    Replies: 1
    Last Post: February 21st, 2012, 05:38 PM

Tags for this Thread