Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: While loop not incrementing java

  1. #1
    Junior Member
    Join Date
    Sep 2021
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: While loop not incrementing java

    while loop does not seem to increment
    ...
    'i', it always prints out 0
    i is set to 0 at the top of the loop. Move that statement outside of the loop if you do not want the value of i to be set to 0 every time the loop iterates.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. counter not incrementing within for loop - please help!!!
    By javaiscool in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 8th, 2013, 03:46 PM
  2. [SOLVED] Help with the incrementing in a For loop
    By gabie1121 in forum Loops & Control Statements
    Replies: 2
    Last Post: October 26th, 2012, 11:55 AM
  3. Problems with incrementing/decrementing in a for loop..
    By kl2eativ in forum What's Wrong With My Code?
    Replies: 18
    Last Post: June 13th, 2011, 05:15 AM
  4. Return from incrementing for loop?
    By mindlessn00b in forum Loops & Control Statements
    Replies: 16
    Last Post: October 4th, 2010, 12:15 PM
  5. Return from incrementing for loop HELP?!
    By mindlessn00b in forum What's Wrong With My Code?
    Replies: 0
    Last Post: October 3rd, 2010, 02:36 PM

Tags for this Thread