944,113 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 366
  • Java RSS
Oct 22nd, 2009
0

for loop help?

Expand Post »
I have this code:
Java Syntax (Toggle Plain Text)
  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.
Java Syntax (Toggle Plain Text)
  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?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
dhanh90 is offline Offline
23 posts
since Oct 2009
Oct 22nd, 2009
0
Re: for loop help?
Second code:
Line 19: o ???
Line 21: j ???

Read compile errors.
Last edited by quuba; Oct 22nd, 2009 at 10:56 am.
Reputation Points: 123
Solved Threads: 106
Posting Pro
quuba is offline Offline
573 posts
since Nov 2008
Oct 22nd, 2009
0
Re: for loop help?
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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
dhanh90 is offline Offline
23 posts
since Oct 2009
Oct 22nd, 2009
1
Re: for loop help?
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.
Reputation Points: 123
Solved Threads: 106
Posting Pro
quuba is offline Offline
573 posts
since Nov 2008
Oct 22nd, 2009
0
Re: for loop help?
Oh.i understand now.Thanks for your help.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
dhanh90 is offline Offline
23 posts
since Oct 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: first ever program using more than one method
Next Thread in Java Forum Timeline: Out of bounds and I don't know why





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC