Строки Java нуль-символ с nextLine () - PullRequest
1 голос
/ 27 сентября 2011
    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 ..

Ответы [ 2 ]

1 голос
/ 27 сентября 2011

То, что сказал 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);

        }

    }
}
1 голос
/ 27 сентября 2011

Просто добавьте scan.nextLine(); после input = scan.nextInt();.Проблема в input = scan.nextInt();, она читает только целое число, поэтому вам нужно scan.nextLine();, чтобы отбросить новую строку \n, созданную нажатием Enter.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...