for loop help?

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Oct 2009
Posts: 15
Reputation: dhanh90 is an unknown quantity at this point 
Solved Threads: 0
dhanh90 dhanh90 is offline Offline
Newbie Poster

for loop help?

 
0
  #1
Oct 22nd, 2009
I have this code:
  1. /*
  2.  
  3. package lab;
  4.  
  5.  
  6. public class Main {
  7.  
  8.  
  9.   public static void main(String[] args) {
  10.   int t=0;
  11.   int h=0;
  12.   int w=0;
  13.   String Name="http://hcmiu.edu.vn/mail/cuem";
  14.   char[] ch=new char[Name.length()];
  15.   for(int i=0;i<Name.length();i++){
  16.   ch[i]=Name.charAt(i);
  17.   for(int j=0;j<Name.length();j++){
  18.   if((ch[j]=='/')&&(ch[j+1]=='/')&&(Character.isLetter(ch[j+2]))){
  19.   t=j+2;
  20.   System.out.println(t);
  21.   h=Name.indexOf('/', j+3);
  22.  
  23.   }
  24.  
  25.   }
  26.  
  27.   }
  28.   System.out.println(Name.subSequence(t, h));
  29.   }
  30.  
  31. }
  32.  
This code run correctly.But when i dont use the second for loop.i couldnt work.I dont know? plz give me the answer.
  1. /*
  2.  
  3.  
  4. package lab;
  5.  
  6. public class Main {
  7.  
  8.  
  9.   public static void main(String[] args) {
  10.   int t=0;
  11.   int h=0;
  12.   int w=0;
  13.   String Name="http://hcmiu.edu.vn/mail/cuem";
  14.   char[] ch=new char[Name.length()];
  15.   for(int i=0;i<Name.length();i++){
  16.   ch[i]=Name.charAt(i);
  17.  
  18.   if((ch[i]=='/')&&(ch[i+1]=='/')&&(Character.isLetter(ch[i+2]))){
  19.   t=o+2;
  20.   System.out.println(t);
  21.   h=Name.indexOf('/', j+3);
  22.  
  23.   }
  24.  
  25.  
  26.   }
  27.   System.out.println(Name.subSequence(t, h));
  28.  
  29.   }
  30.  
  31. }
  32.  
Why do i need the second for loop?
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 332
Reputation: quuba is on a distinguished road 
Solved Threads: 54
quuba quuba is offline Offline
Posting Whiz
 
0
  #2
Oct 22nd, 2009
Second code:
Line 19: o ???
Line 21: j ???

Read compile errors.
Last edited by quuba; Oct 22nd, 2009 at 10:56 am.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 15
Reputation: dhanh90 is an unknown quantity at this point 
Solved Threads: 0
dhanh90 dhanh90 is offline Offline
Newbie Poster
 
0
  #3
Oct 22nd, 2009
line 19: 0=j
line 21: replace j =i
sorry i type incorrectly.
So now,could you run and show me why its wrong.thanks
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 332
Reputation: quuba is on a distinguished road 
Solved Threads: 54
quuba quuba is offline Offline
Posting Whiz
 
1
  #4
Oct 22nd, 2009
In one loop
for(int i=0;i<Name.length();i++){
ch[i] = Name.charAt(i);
.....
}
for current i , are only defined values ch[0]......ch[i-2]..ch[i-1]...ch[i]
but no ch[i+1] or ch[i+2] .They will set in the the future course of the loop.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 15
Reputation: dhanh90 is an unknown quantity at this point 
Solved Threads: 0
dhanh90 dhanh90 is offline Offline
Newbie Poster
 
0
  #5
Oct 22nd, 2009
Oh.i understand now.Thanks for your help.
Reply With Quote Quick reply to this message  
Reply

Message:




Views: 206 | Replies: 4
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC