|
-
September 24th, 2009, 03:10 AM
#1
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.
-
September 24th, 2009, 04:19 AM
#2
Re: for loop
 Originally Posted by TDX
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.
-
September 24th, 2009, 04:33 AM
#3
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.
-
September 24th, 2009, 04:44 AM
#4
Re: for loop
First, use [CODE]...[/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 [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.
-
September 24th, 2009, 05:20 AM
#5
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();
}
}
-
September 24th, 2009, 06:42 AM
#6
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 [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.
-
September 25th, 2009, 12:08 AM
#7
-
September 25th, 2009, 03:19 AM
#8
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.
-
September 25th, 2009, 03:36 AM
#9
Re: for loop
 Originally Posted by TDX
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 
-
September 25th, 2009, 04:28 AM
#10
Re: for loop
 Originally Posted by TDX
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 [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.
-
September 25th, 2009, 05:47 AM
#11
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|