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 it always reads the file correctly and in order. Also got help from the java cheat sheet