有人可以帮我弄清楚为什么我的一个方法根本没有运行?

时间:2013-03-12 02:51:12

标签: java infinite-loop

为什么当我运行我的程序并输入5时,它允许我输入我的记录,但是当主菜单再次运行并且我输入6时,不运行changePhoneNumber方法并返回主菜单。 while(true)循环是否会以某种方式弄乱事物?  我有一个名为Record的类,看起来像:

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);

    }

和另一个班级...... 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);
}

2 个答案:

答案 0 :(得分:4)

问题是您使用2 Scanners来阅读InputStream。当您打开第二个Scanner时,您将无法使用原始版本阅读,因为第二个将拥有对它的独占访问权限。

对于此应用程序,您可以轻松使用单个Scanner

请参阅:Do not create multiple buffered wrappers on a single InputStream

答案 1 :(得分:1)

正确的方法是对输入流使用一个读取(扫描仪)。编辑上一个答案以使用单个读取选项 有效的完整程序如下:

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);
    }
}