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: Console skips first input in a function and goes forward to next input.

  1. #1
    Junior Member
    Join Date
    Feb 2019
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Console skips first input in a function and goes forward to next input.

    Hello all,

    Once I run the application, the menu is printed and showing available options the user has got to choose from.

    If a user chooses option one the function register() should be called and the console should ask for the name, surname, dob and other parameters which will be assigned to the object inside a patient class which will be stored within linked listed called, patientList.

    However, for some reason, once the function is launched it prints the line asking to enter the first name of the patient but skips the input for the name and goes forward towards printing the second line asking to enter the surname and stops waiting at the input of surname.

    import java.util.LinkedList;
    import java.util.Scanner;
     
    public class Main {
        private static Scanner input = new Scanner(System.in);
        private static LinkedList<Patient> patientList = new LinkedList<>();
     
        public static void main(String[] args) {
     
            boolean quit = false;
            int choice;
     
            printInstructions();
     
            while (!quit) {
     
                System.out.println("Enter your choice: ");
                choice = input.nextInt();
     
                switch (choice) {
                    case 0:
                        printInstructions();
                        break;
                    case 1:
                        register();
                        break;
                    case 2:
                        printPatients();
                        break;
                    case 9:
                        quit = true;
                        break;
                    default:
                        printInstructions();
                        break;
                }
            }
        }
     
        private static void printInstructions() {
            System.out.print(
                    "****************************\n" +
                            "\tArmstrong GP Surgery\n" +
                            "****************************\n" +
                            "\n" +
                            "1. \t Register a new patient.\n" +
                            "2. \t Show Patients.\n" +
                            "9. \t Exit the program.\n");
        }
     
        private static void register() {
     
            System.out.println("To register a new patient please his name:");
            String name = input.nextLine();
     
            System.out.println("To register a new patient please his surname:");
            String surname = input.nextLine();
     
            System.out.println("To register a new patient please his date of birth:");
            String dob = input.nextLine();
     
            System.out.println("To register a new patient please his address:");
            String address = input.nextLine();
     
            System.out.println("To register a new patient please his post code:");
            String post_code = input.nextLine();
     
            System.out.println("To register a new patient please his phone number:");
            String phone = input.nextLine();
     
            System.out.println("To register a new patient please his email address:");
            String email = input.nextLine();
     
            patientList.add(new Patient(name, surname, dob, address, post_code, phone, email));
     
        }
     
        private static void printPatients() {
            if (patientList.size() == 0) {
                System.out.println("No patients registered.");
            } else {
                for (Patient patient : patientList) {
                    System.out.println(patient.getName());
                    System.out.println(patient.getSurname());
                    System.out.println(patient.getUniqueID());
                }
            }
        }
    }

  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: Console skips first input in a function and goes forward to next input.

    once the function is launched it prints the line asking
    Please copy the contents of the console and paste it here. Add some comments showing what is wrong and show the desired output.

    The issue is with the Scanner class. See the following:
    https://christprogramming.wordpress....on-mistakes-1/
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Input error. Program skips several lines of code during execution.
    By TMahoney1979 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: September 21st, 2018, 10:52 AM
  2. Replies: 1
    Last Post: November 2nd, 2012, 02:21 PM
  3. Reading input from console (greek chars)
    By lemmyz in forum File I/O & Other I/O Streams
    Replies: 10
    Last Post: August 16th, 2010, 05:37 AM
  4. Problem with Console Input from Clipboard
    By Nilhanth in forum File I/O & Other I/O Streams
    Replies: 0
    Last Post: March 1st, 2010, 06:47 PM
  5. [SOLVED] help with console input strnig....
    By mdstrauss in forum File I/O & Other I/O Streams
    Replies: 5
    Last Post: August 17th, 2009, 03:59 AM