Search code examples
javaarrayswhile-loopjava.util.scannerfilewriter

While loop not incrementing java


i'm just starting to learn about java. My while loop does not seem to increment. Here's the snippet of my while loop inside a try and catch:

File file = new File("Reservation.txt");
Scanner sc = new Scanner(file);

    sc.useDelimiter(",");
try {
    while (sc.hasNext()) {
        i = 0;

        newRes[i] = readRec;

        fuN2 = sc.next();
        newRes[i].fullName = fuN2;
        readRec.setFirstName(fuN2);
        System.out.println("\n" + newRes[i].fullName);

        cn2 = sc.next();
        newRes[i].contact = cn2;
        readRec.setContact(cn2);
        System.out.println(newRes[i].contact);

        dt2 = sc.next();
        newRes[i].date = dt2;
        readRec.setDate(dt2);
        System.out.println(newRes[i].date);

        pa2 = sc.nextInt();
        newRes[i].pax = pa2;
        readRec.setPax(pa2);
        System.out.println(newRes[i].pax);

        bt2 = sc.next();
        newRes[i].bday = bt2;
        readRec.setBirthday(bt2);
        System.out.println(newRes[i].bday);

        ch2 = sc.nextInt();
        newRes[i].child = ch2;
        readRec.setChild(ch2);
        System.out.println(newRes[i].child);

        se2 = sc.nextInt();
        newRes[i].senior = se2;
        readRec.setSenior(se2);
        System.out.println(newRes[i].senior);

        pr2 = sc.nextInt();
        newRes[i].j = pr2;
        readRec.setPrice(pr2);
        System.out.println(newRes[i].j);

        dpr2 = sc.nextInt();
        newRes[i].k = dpr2;
        readRec.setDisPrice(dpr2);
        System.out.println(newRes[i].k);
        sc.next();
        sc.nextLine();
        i++;
    }

} catch (NoSuchElementException e)
{
    sc.close();
    System.out.println("===============================");

}

Whenever I try to print out the variable 'i', it always prints out 0, but the it always reads the file correctly and in order.

UPDATE: i removed the i declaration from the while loop, the answer should be below.


Solution

  • The first thing you do inside the loop is to set i to 0 …

    So i is incremented at the end of the loop, but that is reverted at the next cycle.

    But when printing i, you should get 1 always, not 0, when you print after the loop's execution.

    Try this:

    var i = 0;
    File file = new File( "Reservation.txt" );
    Scanner sc = new Scanner( file );
    
    sc.useDelimiter(",");
    try 
    {
      while (sc.hasNext()) 
      {
        // i = 0; Get rid of this line!!!
    
        newRes [i] = readRec;
    
        fuN2 = sc.next();
        newRes [i].fullName = fuN2;
    
        …
    
        dpr2 = sc.nextInt();
        newRes [i].k = dpr2;
        readRec.setDisPrice( dpr2 );
        System.out.println( newRes [i].k );
        sc.next();
        sc.nextLine();
        ++i;
      }
    } 
    catch( NoSuchElementException e )
    {
      sc.close();
      System.out.println("===============================");
    }