带有nextLine()的字符串java null字符

时间:2011-09-27 16:17:04

标签: java string

    System.out.println("-----------------------------------------------------------");
    System.out.println("Please select a task");
    System.out.println("1: List employees with details");
    System.out.println("2: List of clients in state");
    System.out.println("3: List portfolio of client");
    System.out.println("4: Release an employee");
    System.out.println("5 Display stocks available for purchase/selling");
    System.out.println("6: Display client details");
    System.out.println("7: Buy Stock for client ");
    System.out.println("0: Exit program");
    System.out.println("-----------------------------------------------------------" + "\n" + "\n");

    System.out.print("Input:");

    Scanner scan = new Scanner(System.in);

    input = scan.nextInt();

    if (input == 1)
        {
        System.out.println("length of array list: " + employeeList.size());

        int index = 0;
        while ( index < employeeList.size() )
            {
            System.out.println(employeeList.get(index));
            index++;
            }
        }
    else if (input == 2)
        {
        String state_choice;
        System.out.println("Please enter the abbrivation for the state:");
        state_choice = scan.nextLine();

这是我当前代码的一部分我在[else if(input == 2)]遇到问题当我尝试运行它时不让我输入而实际上只是忽略它。我认为这是因为前一个条目中的“\ n”。有没有办法删除那个“\ n”或额外的字符而不在我的条目之前放入另一个scan.nextLine()?在java docs中没有真正看到任何东西..

2 个答案:

答案 0 :(得分:1)

只需在scan.nextLine();之后添加input = scan.nextInt();即可。问题在input = scan.nextInt();范围内,它只读取整数,因此您需要scan.nextLine();来放弃按Enter键生成的新行\n

答案 1 :(得分:1)

Eng.Fouad所说的只有scanner.nextLine()而非input.nextLine();)

这就是我运行得很好的事情:

import java.util.ArrayList;
import java.util.Scanner;

public class Testing {
    private static final ArrayList<String> employeeList = new ArrayList<String>();

    public static void main(String[] args) {

        System.out.println("---------------------------------------------------------");
        System.out.println("Please select a task");
        System.out.println("1: List employees with details");
        System.out.println("2: List of clients in state");
        System.out.println("3: List portfolio of client");
        System.out.println("4: Release an employee");
        System.out.println("5: Display stocks available for purchase/selling");
        System.out.println("6: Display client details");
        System.out.println("7: Buy Stock for client ");
        System.out.println("0: Exit program");
        System.out.println("---------------------------------------------------------");
        System.out.println("\n\n");

        System.out.print("Input: ");

        Scanner scan = new Scanner(System.in);

        int input = scan.nextInt();
        scan.nextLine();

        if (input == 1) {
            System.out.println("length of array list: " + employeeList.size());

            int index = 0;
            while (index < employeeList.size()) {
                System.out.println(employeeList.get(index));
                index++;
            }
        } else if (input == 2) {
            System.out.print("Please enter the abbrivation for the state: ");
            String state_choice = scan.nextLine();
            System.out.println("State: " + state_choice);

        }

    }
}