CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11

Thread: for loop

  1. #1
    Join Date
    Sep 2009
    Posts
    9

    for loop

    Hi all
    I have an input sentence as follows:-
    Ask your Java programming question and help out others with theirs.

    and generate the following subsentences using for loop.

    Ask your
    Ask your Java
    Ask your Java programming
    Ask your Java programming question
    Ask your Java programming question and
    your Java
    your Java programming
    your Java programming question
    your Java programming question and
    your Java programming question and help
    Java programming
    Java programming question
    Java programming question and
    Java programming question and help
    Java programming question and help out
    programming question
    programming question and
    programming question and help
    ....
    ....

    can anyone help me providing code using for loop in Java? Thanks in advance.

  2. #2
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: for loop

    Quote Originally Posted by TDX View Post
    can anyone help me providing code using for loop in Java?
    Sure, what are you having trouble with?

    Optimism is an occupational hazard of programming: testing is the treatment...
    K. Beck
    Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  3. #3
    Join Date
    Sep 2009
    Posts
    9

    Re: for loop

    Please correct from my code:


    String[] choppedUpString = sentence.trim().split(" ");
    String tempString="";
    for (int i=0; i< choppedUpString.length ; i++)
    for(int j=2; j<5; j++){
    for (int k=i; k<j; k++)
    tempString= tempString+" "+choppedUpString[k];
    System.out.println(tempString);
    }

    I am unable to get the desired output.

  4. #4
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: for loop

    First, use &#91;CODE]...&#91;/CODE] tags when posting code, so it is readable. Second, if you say that you can't get the desired output, that implies you were able to run the program. The code you posted is incomplete, and won't even compile, so please post the actual code that you ran.

    Time is an excellent teacher; but eventually it kills all its students...
    Anon.
    Please use &#91;CODE]...your code here...&#91;/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  5. #5
    Join Date
    Sep 2009
    Posts
    9

    Re: for loop

    Here is the code:-


    Code:
    import java.io.BufferedReader;
    import java.io.FileReader;
    import java.io.IOException;
    import java.io.FileNotFoundException;
    
    public class codeguru{
        
        public codeguru(){
        }    
        
        public void group() throws FileNotFoundException, IOException{
            BufferedReader br=new BufferedReader(new FileReader("Input_sentences.txt"));
    	String sentence=null;    
            String tempSen=null;   
            while((sentence=br.readLine())!=null){
                String[] choppedUpString = sentence.trim().split(" ");           
    	    String tempString="";
    	    for (int i=0; i< choppedUpString.length ; i++)
    		for(int j=2; j<=6; j++){	
    	     	for (int k=i; k<j; k++)
    			tempString= tempString+" "+choppedUpString[k];			
    		System.out.println(tempString);
              	}
            }         
       } 
        public static void main(String[] args) throws IOException{
            // TODO code application logic here
            codeguru m=new codeguru();
            m.group();
        }
        }

  6. #6
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: for loop

    OK, your first problem is that you're not clearing the tempString after printing each line.

    Once you have the first set of lines printing correctly, you need to change the code so that it repeats one word further on for each set of lines. You can do this with a 'start' counter set to zero at the start (before any loops) and incremented after each set of lines is finished.

    Then you can set the inner loop counter 'k' to this 'start' value so it starts one word further in for every set of lines. You will also have to change the end condition so it ends one word further on than before, so it will be:

    for (int k = start; k < start + j; ...

    You also need to stop 'k' going beyond the end of the choppedUpString array (wouldn't 'words' be a better name?), but I'll leave that up to you.

    Plan to throw one away; you will anyhow...
    F. Brooks
    Please use &#91;CODE]...your code here...&#91;/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  7. #7
    Join Date
    Sep 2009
    Posts
    9

    Re: for loop

    Yes it worked.

  8. #8
    Join Date
    Sep 2009
    Posts
    9

    Re: for loop

    Hi

    The modified code is

    Code:
    import java.io.BufferedReader;
    import java.io.FileReader;
    import java.io.IOException;
    import java.io.FileNotFoundException;
    
    public class codeguru{
    
        public codeguru(){
        }
    
        public void group() throws FileNotFoundException, IOException{
            BufferedReader br=new BufferedReader(new FileReader("Input_sentences.txt"));
            String sentence="";
            String tempSen="";
            while((sentence=br.readLine())!=null){
                String[] choppedUpString = sentence.trim().split(" ");
                String tempString="";
                int start=0;
                for (int i=0; i< choppedUpString.length ; i++){
                    for(int j=2; j<6; j++){
                    for (int k=start; k<start+j; k++)
                            tempString= tempString+" "+choppedUpString[k];
                    System.out.println(tempString);
                    tempString="";
                    }
                    start++;
                    }
            }
       }
        public static void main(String[] args) throws IOException{
            // TODO code application logic here
            codeguru m=new codeguru();
            m.group();
        }
    The program gives the output.
    But at the end of the output the following message comes:

    Code:
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 13
    	at codeguru.group(codeguru.java:22)
    	at codeguru.main(codeguru.java:33)
    What could be the problem and how to correct it?
    Last edited by TDX; September 25th, 2009 at 03:24 AM.

  9. #9
    Join Date
    Apr 2009
    Location
    TR
    Posts
    97

    Re: for loop

    Quote Originally Posted by TDX View Post
    But at the end of the output the following message comes:

    Code:
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 13
        at codeguru.group(codeguru.java:22)
        at codeguru.main(codeguru.java:33)
    What could be the problem and how to correct it?
    ArrayIndexOutOfBoundsException..this exception appears when u want to reach an illegal index of array..review your code..exception message already tells u where it appears..
    Last edited by holestary; September 25th, 2009 at 04:02 AM.

    Please use code tags like this :
    [SIGPIC][/SIGPIC]
    Code Your Dreams

  10. #10
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: for loop

    Quote Originally Posted by TDX View Post
    But at the end of the output the following message comes:

    Code:
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 13
    	at codeguru.group(codeguru.java:22)
    	at codeguru.main(codeguru.java:33)
    What could be the problem and how to correct it?
    I told you in my last post:
    You also need to stop 'k' going beyond the end of the choppedUpString array
    You can correct it by adding an extra check on 'k' to your loop condition, or by checking 'k' inside the loop (before it is used to index the array) and exiting the loop if it is too large.

    Good judgment comes from experience; experience comes from bad judgment...
    F. Brooks
    Please use &#91;CODE]...your code here...&#91;/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  11. #11
    Join Date
    Sep 2009
    Posts
    9

    Re: for loop

    Yes, it's done

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured