-3

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.

  • 1
    If you're referring to the value of `i` that's because you declared it _inside_ your loop – Jeroen Steenbeeke Dec 03 '21 at 07:17
  • 1
    Please try to reduce your code to a minimal example reproducing the issue! Also, what are all these variables: `i`, `fuN2`, `newRes`, `readRec`, `cn2`, `dt2`, `pa2`, `bt2`, `ch2`, `se2`, `pr2`, `dpr2`? The names do not give away anything and the types are not clear (if they're used only within the loop, you should declare them right where they are used). Make your question easier to understand and you'll have better chances to get an answer! – Amadán Dec 03 '21 at 07:19
  • 1
    @JeroenSteenbeeke No, given that this shall be Java code, `i` has _not_ been declared in that snippet. – Amadán Dec 03 '21 at 07:20
  • 1
    @Amadán thanks for pointing that out. What does happen though is that it gets set to 0 at the start of each iteration. – Jeroen Steenbeeke Dec 03 '21 at 07:21
  • 1
    *lol* I was so overwelmed by the massed of cryptic abbreviations that I _really_ missed this one. Talking about: "clean code" … :-) – Amadán Dec 03 '21 at 07:23
  • @JeroenSteenbeeke i removed it now from the while loop, but it just prints 1 when im expecting 3 – Leonardo DaFishy Dec 03 '21 at 07:23
  • Why are you expecting 3? Could you give us a sample of the file you're trying to process? – Jeroen Steenbeeke Dec 03 '21 at 07:26
  • @LeonardoDaFishy Frankly, I just do not even understand what your code intends to do. You expect the scanner to give you _9 * n_ items. That's about all I understand. Please add a little explanation what you're trying to do! Renaming those variables to anything meaningful and adding information about their types may help, too. – Amadán Dec 03 '21 at 07:27
  • @JeroenSteenbeeke im trying to read a text file into an array, and the file contains four lines, and i separated 9 elements with a comma. so that gives me 9 elements per line in the text file. – Leonardo DaFishy Dec 03 '21 at 07:31

1 Answers1

3

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("===============================");
}
tquadrat
  • 3,033
  • 1
  • 16
  • 29