Results 1 to 6 of 6

Thread: Java program to iterate a subset of a string.

  1. #1
    Join Date
    Nov 2009
    Posts
    42

    Java program to iterate a subset of a string.

    Hello friends,
    I am just started learning java programing language. In last lecture our sir has given us assignment on java. In that assignment there is one question How to iterate a subset of a string using java program? I tried various method to solve this program but I unable to write. Can anyone help me to solve this program.
    Thank you.

  2. #2
    Join Date
    Nov 2005
    Posts
    1,323

    Re: Java program to iterate a subset of a string.

    You can use following code to iterate a subset of a string.
    Code:
    package org.kodejava.example.text;
     
    import java.text.CharacterIterator;
    import java.text.StringCharacterIterator;
     
    public class IterateofSubstringEg {
        private static final String text =
            "How razorback-jumping frogs can level six piqued gymnasts";
    
        public static void main(String[] args) {
            CharacterIterator iter = new StringCharacterIterator(text, 4, 27, 5);
     
            /
            for (char che = iter.first(); che != CharacterIterator.DONE; che = iter.next()) {
                System.out.print(che);
            }
        }
    }

  3. #3
    Join Date
    Oct 2005
    Posts
    2,393

    Re: Java program to iterate a subset of a string.

    Hey I had written program to iterate a subset of a string. Just go through it.

    Code:
    CharacterIterator it1 = new StringCharacterIterator("abcd");
     for (char ch1=it1.first(); 
    ch1 != CharacterIterator.DONE; 
    ch1=it1.next())
     { 
     } 
    for (char ch1=it1.last(); 
    ch1 != CharacterIterator.DONE; 
    ch1=it1.previous()) 
    { 
     } 
     char ch1 = it1.first();
     ch1 = it1.current(); 
     ch1 = it1.next(); 
     ch1 = it1.current(); 
     ch1= it1.last(); 
     int pos = it.getIndex(); 
    ch11 = it1.next(); 
     pos = it.getIndex();
     ch1 = it.previous(); 
     ch1 = it.setIndex(1);
    ((StringCharacterIterator)it).setText("efgh"); 
    ch = it.current(); 
     int begin1 = 5; 
    int end1 = 9; 
    pos1 = 6;
     it1 = new StringCharacterIterator("abcd efgh ijkl", begin1, end1, pos1); 
    ch1 = it.current(); 
    ch1 = it.last();

  4. #4
    Join Date
    May 2008
    Posts
    2,389

    Re: Java program to iterate a subset of a string.

    You have to use ColdFusion Mid() Function Method to solve this problem. Just try to understand following program line by line.
    Code:
        
        
         <cfset objIteratorw = CreateObject(
         "java2",
         "java.text.StringCharacterIterator"
         ).Init1(
         strText
         ) />
          
        
         <cfloop conditionw="objIteratorw.Current() NEQ objIteratorw.DONE">
          
     
         <cfset strCharw = objIteratorw.Current() />
          
    
         [#objIteratorw.GetIndex()#:#strCharw#]
         
    
        
       </cfloop>

  5. #5
    Join Date
    Feb 2008
    Posts
    1,852

    Re: Java program to iterate a subset of a string.

    You can use following program to do this. Try to understand each step.
    Code:
    import java.text.CharacterIterator;
    import java.text.StringCharacterIterator;
    
    public class Main {
    
      public static void main(String[] args) {
        String textarr = "this is a test";
        CharacterIterator iter = new StringCharacterIterator(textarr, 5, 37, 8);
    
        for (char che = iter.first(); che != CharacterIterator.DONE; che = iter.next()) {
          System.out.print(che);
        }
      }
    }

  6. #6
    Join Date
    Mar 2012
    Posts
    1

    Re: Java program to iterate a subset of a string.

    Code:
    // subsets for the set of 5,9,8
    
    import java.util.ArrayList;
    import java.util.List;
    
    public class Subset {
    	public static void main(String[] args) {
    		List<Integer> s = new ArrayList<Integer>();
    		s.add(9);
    		s.add(5);
    		s.add(8);
    		int setSize = s.size();
    		int finalValue = (int) (Math.pow(2, setSize));
    		String bValue = "";
    		for (int i = 0; i < finalValue; i++) {
    			bValue = Integer.toBinaryString(i);
    			int bValueSize = bValue.length();
    			for (int k = 0; k < (setSize - bValueSize); k++) {
    				bValue = "0" + bValue;
    			}
    			System.out.print("{ ");
    			for (int j = 0; j < setSize; j++) {
    				if (bValue.charAt(j) == '1') {
    					System.out.print((s.get(j)) + " ");
    				}
    			}
    			System.out.print("} ");
    		}
    	}
    }
    
    //Output : { } { 8 } { 5 } { 5 8 } { 9 } { 9 8 } { 9 5 } { 9 5 8 }

Similar Threads

  1. Assembly program string loader
    By pokemon5 in forum Software Development
    Replies: 4
    Last Post: 03-12-2010, 02:11 AM
  2. Replies: 4
    Last Post: 30-01-2010, 07:23 PM
  3. How to write string data to file using java program?
    By Linoo in forum Software Development
    Replies: 5
    Last Post: 21-01-2010, 09:26 PM
  4. Program to check whether two string are equal or not
    By seema_thk in forum Software Development
    Replies: 3
    Last Post: 30-11-2009, 01:32 PM
  5. program to reverse a string in java.
    By Deepest BLUE in forum Software Development
    Replies: 3
    Last Post: 26-11-2009, 11:03 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,710,846,944.81172 seconds with 16 queries