Buffered Reader, how to make a loop to terminate

Hello, the code uses each line from the input.txt and does whatever we want, it's termination condition is when the next line is null. But this fails to work if the input.txt doesn't have an emptyline after the last line, how to change the code to work for those files?
   try {
        BufferedReader in = new BufferedReader(new FileReader("input.txt"));
        String str;
        while ((str = in.readLine()) != null) {
            /*WE DO WHATEVER FOR EACH LINE HERE */
        in.close();
    } catch (IOException e) {
    }Edited by: Odinn on Jan 10, 2009 10:18 AM
Edited by: Odinn on Jan 10, 2009 10:19 AM

Odinn wrote:
Nop, say I have an input.txt file that contains
1
2
3
4the code above won't work files that the end line isn't empty. It works for:
1
2
3
4
<assume theres an empty line here>Edited by: Odinn on Jan 10, 2009 10:37 AM
Edited by: Odinn on Jan 10, 2009 10:38 AMAre you sure about that? Did you try to run it? If so, what happened? I just ran your code as is (just modified it to print the lines out) with two input files: One with a blank line at the end, and one without. I got the same exact output, and no error messages.
Odinn wrote:
the code uses each line from the input.txt and does whatever we want, it's termination condition is when the next line is null. But this fails to work if the input.txt doesn't have an emptyline after the last lineI don't think your logic is correct. A blank line at the end of a text file is not the same as a null, and in fact, it does not fail. If you run the code, you will see that you are not in an endless loop.

Similar Messages

  • How to make a loop run for specific period of time ?

    hello !
    i'm new to labview and could'nt found how to make a loop run for 2 sec for example, and then to exit.
    Solved!
    Go to Solution.

    Hi Hookso,
    You can do it using the different loops like While loop and For Loop. Roderic has already stated how to do it using a for loop. I have illustrated in the attached vi how to do it using a while loop and Elapsed Time function. Take a look.
    Regards,
    Nitzz
    (Give kudos to good Answers and Mark it as a Solution if your problem is Solved) 
    Attachments:
    Untitled 2.vi ‏36 KB

  • How to make for loop pass only once in a next() method

    Good Day!
    Can anyone help me or suggest any idea to resolve my problem with the below code, wherein it will only pass the for loop only once. I already tried inserting the for loop in side the if (sqlset4.isFirst()) condition but the problem is it only retrieved the first row of the resultset.
    Cheers!
                   Statement sOutput = consrc.createStatement();
                            ResultSet sqlset4 = sOutput.executeQuery(xquery);
                            ResultSetMetaData rsMetaData = sqlset4.getMetaData();
                            int numberOfColumns = rsMetaData.getColumnCount();
                            String writefld = "";
                            while (sqlset4.next()) {
                                 writefld = "";
                                 for (int i = 1; i <= numberOfColumns; i++) {
                                     if (xxformatid.equals("1") || xxformatid.equals("3")) {
                                         writefld = writefld + "sqlset4.getString(" + i + ").trim()" + "|";
                                writefld = writefld.substring(0, writefld.length() - 1) + ")";
                                output.write("\r\n");
                                output.write(writefld);
                            output.close();I am using Netbean IDE 6.8
    Oracle Database 10g Enterprise Edition Release 10.2.0.3.0 - 64bi

    Hi everyone!
    What I actually trying to do is that I have a multiple tables and from these tables I'm going to write each of it into a flatfile that is a pipe delimeted that is why I have to make a loop to know how many fields I am going to write. The code that was attached are actually working, my only concern is that it will take a longer time of processing cause every record of a table it will pass to the for loop(checking how many column) wherein number of column/ were already known on the first loop.
    Hi kajbj,
    I think what your trying to explain is almost the same with below code which i had already tried. The problem with this is that the every loop of the outer loop data retrieve is only the data of the first record.
                   Statement sOutput = consrc.createStatement();
                            ResultSet sqlset4 = sOutput.executeQuery(xquery);
                            ResultSetMetaData rsMetaData = sqlset4.getMetaData();
                            int numberOfColumns = rsMetaData.getColumnCount();
                            String writefld = "";
                            while (sqlset4.next()) {
                                 writefld = "";
                                 if (sqlset4.isFirst()) {
                                    for (int i = 1; i <= numberOfColumns; i++) {
                                        if (xxformatid.equals("1") || xxformatid.equals("3")) {
                                            writefld = writefld + "sqlset4.getString(" + i + ").trim()" + "|";
                                writefld = writefld.substring(0, writefld.length() - 1) ;
                                output.write("\r\n");
                                output.write(writefld);
                            output.close();

  • How to make a loop that changes two different movieclips from visible to invisible every second

    I am writing some code that puzzles me. It may be, because I have a cold or because I've never needed to do anything like this. I want to have two movieclips one is visible and one is not. then one second passes the one that was visible is invisible and the invisible one is visible. What would be the best approach to this, besides handcoding if statements. I know there has to be a faster way and less expensive way than handcoding if statements.
    I'd like to thank anyone who replies in advance .

    this is how i would do it
    package
              import flash.display.MovieClip;
              import flash.display.Sprite;
              import flash.events.TimerEvent;
              import flash.utils.Timer;
              public class Main extends Sprite
                        private var mc1:MovieClip;
                        private var mc2:MovieClip;
                        public function Main()
                                  // create your mc's here
                                  mc1 = new MovieClip();
                                  mc2 = new MovieClip();
                                  mc2.visible = false;
                                  var timer:Timer = new Timer(1000);
                                  timer.addEventListener(TimerEvent.TIMER, loop);
                                  timer.start();
                        private function loop(e:TimerEvent):void
                                  mc1.visible = !mc1.visible;
                                  mc2.visible = !mc2.visible;

  • DVD STUDIO PRO - How to make chapters loop?

    Hi
    I'm trying to create a DVD and my last problem I'd like to resolve before the final burn is:
    Is there a way to make the Chapters in my Track Loop?
    My scenario: I have 5 Chapters Total. In my Chpater Menu I created 5 Buttons so you can jump straight to 5 Chapters. But let's say for Button 3 (Chapter 3), when I activate/play it, if I hit SKIP FWD I can skip to Chapter 4, & 5, BUT if I hit SKIP BACK it stops at Chapter 3 (which is where I started - Button 3).
    Is there a way for me to set it so that if I hit Button 3-Chapter 3 from the Chapter Menu - I can access Chapter 1&2 by hitting Skip Backwards while the track is playing? The only way I can think to resolve this is to add Chapter 1&2 after Chapter 5 in my Story List but that means I can only hit Skip Forward and it will eventually stop skipping forward once it hits Chapter 2. So is there a way to loop it???
    Sorry I hope I explained it decent enough for you guys to understand
    NEED HELP ASAP!!! Trying to finish for a client this weekend! :

    Yeahhh so it took me giving up and just burning to DVD and playing it in an actual DVD player to realize that the DVD Studio Pro does its "behind-the-scenes" magic and will have that function automatically figured out for the DVD Player. DO NOT rely on the simulation feature...geez...problem solved.

  • How to make animation loop smoothly

    http://dietzclublambs.com/
    The Flash on this site shows scrolling pictures on the home
    page. When it comes to the end, you'll see it stops abruptly and
    restarts at the beginning.
    Any suggestions for a way to make it look like a seamless
    transition from the end back to the beginning and keep the images
    continuously scrolling?

    You need to create a duplicate of the row of images and on
    the last frame, position the images as they are in the first frame.
    You may need to nudge the duplicate slightly to get a smooth
    transition but the general idea is to utilize 2 sets of images so
    that the last and first image seem to be back to back. Hope this
    makes sense.

  • How to make a loop of redirecting?

    I'm writing a servlet that can redirect through sites after an amount of time. For example: yahoo.com then google.com then dell.com... But my knowledge is limited to using setHeader("Refresh", "Time; URL") so it can only redirect for 1 time.
    Can any one give me a help?

    Hi Hookso,
    You can do it using the different loops like While loop and For Loop. Roderic has already stated how to do it using a for loop. I have illustrated in the attached vi how to do it using a while loop and Elapsed Time function. Take a look.
    Regards,
    Nitzz
    (Give kudos to good Answers and Mark it as a Solution if your problem is Solved) 
    Attachments:
    Untitled 2.vi ‏36 KB

  • How to make a switch loop? ( Read inside for more info.)

    I'm working on a project using switch and I want to know how to make it loop/ or is it possible to do so with for or while. If anyone could help me I'd be very thankfull.
    Since my switch is too long I'll make an example:
    System.out.println();
    System.out.println();
    System.out.println("Zgjedhni operacionin: ");
    System.out.println("1. Mbledhja A + B\n");
    System.out.println("2. Zbritja A - B\n");
    optionNumber = sc.nextInt();
    switch ( optionNumber ) {               
                        case 1: System.out.println("\nMbledhja A + B: ");
                              s11 = (a11+b11);
                              s12 = (a12+b12);
                              s13 = (a13+b13);
                              s21 = (a21+b21);
                              s22 = (a22+b22);
                              s23 = (a23+b23);
                              s31 = (a31+b31);
                              s32 = (a32+b32);
                              s33 = (a33+b33);
                              System.out.println( s11 + " - " + s12 + " - " + s13 );
                              System.out.println( s21 + " - " + s22 + " - " + s23 );
                              System.out.println( s31 + " - " + s32 + " - " + s33 );
                              break;
                        case 2: System.out.println("\nZbritja A - B: ");
                              s11 = (a11-b11);
                              s12 = (a12-b12);
                              s13 = (a13-b13);
                              s21 = (a21-b21);
                              s22 = (a22-b22);
                              s23 = (a23-b23);
                              s31 = (a31-b31);
                              s32 = (a32-b32);
                              s33 = (a33-b33);
                              System.out.println( s11 + " - " + s12 + " - " + s13 );
                              System.out.println( s21 + " - " + s22 + " - " + s23 );
                              System.out.println( s31 + " - " + s32 + " - " + s33 );
                              break;I've got the variables declared of course :P so now what I want after doing case 1 or case 2 or any other cases ( 9 in my program) I want to make a question if I want to do another operation and start again from beginning :$/

    Solved it.
    do {
    System.out.println();
    System.out.println();
    System.out.println("Zgjedhni operacionin: ");
    System.out.println("1. Mbledhja A + B\n");
    System.out.println("2. Zbritja A - B\n");
    optionNumber = sc.nextInt();
    switch ( optionNumber ) {               
                        case 1: System.out.println("\nMbledhja A + B: ");
                              s11 = (a11+b11);
                              s12 = (a12+b12);
                              s13 = (a13+b13);
                              s21 = (a21+b21);
                              s22 = (a22+b22);
                              s23 = (a23+b23);
                              s31 = (a31+b31);
                              s32 = (a32+b32);
                              s33 = (a33+b33);
                              System.out.println( s11 + " - " + s12 + " - " + s13 );
                              System.out.println( s21 + " - " + s22 + " - " + s23 );
                              System.out.println( s31 + " - " + s32 + " - " + s33 );
                              break;
                        case 2: System.out.println("\nZbritja A - B: ");
                              s11 = (a11-b11);
                              s12 = (a12-b12);
                              s13 = (a13-b13);
                              s21 = (a21-b21);
                              s22 = (a22-b22);
                              s23 = (a23-b23);
                              s31 = (a31-b31);
                              s32 = (a32-b32);
                              s33 = (a33-b33);
                              System.out.println( s11 + " - " + s12 + " - " + s13 );
                              System.out.println( s21 + " - " + s22 + " - " + s23 );
                              System.out.println( s31 + " - " + s32 + " - " + s33 );
                              break;
    System.out.println("Doni te beni edhe nje operacion?: ");
    vazhdo = sc.next().equalsIgnoreCase("PO");
    while ( vazhdo == true);Sorry for double posting can't see an edit button :$

  • How to make labview program to get average value of 200 reading from multimeter (by using loop)

    Hello
    How to make labview program to get average value of 200 reading from multimeter (I using using loop) to read voltage from mulmeter  but I like to get average value of all of 200 reding how can I do that?
    Thanks
    Wee
    Solved!
    Go to Solution.

    Another idea with less programming - take advantage of the "free" array that comes with a Chart - i.e. the  History Data.
    1) Wire your DMM data to a Chart. (You can set the chart to invisible if you don't plan on using it in the GUI).
    2) Set the Chart History Length to 200 (right click on the chart, click on Chart History Length...)
    3) On the block diagram, use the History Data Property Node, wire it to Mean.vi, and you're done.
    Easiest running average ever.
    Message Edited by Broken Arrow on 04-07-2010 11:36 AM
    Richard
    Attachments:
    EasyAvg.jpg ‏8 KB

  • How do I make apples loops that show up in the loop browser

    How do I make apples loops from my audio files that show up in the loop browser, and where do they reside on the computer so that I know they are there.

    Refer to Logic manual page 848 where it is well explained.
    Regarding the path you can search:
    localdrive\Users\your_ user_account\Library\Audio\Apple Loops\User Loops
    !http://img59.imageshack.us/img59/4967/aglogo45.gif!

  • HOW TO MAKE FIELDS AS READ ONLY IN TABLE MAINTAINANCE

    HI ABAPers,
    HOW TO MAKE SOME FIELDS AS DISPLAY MODE.
    FOR SUPPOSE, THERE ARE 5 FIELDS in SM30.
    AMONG THEM 1 FIELD SHUD BE USED FOR EDITING AND THE OTHER 4 FIELDS SHUD ONLY BE USED FOR DISPLAY PURPOSE
    WHEN EVER I ENTER A VALUE IN THAT 1 PARTICULAR FIELD THEN THE DATA OF THE REMAINING 4 FIELDS SHUD BE DISPLAYED IN THE SAME SCREEN ITSELF.
    HOW CAN IT BE POSSIBLE...
    PLZ HELP.
    THNX IN ADV,
    SP.

    Hello SP,
    You can create a maintenance view for the table & here you can define the maintenance attribute for individual fields.
    For the fields you want to be READ-ONLY pass 'R' to those fields. (It is the 4th column from the left, the one to the immediate left of the Key column).
    Don't change the TMG function group directly. If you regenerate the TMG the changes would be overwritten !
    You can use the Event '01' to fulfill your requirement:
    *&      Form  f_event_01
    *       text
    FORM f_event_01.
      DATA: v_indx TYPE sy-index,
            v_land1 TYPE land1,
            v_name1 TYPE name1.
      FIELD-SYMBOLS: <vendor> TYPE lifnr,
                     <val> TYPE ANY.
      BREAK-POINT.
      "TOTAL contains all data which are read, changed and created in TMG
      LOOP AT total.
        IF <action> = neuer_eintrag " New Entry.
        OR <action> = aendern "Changed entry
        OR <action> = original. "Same as DB
          READ TABLE extract WITH KEY <vim_xtotal_key>.
          IF sy-subrc = 0.
            v_indx = sy-tabix.
          ELSE.
            CLEAR v_indx.
          ENDIF.
          ASSIGN COMPONENT 'LIFNR' OF STRUCTURE total TO <vendor>.
          CHECK sy-subrc = 0.
    *     select data from the LFA1 based on LIFNR
          SELECT SINGLE name1 land1 FROM lfa1
            INTO (v_name1,v_land1)
            WHERE lifnr = <vendor>.
          CHECK sy-subrc = 0.
    *     Populate the hidden fields NAME1 & LAND1
          ASSIGN COMPONENT 'NAME1' OF STRUCTURE total TO <val>.
          CHECK sy-subrc = 0.
          <val> = v_name1.
          ASSIGN COMPONENT 'LAND1' OF STRUCTURE total TO <val>.
          CHECK sy-subrc = 0.
          <val> = v_land1.
          MODIFY total.
          CHECK v_indx GT 0.
          extract = total. "Pass the changes in TOTAL to EXTRACT
          MODIFY extract INDEX v_indx.
        ENDIF.
      ENDLOOP.
      sy-subrc = 0.
    ENDFORM.                                                    "f_event_01
    BR,
    Suhas
    Edited by: Suhas Saha on Aug 16, 2010 11:58 AM

  • Loop in smartform - how to make values valid in all pages

    Hi All.
    I have a smartforms with several pages (9). I must print these 9 pages once for each line in the internal table ITAB. When I put a loop in the first page, the data under that loop is printed correctly, but every text node outside the loop always prints the last line of ITAB.
    Do any of you know how to make the data in the loop valid throughout the other pages?
    any help will be welcome.
    thanks,
    Hermes.

    I got it. The main window must be the first one in the first page. The loop must be inside the main window, and bellow the loop there must be a page-break command.

  • How to make a Thread loop?

    Hi, I was wondering how to make a thread start over again from the beginning once it finishes, if it hasn't been stopped already.

    package src.Utility;
    public class ThreaTest extends Thread{
          * @param args
         public static void main(String[] args) {
              // TODO Auto-generated method stub
              ThreaTest threaTest= new ThreaTest();
              while(true){
                   try{
                   if (!threaTest.isAlive()){
                        threaTest= new ThreaTest();
                        threaTest.start();
                   }//else{
                        //System.out.println("");
                   }catch (Exception e) {
                        e.printStackTrace();
         public void run(){
              System.out.println("-----11---------");
    }try the above code and then tell me if this loop works for you???

  • How to make a button to stop and run a for loop?

    How to make a button to stop and run a for loop?  and if it is stopped it shall start from where i t stopped.

    Your VI has some very fundamental flaws.
    The start/stop button is outside the FOR loop, thus it will NOT get read during execution of the FOR loop, but only before the FOR loop starts. Think dataflow!
    Thus the terminal of the start/stop button belongs inside the FOR loop.
    Your FOR loop iterates 20x, which probably takes about a nanosecond. You will NOT be fast enough to reliably press the start/stop button during a specific iteration of the FOR loop.
    Your code does not "stop and run" a FOR loop (sic). The loop always spins, but executes an empty case when "Stopped". I guess that's what you actually want?
    Once you solve (2), the x indicator will contain a random value whenever you "stop".
    As soon as the 20 iterations complete, the outer while loop spin an things start over, another nanosecond later.
    Place e.g. a 500ms wait inside the FOR loop to solve this.
    Don't place terminals of indicators on top of formula nodes.
    Your formula does not produce any output, so really has no purpose.
    Use a real "stop" button to terminate the while loop (mechanical action: latch when released). Right now you are using a plain switch, which does not reset to false before the next run. This means that you need to manually reset it before running the program again.
    It is oftern useful to operate the VI in execution highlighting mode. You will immediately see that your button does not get read during execution of the FOR loop. Try it!
    LabVIEW Champion . Do more with less code and in less time .

  • How to make a column in Table popin read only

    Hi Everyone
    Could anyone let me know how to make a column in table popin read only.
    Regards

    if you ar eusing an ALV table
    try this
      DATA: lr_salv_wd_table TYPE REF TO iwci_salv_wd_table,
            r_table TYPE REF TO CL_SALV_WD_CONFIG_TABLE.
    * get reference to ALV component interface
      lr_salv_wd_table = wd_this->wd_cpifc_alvmain( ).
    * get ConfigurationModel from ALV Component
      wd_this->r_table = lr_salv_wd_table->get_model( ).
    * init ColumnSettings
      DATA: lr_column_settings TYPE REF TO if_salv_wd_column_settings,
            lr_col_header TYPE REF TO cl_salv_wd_column_header.
      lr_column_settings ?= wd_this->r_table.
    * get table of column settings - each line one column
      DATA: lt_columns TYPE salv_wd_t_column_ref.
      lt_columns = lr_column_settings->get_columns( ).
    * loop over table - in each loop another column can be modified
      DATA: ls_column TYPE salv_wd_s_column_ref.
    * define visible columns (fields) by naming them,
    * exclude others by setting visibility to none
      DATA: ls_tooltip TYPE string.
      LOOP AT lt_columns INTO ls_column.
        " get header of column
        lr_col_header = ls_column-r_column->get_header( ).
        " do settings here
    ENDLOOP.
    Or see this document for more tips.
    <a href="https://www.sdn.sap.comhttp://www.sdn.sap.comhttp://www.sdn.sap.com/irj/sdn/go/portal/prtroot/docs/library/uuid/40794172-b95a-2910-fb98-b86d8a0918b4">https://www.sdn.sap.comhttp://www.sdn.sap.comhttp://www.sdn.sap.com/irj/sdn/go/portal/prtroot/docs/library/uuid/40794172-b95a-2910-fb98-b86d8a0918b4</a>
    regards,
    Joris

Maybe you are looking for