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: Input error. Program skips several lines of code during execution.

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

    Default Input error. Program skips several lines of code during execution.

    I am a beginner experimenting with user input and data types. My program, chatbot, has an error in it that I cannot find. When run, it collects my name and age, but then skips right over prompting for my favorite pasttime, as if I had pressed the "enter" key prematurely. It then goes to the next question. I believe that this error has something to do with my use of the 'byte' data type, but I don't know how to fix it.

    Here is my code:

    import java.util.Scanner;
     
    public class ChatBot {
     
    	public static void main(String[] args) {
    		Scanner in = new Scanner(System.in);
     
    		String name;
    		System.out.print("What is your name? ");
    		name = in.nextLine();
    		System.out.println("Hello, " + name + ".  ");
     
    		byte age;
    		System.out.print("How old are you?  ");
    		age = in.nextByte();
     
                    \\ Here is where it skips ahead in the code.
     
    	        System.out.println("So, you're " + age + " years old?  ");
     
    		String hobby;
    		System.out.print("What is your favorite hobby?  ");
    		hobby = in.nextLine();
    		System.out.println("So, you like " + hobby + "?  ");
    		\\ Here is where it picks up.  It does prompt for my mother's name, but not my hobby.
     
    		String mother;
    		System.out.print("What is your mother's name?  ");
    		mother = in.nextLine();
    		System.out.println("So, your mother's name is " + mother + ".  ");
    	}
     
    }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Input error. Program skips several lines of code during execution.

    had pressed the "enter" key prematurely.
    That is because of a Scanner class feature. The next... (except nextLine) methods leave the lineend char in its buffer. The nextLine method reads and returns that lineend char as an empty line. The fix is to call the nextLine method after any calls to next... methods to clear the lineend char.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. My program skips an if statement I want to include in my loop--don't know why
    By ciscocoder in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 3rd, 2013, 09:53 AM
  2. Sqrt Program, skips over method
    By Lashickk in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 21st, 2013, 01:06 PM
  3. [SOLVED] BufferedReader skips odd numbered lines
    By beer-in-box in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 14th, 2013, 07:46 AM
  4. "Program execution skips the "if" statement"!
    By antony1925 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 7th, 2012, 07:15 AM
  5. [SOLVED] New at Java... my if, else if, else program doesn't seem to work, skips to else.Help!
    By KevinE in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 1st, 2010, 03:51 PM

Tags for this Thread