-3
package contractmanager;
import java.util.*;
/**
*
* @author Tom McCloud
*/
public class ContractManager {
    static Scanner keyb = new Scanner(System.in);
    // global scanner
    public static void main(String[] args) {
        int option;
        //variable declaration
        String clientName;
        String packageSize;
        String dataBundle;
        String reference;
        int period;
        boolean intlCalls;
        //display menu to user
        System.out.println("Welcome: \n");
        System.out.println("1. Enter new contract       ");
        System.out.println("2. Display contract summary");
        System.out.println("3. Display summary of contract for selected month");
        System.out.println("4. Find and display contract");
        System.out.println("0. Exit");

        //take option off user
        option = keyb.nextInt();

        //WIP - only working on option 1 at the minute
        switch(option) {
        case 1:
               clientName = clientName();
               packageSize = packageSize();
               dataBundle = dataBundle();
               reference = reference();
               break;
        }

        exit();

    }

    public static void exit()
    {
        System.out.println("Thank you for using the contract manager. Goodbye!");
    }

    public static String clientName()
    {
        String name = " ";
        System.out.println("Please input your full name: ");
        name = keyb.nextLine(); 


         return name;
    }

    public static String packageSize()
    {
        String size;

        System.out.println("Please input your package size: ");
        System.out.println(" 1. Small \n 2. Medium \n 3. Large");
        size = keyb.next();

        return size; 
    }

    public static String dataBundle()
    {
        String data;

            System.out.println("Please input data bundle size: ");
            System.out.println("1. Low \n 2. Medium \n 3. High \n 4. Unlimited");
            data = keyb.next();


        return data;
    }

    public static String reference()
    {
        String ref;
        boolean isRefValid = false;
        do {
            System.out.println("Please input your reference code: ");
            ref = keyb.next();

            if(ref.length() > 6)
            {
                System.out.println("Reference number too long, re-enter!");
            }

            for(int i = 0; i < 2; i++)
            {
               if(Character.isDigit(ref.charAt(i)))
               {
                   System.out.println("First two characters must be letters!");
               }
            }

        } while(isRefValid = false);

        return ref;     
    }
}

So, this is some code I have. If I press enter code hereone, it executes these, now technically shouldn't this be in order of one another once each method reaches completion and returns?

For example, on execution after pressing "1" I get the following output:

Please input your full name: 
Please input your package size: 
 1. Small 
 2. Medium 
 3. Large

Whereas this should come one by one, after the full name has been inputted it should move onto the package size step. If I input it goes to the third step rather than repeating for the second step's input.

Ian2thedv
  • 2,628
  • 2
  • 22
  • 43

1 Answers1

1

I think it's because in your clientName function you have just printed "Please input your full name: " without waiting for input. For example you have to do something like below here scan.nextLine() will wait until user have press enter:

Scanner scan = new Scanner();
System.out.println("Please input your full name:");

String name= scan.next();

System.out.println(name);

scan.nextLine();

Updated: Try by updating clientName function as below

public static String clientName() {
    String name = " ";
    System.out.println("Please input your full name: ");
    name = keyb.next();

    keyb.nextLine();

    return name;
}
Ian2thedv
  • 2,628
  • 2
  • 22
  • 43
Rishi Saraf
  • 1,394
  • 12
  • 21
  • If you look at my code, you're right, taking `nextLine();` and replacing with `next();` works for the first function, but after the first input, it loads function 2 and 3 at the same time. – Tom McCloud Nov 19 '15 at 13:54
  • Thanks a lot. That works. Am I right in understanding that this is because next() will take everything until the space key is pressed? If so, why does that then load two functions? – Tom McCloud Nov 19 '15 at 14:11