Results 1 to 5 of 5
Like Tree2Likes
  • 1 Post By jim829
  • 1 Post By superhaNds

Thread: bug in program flow?

  1. #1
    pizzapim is offline Member
    Join Date
    Jul 2013
    Posts
    2
    Rep Power
    0

    Default bug in program flow?

    Hello,

    I'm new to Java and I have a problem with this code. When my program gets to the following piece of code it ignores the while loop and automatically goes to the next while loop:
    Java Code:
    while (name[1] == null) {
    			System.out.println("Enter your family's name: ");
    			name[1] = scanner1.nextLine();
    			System.out.println(name[1]);
    }
    If someone sees the bug please leave a reply.
    Here's my class:
    Java Code:
    import java.util.Scanner;
    
    public class test2 {
    	public static void main(String[] args) {
    		Scanner scanner1 = new Scanner(System.in);
    		int[] age;
    		String[] name;
    		age = new int[2];
    		name = new String[2];
    		
    		while (name[0] == null) {
    			System.out.println("Enter your name: ");
    			name[0] = scanner1.nextLine();
    		}
    		while (age[0] == 0) {
    			System.out.println("Enter your age: ");
    			age[0] = scanner1.nextInt();
    		}
    		while (name[1] == null) {
    			System.out.println("Enter your family's name: ");
    			name[1] = scanner1.nextLine();
    			System.out.println(name[1]);
    		}
    		while (age[1] == 0) {
    			System.out.println("How much money yar have?");
    			age[1] = scanner1.nextInt();
    		}
    	}
    }
    greetings, pizzapim
    Last edited by pizzapim; 07-23-2013 at 09:58 PM.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: bug in program flow?

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,411
    Rep Power
    5

    Default Re: bug in program flow?

    First,you should read the forum guidelines mentioned in the FAQ. One of the things it says to do is use code tags for formatting code. I have done it for you here.


    Java Code:
    while (name[1] == null) {
                System.out.println("Enter your family's name: ");
                name[1] = scanner1.nextLine();
                System.out.println(name[1]);
    }
    If someone sees the bug please leave a reply.
    Here's my class:

    Java Code:
    import java.util.Scanner;
     
    public class test2 {
        public static void main(String[] args) {
            Scanner scanner1 = new Scanner(System.in);
            int[] age;
            String[] name;
            age = new int[2];
            name = new String[2];
     
            while (name[0] == null) {
                System.out.println("Enter your name: ");
                name[0] = scanner1.nextLine();
            }
            while (age[0] == 0) {
                System.out.println("Enter your age: ");
                age[0] = scanner1.nextInt();
            }
            while (name[1] == null) {
                System.out.println("Enter your family's name: ");
                name[1] = scanner1.nextLine();
                System.out.println(name[1]);
            }
            while (age[1] == 0) {
                System.out.println("How much money yar have?");
                age[1] = scanner1.nextInt();
            }
        }
    }
    I believe the problem is that after your scanner1.nextInt() is executed, there is still a trailing line terminator in the buffer which is picked up by the next loop without user intervention.

    Regards,
    Jim
    gimbal2 likes this.
    The Java Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  4. #4
    pizzapim is offline Member
    Join Date
    Jul 2013
    Posts
    2
    Rep Power
    0

    Default Re: bug in program flow?

    I figured out that I mustn't create the Scanner outside of the while loop, so I created it inside it and now it works.
    Thanks to jim829!

    Regards, pizzapim

  5. #5
    superhaNds is offline Senior Member
    Join Date
    Apr 2013
    Location
    Sweden
    Posts
    262
    Rep Power
    2

    Default Re: bug in program flow?

    Quote Originally Posted by pizzapim View Post
    I figured out that I mustn't create the Scanner outside of the while loop, so I created it inside it and now it works.
    Thanks to jim829!

    Regards, pizzapim
    You could also just discard the token by using scanner.next();
    gimbal2 likes this.

Similar Threads

  1. Replies: 6
    Last Post: 05-15-2013, 09:30 PM
  2. Flow-Charting?
    By danthegreat in forum Forum Lobby
    Replies: 4
    Last Post: 09-12-2011, 06:24 AM
  3. Web Service flow
    By sameerk in forum Web Frameworks
    Replies: 1
    Last Post: 05-06-2011, 09:56 AM
  4. How to generate Data Flow Diagram(Work Flow Diagram)
    By stsivaraj in forum AWT / Swing
    Replies: 1
    Last Post: 12-22-2010, 11:21 PM
  5. program flow
    By loktamu in forum AWT / Swing
    Replies: 1
    Last Post: 09-17-2008, 04:52 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •