0

im novice in java,i have condition with while and it work nice but i need using looping with for:this while condition for looping

String command = "";
while ((command = br.readLine())!=null && !command.isEmpty()) {
  int b=0; 
  thisObj.perintah(b,command);
}

i have try write with for,i think similar like this but its not work

for (int b=0;b<command;b++)
   {
   String command = br.readLine();
   thisObj.perintah(b,command);
   }

did anyone know what i missing

pound Pound
  • 123
  • 3
  • 12
  • 2
    What do you think that an inequality comparison between an integer and a string should do? – Phoshi Oct 09 '13 at 15:05
  • You are comparing the String command to the int b. That is not a good idea. – luanjot Oct 09 '13 at 15:05
  • 1
    What's the point of `b` in your first snippet? – arshajii Oct 09 '13 at 15:05
  • What you're asking is really **unclear**. `command` doesn't seem to be a number (given [your other post](http://stackoverflow.com/questions/19272122/stop-looping-if-method-read-next-line-empty)) and there is no way for us to guess what the purpose of `b` is and thus what value should it take. – zakinster Oct 09 '13 at 15:29
  • In your `for` loop, the symbol `command` is not defined until you enter the loop body. You cannot use it in the `for` statement itself. Even if you could, the `<` operator is not defined between an `int` and a `String` value in Java. – Ted Hopp Oct 09 '13 at 15:35

3 Answers3

1

The while loop expressed as a for loop:

int b = 0;
for (String command = br.readLine(); command !=null && !command.isEmpty(); command = br.readLine()) {
  thisObj.perintah(b++, command);
}

Using the variable name command makes the for line quite long, so here's the same code with a shorter variable name so it's clearer what's going on:

int b = 0;
for (String s = br.readLine(); s !=null && !s.isEmpty(); s = br.readLine()) {
  thisObj.perintah(b++, s);
}
Bohemian
  • 412,405
  • 93
  • 575
  • 722
  • This is a `for each` loop.. It is definitely more appropriate but perhaps you can include a classic `for` loop too? Cover all the bases! – christopher Oct 09 '13 at 15:07
  • @Chris this is not a foreach: There is no iterable object here, nor is there the `:` operator of the foreach syntax. This is a standard `for` loop. – Bohemian Oct 09 '13 at 15:11
  • Merlin's beard. So it is. I retract my statement and award you a +1 for a lovely solution. – christopher Oct 09 '13 at 15:11
  • i need an increment because im using b for other method,your code just give 1 output for my other method – pound Pound Oct 09 '13 at 15:21
  • But your while loop doesn't increment `b`, and that was supposed to be the prototype for the for loop. Oh well, I've incremented b... – Bohemian Oct 09 '13 at 15:32
0

It's not clear what value should b take. Either way, you'll have to convert the string into integer though.

String command = "";
for(int b  = 0; (command = br.readLine())!=null && !command.isEmpty(); ++b) {
    thisObj.perintah(b,command);
    String command = br.readLine();
}
zakinster
  • 10,508
  • 1
  • 41
  • 52
0

Java can't compare int with String without some help. You will need to convert the command into a number. Try Integer.parseInt().

But you can't do that in the condition of the for loop. Try this instead:

int b = 0;
String command = "";
while ((command = br.readLine())!=null && !command.isEmpty()) {
  int commandAsInt = Integer.parseInt(command);
  if(b >= commandAsInt) break; // exit the loop

  thisObj.perintah(b,command);
  b++;
}
Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820