reading lines

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • carlos123
    New Member
    • Sep 2007
    • 53

    reading lines

    why doesnt this work i get null values any suggestions? thanks.
    Code:
         if (event.getSource() == submit)
            {
               try {
            BufferedReader in = new BufferedReader(new FileReader("names.txt"));
           if (counter < 3)
           {
               namestring = in.readLine();
               System.out.println(namestring);
               namearray[a] = namestring;
                 //namearray.add(a,namestring);
               counter++;
               a++;
            }
            else if (counter > 3)
            {
              idstring = in.readLine();
     
             
               idint = Integer.parseInt(idstring);
              //  idarray.add(b,idstring);
              idarray[b] = idint;
              counter++;
              b++;
            }
     
            
            in.close();
            for(int a = 0;  a< 3; a++)
            {
                System.out.println(namearray[a]);
            }
            for(int b = 0;  b< 3; b++)
            {
                System.out.println(idarray[b]);
            }
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    What do you do when counter ==3?

    kind regards,

    Jos

    Comment

    • carlos123
      New Member
      • Sep 2007
      • 53

      #3
      oh. thats an error. would i make the second one >2?

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by carlos123
        oh. thats an error. would i make the second one >2?
        If a number is *not* < 3 it is >= 3; < and >= are complementary; try it with a
        couple of examples.

        kind regards,

        Jos

        Comment

        • carlos123
          New Member
          • Sep 2007
          • 53

          #5
          the txt looks like this
          greg
          john
          joe
          54343
          45777
          33444

          i run this program, and it displays

          greg
          greg
          null
          0
          0
          0

          whats up? thanks.


          Code:
           if (event.getSource() == submit)
                  {
                     try {
                  BufferedReader in = new BufferedReader(new FileReader("names.txt"));
                 if (counter <= 3)
                 {
                     namestring = in.readLine();
                
                     namearray[a] = namestring;
                       //namearray.add(a,namestring);
                     counter++;
                     a++;
                  }
                  else if (counter > 3)
                  {
                    idstring = in.readLine();
          
                   
                     idint = Integer.parseInt(idstring);
                    //  idarray.add(b,idstring);
                    idarray[b] = idint;
                    counter++;
                    b++;
                  }
          
                  
                  in.close();
                  for(int a = 0;  a< 3; a++)
                  {
                      System.out.println(namearray[a]);
                  }
                  for(int b = 0;  b< 3; b++)
                  {
                      System.out.println(idarray[b]);
                  }
                  
              } catch (IOException e) {
              }

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            You do realize that you're opening a new BufferedReader everytime you press
            that button don't you? A new BufferedReader starts reading from the start over
            and over again.

            kind regards,

            Jos

            Comment

            Working...