开发者

Strings java null character with nextLine()

开发者 https://www.devze.com 2023-04-07 19:52 出处:网络
System.out.println(\"-----------------------------------------------------------\"); System.out.println(\"Please select a task\");
    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.开发者_如何学Cout.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();

This is a portion from my current code I'm having issues at [else if (input == 2)] when I try to run it doesn't let me input and just ignores it actually. I think its because of the "\n" from the previous entry. Is there a way to remove that "\n" or extra character without putting another scan.nextLine() before my entry? Didn't really see anything in java docs..


Just add scan.nextLine(); after input = scan.nextInt();. The problem is within input = scan.nextInt();, it only reads integer number, so you need scan.nextLine(); to discard the new line \n generated by pressing Enter.


What Eng.Fouad said, only scanner.nextLine() not input.nextLine() ;)

Here's what I got running just fine:

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

        }

    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消