Program run halfway in CMD and pause.. how?
Hi All
I got a program that run halfway and it just pause without error msg
I realise that it always stop at the same area (before a for loop)
I tried to put a try.. catch statement at the 'for' loop but to no avail
btw my program can work in the ide but not at the Dos
Pls Help~! Thank you.
while(tk.hasMoreTokens()){
ric = tk.nextToken();
foundMatch =false;
System.out.println("where's the exception output? ");
}
System.out.println ("Print if it run to this portion");
try{
for(int i=0; i<letter.length;i++){
System.out.println ("finally in for loop");
if(ric.substring(0,1).equals(letter[i])){
foundMatch =true;
System.out.println("test2");
break;
}
}
}catch (Exception e){System.out.println("e.Message" + e.toString());}
>
[1563 byte] By [
peachteaa] at [2007-11-27 4:04:04]

isn't your for loop supposed to be INTO the while loop?since in your for loop you work on "ric", i assume you want to work on all the tokens, not only the last oneplus: if the for loop is never entered, it could mean the letter array is just 0 in length
You may want to quote the output you get from running your program?
output is
hi
hi2
hi3
hi4
hi5
wait for 2 seconds
test
*All these r ok cos i keep putting System.out.println to find out where it got paused*
Then after the word "test"
it will looping these 2 outputs in a infinite loop:
"Where's the exception message?"
"Print if it run this portion"
and i tried putting try... catch at the area where it seems to pause but no exception msg is thrown so i really dunno what else i can do
That tells us something.
The following statement is not in a loop in the piece of code you've given us:
System.out.println ("Print if it run to this portion");
You tell us the output from this line is repeated forever. If I'm not mistaken, your endless loop is somewhere else in your program, at keeps calling the piece of code you've posted (or the loop is simply around that piece of code).
It further seems that you get 1 token from what I guess is a tokenizer, and that letter is empty (has length 0), as a previous poster suggested, since you never enter the for loop.
you may just have misplaced some { and }
> That tells us something.
>
> The following statement is not in a loop in the piece
> of code you've given us:
>
> System.out.println ("Print if it run to this
> portion");
>
> You tell us the output from this line is repeated
> forever. If I'm not mistaken, your endless loop is
> somewhere else in your program, at keeps calling the
> piece of code you've posted (or the loop is simply
> around that piece of code).
>
> It further seems that you get 1 token from what I
> guess is a tokenizer, and that letter is empty (has
> length 0), as a previous poster suggested, since you
> never enter the for loop.
the "letter" is an array. Interestingly, it doesn't keep looping this way when i run it in the ide. BTW, i have another for loop that make use of the "letter" but it does go into that loop.
And this other for loopis before the one that i have posted earlier
hmm i try to scan my codes once more
I think the previous poster's suggestion about re-arranging the loops is correct. Try something like this.
import java.util.StringTokenizer;
public class st {
public static void main(String[] args) {
boolean foundMatch = false;
String test_s = "hi,hi2,hi3,hi4,hi5,test";
String s = null;
char[] letters = {'a','b','c','d','e','f','g','h'};
StringTokenizer st = new StringTokenizer(test_s, ",");
while( st.hasMoreTokens() && !foundMatch ) {
s = st.nextToken();
// check for match
char firstLetter = s.charAt(0);
for( int i=0; i<letters.length; i++ ) {
if( firstLetter == letters[i] ) {
foundMatch = true;
break;
}
}
}
if( foundMatch)
System.out.println( "match: " + s );
else
System.out.println( "no match was found" );
}
}
a more concise 5.0 version follows
public class st2 {
public static void main(String[] args) {
boolean foundMatch = false;
String test_s = "hi,hi2,hi3,hi4,hi5,test";
String match_s = null;
String[] strings = test_s.split(",");
for( String s : strings ) {
if( s.matches("^[a-h].*") ) {
match_s = s;
foundMatch = true;
break;
}
}
if( foundMatch)
System.out.println( "match: " + match_s );
else
System.out.println( "no match was found" );
}
}
>
woah... thanks~! I just swop the order of the while and for loop and it can continue runninghowever it cannot insert the data into the db but nvm tt is a new problem (which is the last 20% of the code)but anyway thanks guys for your help*cheers*