简体   繁体   中英

Can someone help me figure out why one of my methods is not running at all?

Why when I run my program and enter 5, it allows me to enter my records, but when the main menu runs again and I enter 6, the changePhoneNumber method is not run and it goes back to the main menu. Is the while(true) loop somehow messing things up? I have a class called Record that looks like:

public static void main(String[] args) {
    BankMethods method = new BankMethods();
    Scanner input = new Scanner(System.in);
    int optionSelected = 0;

    while(true){

    System.out.println("5. Add a New Record");
    System.out.println("6. Change the Phone Number in the Current Record");

    optionSelected = input.nextInt();

if (optionSelected == 5){
        Scanner getRecord = new Scanner(System.in);
        System.out.println("Enter First Name: ");
        String firstName = getRecord.nextLine();
        System.out.println("Enter Last Name: ");
        String lastName = getRecord.nextLine();
        System.out.println("Enter Phone Number: ");
        String phoneNumber = getRecord.nextLine();

        method.addNewRecord(firstName, lastName, phoneNumber);


    }
    if (optionSelected == 6){
        System.out.println("What would you like to change your phone "
                + "number to? ");
        String newNumber = input.nextLine();
        method.changePhoneNumber(newNumber);

    }

and the other class...BankMethods:

public class BankMethods {
LinkedList recordInformation = new LinkedList();
Bankdata mainMenu = new Bankdata();


public void addNewRecord(String firstName, String lastName, 
        String phoneNumber){
    recordInformation.add(firstName); recordInformation.add(lastName);
    recordInformation.add(phoneNumber);
}

public void changePhoneNumber(String newNumber){
    recordInformation.set(2, newNumber);
    System.out.println(recordInformation);
}

The problem is that you are using 2 Scanners to read the one InputStream . When you open the second Scanner you will not be able to read using the original one as the second will have exclusive access to it.

For this application you could easily use a single Scanner .

See: Do not create multiple buffered wrappers on a single InputStream

The correct way is to use one read(scanner) for a input stream. Edited the previous answer to use single read option Complete program that works is given below

package com.stackoverflow.framework;

import java.util.LinkedList;
import java.util.Scanner;

public class Record {

    static Scanner input = new Scanner(System.in);

    public static String readData() {
        return (input.nextLine());
    }

    public static void main(String[] args) {
        BankMethods method = new BankMethods();

        int optionSelected = 0;

        while (true) {

            System.out.println("5. Add a New Record");
            System.out
                    .println("6. Change the Phone Number in the Current Record");

            optionSelected = Integer.parseInt(readData());

            if (optionSelected == 5) {
                // Scanner getRecord = new Scanner(System.in);
                System.out.println("Enter First Name: ");
                String firstName = readData();
                System.out.println("Enter Last Name: ");
                String lastName = readData();
                System.out.println("Enter Phone Number: ");
                String phoneNumber = readData();
                method.addNewRecord(firstName, lastName, phoneNumber);

            }
            if (optionSelected == 6) {
                System.out.println("What would you like to change your phone "
                        + "number to? ");
                // Scanner getRecord = new Scanner(System.in);
                String newNumber = readData();
                method.changePhoneNumber(newNumber);

            }
        }
    }
}

class BankMethods {
    LinkedList recordInformation = new LinkedList();

    public void addNewRecord(String firstName, String lastName,
            String phoneNumber) {
        recordInformation.add(firstName);
        recordInformation.add(lastName);
        recordInformation.add(phoneNumber);
    }

    public void changePhoneNumber(String newNumber) {
        recordInformation.set(2, newNumber);
        System.out.println(recordInformation);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM